forked from asciidoctor/asciidoctor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.rb
330 lines (276 loc) · 12.5 KB
/
options_test.rb
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
# frozen_string_literal: true
require_relative 'test_helper'
require File.join Asciidoctor::LIB_DIR, 'asciidoctor/cli/options'
context 'Options' do
test 'should print usage and return error code 0 when help flag is present' do
redirect_streams do |stdout|
exitval = Asciidoctor::Cli::Options.parse! %w(-h)
assert_equal 0, exitval
assert_match(/^Usage:/, stdout.string)
end
end
test 'should show safe modes in severity order' do
redirect_streams do |stdout|
exitval = Asciidoctor::Cli::Options.parse! %w(-h)
assert_equal 0, exitval
assert_match(/unsafe, safe, server, secure/, stdout.string)
end
end
test 'should print usage and return error code 0 when help flag is unknown' do
exitval, output = redirect_streams do |out, _|
[Asciidoctor::Cli::Options.parse!(%w(-h unknown)), out.string]
end
assert_equal 0, exitval
assert_match(/^Usage:/, output)
end
test 'should dump man page and return error code 0 when help topic is manpage' do
exitval, output = redirect_streams do |out, _|
[Asciidoctor::Cli::Options.parse!(%w(-h manpage)), out.string]
end
assert_equal 0, exitval
assert_includes output, 'Manual: Asciidoctor Manual'
assert_includes output, '.TH "ASCIIDOCTOR"'
end
test 'should an overview of the AsciiDoc syntax and return error code 0 when help topic is syntax' do
exitval, output = redirect_streams do |out, _|
[Asciidoctor::Cli::Options.parse!(%w(-h syntax)), out.string]
end
assert_equal 0, exitval
assert_includes output, '= AsciiDoc Syntax'
assert_includes output, '== Text Formatting'
end
test 'should print message and return error code 1 when manpage is not found' do
old_manpage_path = ENV['ASCIIDOCTOR_MANPAGE_PATH']
begin
ENV['ASCIIDOCTOR_MANPAGE_PATH'] = (manpage_path = fixture_path 'no-such-file.1')
redirect_streams do |_, stderr|
exitval = Asciidoctor::Cli::Options.parse! %w(-h manpage)
assert_equal 1, exitval
assert_equal %(asciidoctor: FAILED: manual page not found: #{manpage_path}), stderr.string.chomp
end
ensure
if old_manpage_path
ENV['ASCIIDOCTOR_MANPAGE_PATH'] = old_manpage_path
else
ENV.delete 'ASCIIDOCTOR_MANPAGE_PATH'
end
end
end
test 'should return error code 1 when invalid option present' do
redirect_streams do |_, stderr|
exitval = Asciidoctor::Cli::Options.parse! %w(--foobar)
assert_equal 1, exitval
assert_equal 'asciidoctor: invalid option: --foobar', stderr.string.chomp
end
end
test 'should return error code 1 when option has invalid argument' do
redirect_streams do |_, stderr|
exitval = Asciidoctor::Cli::Options.parse! %w(-d chapter input.ad) # had to change for #320
assert_equal 1, exitval
assert_equal 'asciidoctor: invalid argument: -d chapter', stderr.string.chomp
end
end
test 'should return error code 1 when option is missing required argument' do
redirect_streams do |_, stderr|
exitval = Asciidoctor::Cli::Options.parse! %w(-b)
assert_equal 1, exitval
assert_equal 'asciidoctor: option missing argument: -b', stderr.string.chomp
end
end
test 'should emit warning when unparsed options remain' do
redirect_streams do |_, stderr|
options = Asciidoctor::Cli::Options.parse! %w(-b docbook - -)
assert_kind_of Hash, options
assert_match(/asciidoctor: WARNING: extra arguments .*/, stderr.string.chomp)
end
end
test 'basic argument assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-w -v -e -d book test/fixtures/sample.adoc)
assert_equal 2, options[:verbose]
refute options[:standalone]
assert_equal 'book', options[:attributes]['doctype']
assert_equal 1, options[:input_files].size
assert_equal 'test/fixtures/sample.adoc', options[:input_files][0]
end
test 'supports legacy option for no header footer' do
options = Asciidoctor::Cli::Options.parse! %w(-s test/fixtures/sample.adoc)
refute options[:standalone]
assert_equal 1, options[:input_files].size
assert_equal 'test/fixtures/sample.adoc', options[:input_files][0]
end
test 'standard attribute assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-a docinfosubs=attributes,replacements -a icons test/fixtures/sample.adoc)
assert_equal 'attributes,replacements', options[:attributes]['docinfosubs']
assert_equal '', options[:attributes]['icons']
end
test 'multiple attribute arguments' do
options = Asciidoctor::Cli::Options.parse! %w(-a imagesdir=images -a icons test/fixtures/sample.adoc)
assert_equal 'images', options[:attributes]['imagesdir']
assert_equal '', options[:attributes]['icons']
end
test 'should only split attribute key/value pairs on first equal sign' do
options = Asciidoctor::Cli::Options.parse! %w(-a name=value=value test/fixtures/sample.adoc)
assert_equal 'value=value', options[:attributes]['name']
end
test 'should not fail if value of attribute option is empty' do
options = Asciidoctor::Cli::Options.parse! ['-a', '', 'test/fixtures/sample.adoc']
assert_nil options[:attributes]
end
test 'should not fail if value of attribute option is equal sign' do
options = Asciidoctor::Cli::Options.parse! ['-a', '=', 'test/fixtures/sample.adoc']
assert_nil options[:attributes]
end
test 'should allow safe mode to be specified' do
options = Asciidoctor::Cli::Options.parse! %w(-S safe test/fixtures/sample.adoc)
assert_equal Asciidoctor::SafeMode::SAFE, options[:safe]
end
test 'should allow any backend to be specified' do
options = Asciidoctor::Cli::Options.parse! %w(-b my_custom_backend test/fixtures/sample.adoc)
assert_equal 'my_custom_backend', options[:attributes]['backend']
end
test 'article doctype assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-d article test/fixtures/sample.adoc)
assert_equal 'article', options[:attributes]['doctype']
end
test 'book doctype assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-d book test/fixtures/sample.adoc)
assert_equal 'book', options[:attributes]['doctype']
end
test 'inline doctype assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-d inline test/fixtures/sample.adoc)
assert_equal 'inline', options[:attributes]['doctype']
end
test 'template engine assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-E haml test/fixtures/sample.adoc)
assert_equal 'haml', options[:template_engine]
end
test 'template directory assignment' do
options = Asciidoctor::Cli::Options.parse! %w(-T custom-backend test/fixtures/sample.adoc)
assert_equal ['custom-backend'], options[:template_dirs]
end
test 'multiple template directory assignments' do
options = Asciidoctor::Cli::Options.parse! %w(-T custom-backend -T custom-backend-hacks test/fixtures/sample.adoc)
assert_equal %w(custom-backend custom-backend-hacks), options[:template_dirs]
end
test 'multiple -r flags requires specified libraries' do
options = Asciidoctor::Cli::Options.new
redirect_streams do |_, stderr|
exitval = options.parse! %w(-r foobar -r foobaz test/fixtures/sample.adoc)
assert_match %(asciidoctor: FAILED: 'foobar' could not be loaded), stderr.string
assert_equal 1, exitval
assert_equal %w(foobar foobaz), options[:requires]
end
end
test '-r flag with multiple values requires specified libraries' do
options = Asciidoctor::Cli::Options.new
redirect_streams do |_, stderr|
exitval = options.parse! %w(-r foobar,foobaz test/fixtures/sample.adoc)
assert_match %(asciidoctor: FAILED: 'foobar' could not be loaded), stderr.string
assert_equal 1, exitval
assert_equal %w(foobar foobaz), options[:requires]
end
end
test '-I option appends paths to $LOAD_PATH' do
options = Asciidoctor::Cli::Options.new
old_load_path = $:.dup
begin
exitval = options.parse! %w(-I foobar -I foobaz test/fixtures/sample.adoc)
refute_equal 1, exitval
assert_equal old_load_path.size + 2, $:.size
assert_equal File.expand_path('foobar'), $:[0]
assert_equal File.expand_path('foobaz'), $:[1]
assert_equal %w(foobar foobaz), options[:load_paths]
ensure
($:.size - old_load_path.size).times { $:.shift }
end
end
test '-I option appends multiple paths to $LOAD_PATH' do
options = Asciidoctor::Cli::Options.new
old_load_path = $:.dup
begin
exitval = options.parse! %W(-I foobar#{File::PATH_SEPARATOR}foobaz test/fixtures/sample.adoc)
refute_equal 1, exitval
assert_equal old_load_path.size + 2, $:.size
assert_equal File.expand_path('foobar'), $:[0]
assert_equal File.expand_path('foobaz'), $:[1]
assert_equal %w(foobar foobaz), options[:load_paths]
ensure
($:.size - old_load_path.size).times { $:.shift }
end
end
test 'should set failure level to FATAL by default' do
options = Asciidoctor::Cli::Options.parse! %w(test/fixtures/sample.adoc)
assert_equal Logger::Severity::FATAL, options[:failure_level]
end
test 'should allow failure level to be set to FATAL using any recognized abbreviation' do
%w(f fatal FATAL).each do |val|
options = Asciidoctor::Cli::Options.parse! %W(--failure-level=#{val} test/fixtures/sample.adoc)
assert_equal Logger::Severity::FATAL, options[:failure_level]
end
end
test 'should allow failure level to be set to ERROR using any recognized abbreviation' do
%w(e err ERR error ERROR).each do |val|
options = Asciidoctor::Cli::Options.parse! %W(--failure-level=#{val} test/fixtures/sample.adoc)
assert_equal Logger::Severity::ERROR, options[:failure_level]
end
end
test 'should allow failure level to be set to WARN using any recognized abbreviation' do
%w(w warn WARN warning WARNING).each do |val|
options = Asciidoctor::Cli::Options.parse! %W(--failure-level=#{val} test/fixtures/sample.adoc)
assert_equal Logger::Severity::WARN, options[:failure_level]
end
end
test 'should not allow failure level to be set to unknown value' do
exit_code, messages = redirect_streams do |_, err|
[(Asciidoctor::Cli::Options.parse! %w(--failure-level=foobar test/fixtures/sample.adoc)), err.string]
end
assert_equal 1, exit_code
assert_includes messages, 'invalid argument: --failure-level=foobar'
end
test 'should set log level to DEBUG when --log-level option is specified' do
options = Asciidoctor::Cli::Options.parse! %w(--log-level debug test/fixtures/sample.adoc)
assert_equal Logger::Severity::DEBUG, options[:log_level]
end
test 'should allow log level to be set to WARN using any recognized abbreviation' do
%w(w warn WARN warning WARNING).each do |val|
options = Asciidoctor::Cli::Options.parse! %W(--log-level=#{val} test/fixtures/sample.adoc)
assert_equal Logger::Severity::WARN, options[:log_level]
end
end
test 'should set verbose to 2 when -v flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(-v test/fixtures/sample.adoc)
assert_equal 2, options[:verbose]
end
test 'should set verbose to 0 when -q flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(-q test/fixtures/sample.adoc)
assert_equal 0, options[:verbose]
end
test 'should set verbose to 2 when -v flag is specified after -q flag' do
options = Asciidoctor::Cli::Options.parse! %w(-q -v test/fixtures/sample.adoc)
assert_equal 2, options[:verbose]
end
test 'should set verbose to 0 when -q flag is specified after -v flag' do
options = Asciidoctor::Cli::Options.parse! %w(-v -q test/fixtures/sample.adoc)
assert_equal 0, options[:verbose]
end
test 'should enable warnings when -w flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(-w test/fixtures/sample.adoc)
assert options[:warnings]
end
test 'should enable timings when -t flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(-t test/fixtures/sample.adoc)
assert options[:timings]
end
test 'timings option is disabled by default' do
options = Asciidoctor::Cli::Options.parse! %w(test/fixtures/sample.adoc)
refute options[:timings]
end
test 'should enable sourcemap when --sourcemap flag is specified' do
options = Asciidoctor::Cli::Options.parse! %w(--sourcemap test/fixtures/sample.adoc)
assert options[:sourcemap]
end
test 'sourcemap option is disabled by default' do
options = Asciidoctor::Cli::Options.parse! %w(test/fixtures/sample.adoc)
refute options[:sourcemap]
end
end