1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
# encoding: UTF-8
require 'test_helper'
require 'asciidoctor/cli/options'
require 'asciidoctor/cli/invoker'
context 'Invoker' do
test 'should parse source and render as html5 article by default' do
invoker = nil
output = nil
redirect_streams do |stdout, stderr|
invoker = invoke_cli %w(-o -)
output = stdout.string
end
assert !invoker.nil?
doc = invoker.document
assert !doc.nil?
assert_equal 'Document Title', doc.doctitle
assert_equal 'Doc Writer', doc.attr('author')
assert_equal 'html5', doc.attr('backend')
assert_equal '.html', doc.attr('outfilesuffix')
assert_equal 'article', doc.attr('doctype')
assert doc.blocks?
assert_equal :preamble, doc.blocks.first.context
assert !output.empty?
assert_xpath '/html', output, 1
assert_xpath '/html/head', output, 1
assert_xpath '/html/body', output, 1
assert_xpath '/html/head/title[text() = "Document Title"]', output, 1
assert_xpath '/html/body[@class="article"]/*[@id="header"]/h1[text() = "Document Title"]', output, 1
end
test 'should set implicit doc info attributes' do
sample_filepath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample.asciidoc'))
sample_filedir = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
invoker = invoke_cli_to_buffer %w(-o /dev/null), sample_filepath
doc = invoker.document
assert_equal 'sample', doc.attr('docname')
assert_equal sample_filepath, doc.attr('docfile')
assert_equal sample_filedir, doc.attr('docdir')
assert doc.attr?('docdate')
assert doc.attr?('doctime')
assert doc.attr?('docdatetime')
assert invoker.read_output.empty?
end
test 'should accept document from stdin and write to stdout' do
invoker = invoke_cli_to_buffer(%w(-s), '-') { 'content' }
doc = invoker.document
assert !doc.attr?('docname')
assert !doc.attr?('docfile')
assert_equal Dir.pwd, doc.attr('docdir')
assert_equal doc.attr('docdate'), doc.attr('localdate')
assert_equal doc.attr('doctime'), doc.attr('localtime')
assert_equal doc.attr('docdatetime'), doc.attr('localdatetime')
assert !doc.attr?('outfile')
output = invoker.read_output
assert !output.empty?
assert_xpath '/*[@class="paragraph"]/p[text()="content"]', output, 1
end
test 'should allow docdir to be specified when input is a string' do
expected_docdir = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
invoker = invoke_cli_to_buffer(%w(-s --base-dir test/fixtures -o /dev/null), '-') { 'content' }
doc = invoker.document
assert_equal expected_docdir, doc.attr('docdir')
assert_equal expected_docdir, doc.base_dir
end
test 'should display version and exit' do
redirect_streams do |stdout, stderr|
invoke_cli %w(--version)
assert_equal "Asciidoctor #{Asciidoctor::VERSION} [http://asciidoctor.org]", stdout.string.chomp
end
end
test 'should report usage if no input file given' do
redirect_streams do |stdout, stderr|
invoke_cli [], nil
assert_match(/Usage:/, stderr.string)
end
end
test 'should report error if input file does not exist' do
redirect_streams do |stdout, stderr|
invoker = invoke_cli [], 'missing_file.asciidoc'
assert_match(/input file .* missing/, stderr.string)
assert_equal 1, invoker.code
end
end
test 'should warn if extra arguments are detected' do
redirect_streams do |stdout, stderr|
invoker = invoke_cli %w(-o /dev/null extra arguments sample.asciidoc), nil
assert_match(/extra arguments detected/, stderr.string)
assert_equal 1, invoker.code
end
end
test 'should output to file name based on input file name' do
sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample.html'))
begin
invoker = invoke_cli
doc = invoker.document
assert_equal sample_outpath, doc.attr('outfile')
assert File.exist?(sample_outpath)
output = File.read(sample_outpath)
assert !output.empty?
assert_xpath '/html', output, 1
assert_xpath '/html/head', output, 1
assert_xpath '/html/body', output, 1
assert_xpath '/html/head/title[text() = "Document Title"]', output, 1
assert_xpath '/html/body/*[@id="header"]/h1[text() = "Document Title"]', output, 1
ensure
FileUtils::rm_f(sample_outpath)
end
end
test 'should output to file in destination directory if set' do
destination_path = File.expand_path(File.join(File.dirname(__FILE__), 'test_output'))
sample_outpath = File.join(destination_path, 'sample.html')
begin
FileUtils::mkdir_p(destination_path)
# QUESTION should -D be relative to working directory or source directory?
invoker = invoke_cli %w(-D test/test_output)
#invoker = invoke_cli %w(-D ../../test/test_output)
doc = invoker.document
assert_equal sample_outpath, doc.attr('outfile')
assert File.exist?(sample_outpath)
ensure
FileUtils::rm_f(sample_outpath)
FileUtils::rmdir(destination_path)
end
end
test 'should output to file specified' do
sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample-output.html'))
begin
invoker = invoke_cli %W(-o #{sample_outpath})
doc = invoker.document
assert_equal sample_outpath, doc.attr('outfile')
assert File.exist?(sample_outpath)
ensure
FileUtils::rm_f(sample_outpath)
end
end
test 'should copy default css to target directory if linkcss and copycss are specified' do
sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample-output.html'))
default_stylesheet = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'asciidoctor.css'))
begin
invoker = invoke_cli %W(-o #{sample_outpath} -a linkcss -a copycss)
invoker.document
assert File.exist?(sample_outpath)
assert File.exist?(default_stylesheet)
ensure
FileUtils::rm_f(sample_outpath)
FileUtils::rm_f(default_stylesheet)
end
end
test 'should not copy default css to target directory if linkcss is set and copycss is not' do
sample_outpath = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'sample-output.html'))
default_stylesheet = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'asciidoctor.css'))
begin
invoker = invoke_cli %W(-o #{sample_outpath} -a linkcss -a copycss!)
invoker.document
assert File.exist?(sample_outpath)
assert !File.exist?(default_stylesheet)
ensure
FileUtils::rm_f(sample_outpath)
end
end
test 'should suppress header footer if specified' do
invoker = invoke_cli_to_buffer %w(-s -o -)
output = invoker.read_output
assert_xpath '/html', output, 0
assert_xpath '/*[@id="preamble"]', output, 1
end
test 'should not compact output by default' do
# NOTE we are relying on the fact that the template leaves blank lines
# this will always fail when using a template engine which strips blank lines by default
invoker = invoke_cli_to_buffer(%w(-o -), '-') { '* content' }
output = invoker.read_output
assert_match(/\n[[:blank:]]*\n/, output)
end
test 'should compact output if specified' do
# NOTE we are relying on the fact that the template leaves blank lines
# this will always succeed when using a template engine which strips blank lines by default
invoker = invoke_cli_to_buffer(%w(-C -s -o -), '-') { '* content' }
output = invoker.read_output
assert_no_match(/\n[[:blank:]]*\n/, output)
end
test 'should output a trailing endline to stdout' do
invoker = nil
output = nil
redirect_streams do |stdout, stderr|
invoker = invoke_cli %w(-o -)
output = stdout.string
end
assert !invoker.nil?
assert !output.nil?
assert output.end_with?("\n")
end
test 'should set backend to html5 if specified' do
invoker = invoke_cli_to_buffer %w(-b html5 -o -)
doc = invoker.document
assert_equal 'html5', doc.attr('backend')
assert_equal '.html', doc.attr('outfilesuffix')
output = invoker.read_output
assert_xpath '/html', output, 1
end
test 'should set backend to docbook45 if specified' do
invoker = invoke_cli_to_buffer %w(-b docbook45 -o -)
doc = invoker.document
assert_equal 'docbook45', doc.attr('backend')
assert_equal '.xml', doc.attr('outfilesuffix')
output = invoker.read_output
assert_xpath '/article', output, 1
end
test 'should set doctype to article if specified' do
invoker = invoke_cli_to_buffer %w(-d article -o -)
doc = invoker.document
assert_equal 'article', doc.attr('doctype')
output = invoker.read_output
assert_xpath '/html/body[@class="article"]', output, 1
end
test 'should set doctype to book if specified' do
invoker = invoke_cli_to_buffer %w(-d book -o -)
doc = invoker.document
assert_equal 'book', doc.attr('doctype')
output = invoker.read_output
assert_xpath '/html/body[@class="book"]', output, 1
end
test 'should set attribute with value' do
invoker = invoke_cli_to_buffer %w(--trace -a idprefix=id -s -o -)
doc = invoker.document
assert_equal 'id', doc.attr('idprefix')
output = invoker.read_output
assert_xpath '//h2[@id="idsection_a"]', output, 1
end
test 'should set attribute with value containing equal sign' do
invoker = invoke_cli_to_buffer %w(--trace -a toc -a toc-title=t=o=c -o -)
doc = invoker.document
assert_equal 't=o=c', doc.attr('toc-title')
output = invoker.read_output
assert_xpath '//*[@id="toctitle"][text() = "t=o=c"]', output, 1
end
test 'should set attribute with quoted value containing a space' do
# emulating commandline arguments: --trace -a toc -a note-caption="Note to self:" -o -
invoker = invoke_cli_to_buffer %w(--trace -a toc -a note-caption=Note\ to\ self: -o -)
doc = invoker.document
assert_equal 'Note to self:', doc.attr('note-caption')
output = invoker.read_output
assert_xpath %(//*[#{contains_class('admonitionblock')}]//*[@class='title'][text() = 'Note to self:']), output, 1
end
test 'should not set attribute ending in @ if defined in document' do
invoker = invoke_cli_to_buffer %w(--trace -a idprefix=id@ -s -o -)
doc = invoker.document
assert_equal 'id_', doc.attr('idprefix')
output = invoker.read_output
assert_xpath '//h2[@id="id_section_a"]', output, 1
end
test 'should set attribute with no value' do
invoker = invoke_cli_to_buffer %w(-a icons -s -o -)
doc = invoker.document
assert_equal '', doc.attr('icons')
output = invoker.read_output
assert_xpath '//*[@class="admonitionblock note"]//img[@alt="Note"]', output, 1
end
test 'should unset attribute ending in bang' do
invoker = invoke_cli_to_buffer %w(-a sectids! -s -o -)
doc = invoker.document
assert !doc.attr?('sectids')
output = invoker.read_output
# leave the count loose in case we add more sections
assert_xpath '//h2[not(@id)]', output
end
test 'default mode for cli should be unsafe' do
invoker = invoke_cli_to_buffer %w(-o /dev/null)
doc = invoker.document
assert_equal Asciidoctor::SafeMode::UNSAFE, doc.safe
end
test 'should set safe mode if specified' do
invoker = invoke_cli_to_buffer %w(--safe -o /dev/null)
doc = invoker.document
assert_equal Asciidoctor::SafeMode::SAFE, doc.safe
end
test 'should set safe mode to specified level' do
levels = {
'unsafe' => Asciidoctor::SafeMode::UNSAFE,
'safe' => Asciidoctor::SafeMode::SAFE,
'server' => Asciidoctor::SafeMode::SERVER,
'secure' => Asciidoctor::SafeMode::SECURE,
}
levels.each do |name, const|
invoker = invoke_cli_to_buffer %W(-S #{name} -o /dev/null)
doc = invoker.document
assert_equal const, doc.safe
end
end
test 'should set eRuby impl if specified' do
invoker = invoke_cli_to_buffer %w(--eruby erubis -o /dev/null)
doc = invoker.document
assert_equal 'erubis', doc.instance_variable_get('@options')[:eruby]
end
test 'should force default external encoding to UTF-8' do
executable = File.expand_path(File.join(File.dirname(__FILE__), '..', 'bin', 'asciidoctor'))
input_path = fixture_path 'encoding.asciidoc'
old_lang = ENV['LANG']
ENV['LANG'] = 'US-ASCII'
begin
# using open3 to work around a bug in JRuby process_manager.rb,
# which tries to run a gsub on stdout prematurely breaking the test
require 'open3'
#cmd = "#{executable} -o - --trace #{input_path}"
cmd = "#{File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']} #{executable} -o - --trace #{input_path}"
_, stdout, stderr = Open3.popen3 cmd
stderr_lines = stderr.readlines
puts stderr_lines.join unless stderr_lines.empty?
assert stderr_lines.empty?, 'Command failed. Expected to receive a rendered document.'
stdout_lines = stdout.readlines
assert !stdout_lines.empty?
if Asciidoctor::FORCE_ENCODING
stdout_lines.each do |l|
l.force_encoding Encoding::UTF_8
end
end
stdout_str = stdout_lines.join
assert stdout_str.include?('Codierungen sind verrückt auf älteren Versionen von Ruby')
ensure
ENV['LANG'] = old_lang
end
end
end
|