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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
require File.expand_path('../helper', __FILE__)
TESTING_REQUIRE = []
class TestRakeApplicationOptions < Rake::TestCase
def setup
super
clear_argv
Rake::FileUtilsExt.verbose_flag = false
Rake::FileUtilsExt.nowrite_flag = false
TESTING_REQUIRE.clear
end
def teardown
clear_argv
Rake::FileUtilsExt.verbose_flag = false
Rake::FileUtilsExt.nowrite_flag = false
super
end
def clear_argv
ARGV.pop until ARGV.empty?
end
def test_default_options
opts = command_line
assert_nil opts.backtrace
assert_nil opts.dryrun
assert_nil opts.ignore_system
assert_nil opts.load_system
assert_nil opts.always_multitask
assert_nil opts.nosearch
assert_equal ['rakelib'], opts.rakelib
assert_nil opts.show_prereqs
assert_nil opts.show_task_pattern
assert_nil opts.show_tasks
assert_nil opts.silent
assert_nil opts.trace
assert_nil opts.thread_pool_size
assert_equal ['rakelib'], opts.rakelib
assert ! Rake::FileUtilsExt.verbose_flag
assert ! Rake::FileUtilsExt.nowrite_flag
end
def test_dry_run
flags('--dry-run', '-n') do |opts|
assert opts.dryrun
assert opts.trace
assert Rake::FileUtilsExt.verbose_flag
assert Rake::FileUtilsExt.nowrite_flag
end
end
def test_describe
flags('--describe') do |opts|
assert_equal :describe, opts.show_tasks
assert_equal(//.to_s, opts.show_task_pattern.to_s)
end
end
def test_describe_with_pattern
flags('--describe=X') do |opts|
assert_equal :describe, opts.show_tasks
assert_equal(/X/.to_s, opts.show_task_pattern.to_s)
end
end
def test_execute
$xyzzy = 0
flags('--execute=$xyzzy=1', '-e $xyzzy=1') do |opts|
assert_equal 1, $xyzzy
assert_equal :exit, @exit
$xyzzy = 0
end
end
def test_execute_and_continue
$xyzzy = 0
flags('--execute-continue=$xyzzy=1', '-E $xyzzy=1') do |opts|
assert_equal 1, $xyzzy
refute_equal :exit, @exit
$xyzzy = 0
end
end
def test_execute_and_print
$xyzzy = 0
out, = capture_io do
flags('--execute-print=$xyzzy="pugh"', '-p $xyzzy="pugh"') do |opts|
assert_equal 'pugh', $xyzzy
assert_equal :exit, @exit
$xyzzy = 0
end
end
assert_match(/^pugh$/, out)
end
def test_help
out, = capture_io do
flags '--help', '-H', '-h'
end
assert_match(/\Arake/, out)
assert_match(/\boptions\b/, out)
assert_match(/\btargets\b/, out)
assert_equal :exit, @exit
end
def test_jobs
flags([]) do |opts|
assert_nil opts.thread_pool_size
end
flags(['--jobs', '0'], ['-j', '0']) do |opts|
assert_equal 0, opts.thread_pool_size
end
flags(['--jobs', '1'], ['-j', '1']) do |opts|
assert_equal 0, opts.thread_pool_size
end
flags(['--jobs', '4'], ['-j', '4']) do |opts|
assert_equal 3, opts.thread_pool_size
end
flags(['--jobs', 'asdas'], ['-j', 'asdas']) do |opts|
assert_equal Rake.suggested_thread_count-1, opts.thread_pool_size
end
flags('--jobs', '-j') do |opts|
assert opts.thread_pool_size > 1_000_000, "thread pool size should be huge (was #{opts.thread_pool_size})"
end
end
def test_libdir
flags(['--libdir', 'xx'], ['-I', 'xx'], ['-Ixx']) do |opts|
$:.include?('xx')
end
ensure
$:.delete('xx')
end
def test_multitask
flags('--multitask', '-m') do |opts|
assert_equal opts.always_multitask, true
end
end
def test_rakefile
flags(['--rakefile', 'RF'], ['--rakefile=RF'], ['-f', 'RF'], ['-fRF']) do |opts|
assert_equal ['RF'], @app.instance_eval { @rakefiles }
end
end
def test_rakelib
dirs = %w(A B C).join(File::PATH_SEPARATOR)
flags(
['--rakelibdir', dirs],
["--rakelibdir=#{dirs}"],
['-R', dirs],
["-R#{dirs}"]) do |opts|
assert_equal ['A', 'B', 'C'], opts.rakelib
end
end
def test_require
$LOAD_PATH.unshift @tempdir
open 'reqfile.rb', 'w' do |io| io << 'TESTING_REQUIRE << 1' end
open 'reqfile2.rb', 'w' do |io| io << 'TESTING_REQUIRE << 2' end
open 'reqfile3.rake', 'w' do |io| io << 'TESTING_REQUIRE << 3' end
flags(['--require', 'reqfile'], '-rreqfile2', '-rreqfile3')
assert_includes TESTING_REQUIRE, 1
assert_includes TESTING_REQUIRE, 2
assert_includes TESTING_REQUIRE, 3
assert_equal 3, TESTING_REQUIRE.size
ensure
$LOAD_PATH.delete @tempdir
end
def test_missing_require
ex = assert_raises(LoadError) do
flags(['--require', 'test/missing']) do |opts|
end
end
assert_match(/such file/, ex.message)
assert_match(/test\/missing/, ex.message)
end
def test_prereqs
flags('--prereqs', '-P') do |opts|
assert opts.show_prereqs
end
end
def test_quiet
Rake::FileUtilsExt.verbose_flag = true
flags('--quiet', '-q') do |opts|
assert ! Rake::FileUtilsExt.verbose_flag, "verbose flag shoud be false"
assert ! opts.silent, "should not be silent"
end
end
def test_no_search
flags('--nosearch', '--no-search', '-N') do |opts|
assert opts.nosearch
end
end
def test_silent
Rake::FileUtilsExt.verbose_flag = true
flags('--silent', '-s') do |opts|
assert ! Rake::FileUtilsExt.verbose_flag, "verbose flag should be false"
assert opts.silent, "should be silent"
end
end
def test_system
flags('--system', '-g') do |opts|
assert opts.load_system
end
end
def test_no_system
flags('--no-system', '-G') do |opts|
assert opts.ignore_system
end
end
def test_trace
flags('--trace', '-t') do |opts|
assert opts.trace, "should enable trace option"
assert opts.backtrace, "should enabled backtrace option"
assert_equal $stderr, opts.trace_output
assert Rake::FileUtilsExt.verbose_flag
assert ! Rake::FileUtilsExt.nowrite_flag
end
end
def test_trace_with_stdout
flags('--trace=stdout', '-tstdout') do |opts|
assert opts.trace, "should enable trace option"
assert opts.backtrace, "should enabled backtrace option"
assert_equal $stdout, opts.trace_output
assert Rake::FileUtilsExt.verbose_flag
assert ! Rake::FileUtilsExt.nowrite_flag
end
end
def test_trace_with_stderr
flags('--trace=stderr', '-tstderr') do |opts|
assert opts.trace, "should enable trace option"
assert opts.backtrace, "should enabled backtrace option"
assert_equal $stderr, opts.trace_output
assert Rake::FileUtilsExt.verbose_flag
assert ! Rake::FileUtilsExt.nowrite_flag
end
end
def test_trace_with_error
ex = assert_raises(Rake::CommandLineOptionError) do
flags('--trace=xyzzy') do |opts| end
end
assert_match(/un(known|recognized).*\btrace\b.*xyzzy/i, ex.message)
end
def test_trace_with_following_task_name
flags(['--trace', 'taskname'], ['-t', 'taskname']) do |opts|
assert opts.trace, "should enable trace option"
assert opts.backtrace, "should enabled backtrace option"
assert_equal $stderr, opts.trace_output
assert Rake::FileUtilsExt.verbose_flag
assert_equal ['taskname'], @app.top_level_tasks
end
end
def test_backtrace
flags('--backtrace') do |opts|
assert opts.backtrace, "should enable backtrace option"
assert_equal $stderr, opts.trace_output
assert ! opts.trace, "should not enable trace option"
end
end
def test_backtrace_with_stdout
flags('--backtrace=stdout') do |opts|
assert opts.backtrace, "should enable backtrace option"
assert_equal $stdout, opts.trace_output
assert ! opts.trace, "should not enable trace option"
end
end
def test_backtrace_with_stderr
flags('--backtrace=stderr') do |opts|
assert opts.backtrace, "should enable backtrace option"
assert_equal $stderr, opts.trace_output
assert ! opts.trace, "should not enable trace option"
end
end
def test_backtrace_with_error
ex = assert_raises(Rake::CommandLineOptionError) do
flags('--backtrace=xyzzy') do |opts| end
end
assert_match(/un(known|recognized).*\bbacktrace\b.*xyzzy/i, ex.message)
end
def test_backtrace_with_following_task_name
flags(['--backtrace', 'taskname']) do |opts|
assert ! opts.trace, "should enable trace option"
assert opts.backtrace, "should enabled backtrace option"
assert_equal $stderr, opts.trace_output
assert_equal ['taskname'], @app.top_level_tasks
end
end
def test_trace_rules
flags('--rules') do |opts|
assert opts.trace_rules
end
end
def test_tasks
flags('--tasks', '-T') do |opts|
assert_equal :tasks, opts.show_tasks
assert_equal(//.to_s, opts.show_task_pattern.to_s)
assert_equal nil, opts.show_all_tasks
end
flags(['--tasks', 'xyz'], ['-Txyz']) do |opts|
assert_equal :tasks, opts.show_tasks
assert_equal(/xyz/.to_s, opts.show_task_pattern.to_s)
assert_equal nil, opts.show_all_tasks
end
flags(['--tasks', 'xyz', '--comments']) do |opts|
assert_equal :tasks, opts.show_tasks
assert_equal(/xyz/.to_s, opts.show_task_pattern.to_s)
assert_equal false, opts.show_all_tasks
end
end
def test_where
flags('--where', '-W') do |opts|
assert_equal :lines, opts.show_tasks
assert_equal(//.to_s, opts.show_task_pattern.to_s)
assert_equal true, opts.show_all_tasks
end
flags(['--where', 'xyz'], ['-Wxyz']) do |opts|
assert_equal :lines, opts.show_tasks
assert_equal(/xyz/.to_s, opts.show_task_pattern.to_s)
assert_equal true, opts.show_all_tasks
end
flags(['--where', 'xyz', '--comments'], ['-Wxyz', '--comments']) do |opts|
assert_equal :lines, opts.show_tasks
assert_equal(/xyz/.to_s, opts.show_task_pattern.to_s)
assert_equal false, opts.show_all_tasks
end
end
def test_no_deprecated_messages
flags('--no-deprecation-warnings', '-X') do |opts|
assert opts.ignore_deprecate
end
end
def test_verbose
capture_io do
flags('--verbose', '-v') do |opts|
assert Rake::FileUtilsExt.verbose_flag, "verbose should be true"
assert ! opts.silent, "opts should not be silent"
end
end
end
def test_version
out, _ = capture_io do
flags '--version', '-V'
end
assert_match(/\bversion\b/, out)
assert_match(/\b#{RAKEVERSION}\b/, out)
assert_equal :exit, @exit
end
def test_bad_option
_, err = capture_io do
ex = assert_raises(OptionParser::InvalidOption) do
flags('--bad-option')
end
if ex.message =~ /^While/ # Ruby 1.9 error message
assert_match(/while parsing/i, ex.message)
else # Ruby 1.8 error message
assert_match(/(invalid|unrecognized) option/i, ex.message)
assert_match(/--bad-option/, ex.message)
end
end
assert_equal '', err
end
def test_task_collection
command_line("a", "b")
assert_equal ["a", "b"], @tasks.sort
end
def test_default_task_collection
command_line()
assert_equal ["default"], @tasks
end
def test_environment_definition
ENV.delete('TESTKEY')
command_line("TESTKEY=12")
assert_equal '12', ENV['TESTKEY']
end
def test_multiline_environment_definition
ENV.delete('TESTKEY')
command_line("TESTKEY=a\nb\n")
assert_equal "a\nb\n", ENV['TESTKEY']
end
def test_environment_and_tasks_together
ENV.delete('TESTKEY')
command_line("a", "b", "TESTKEY=12")
assert_equal ["a", "b"], @tasks.sort
assert_equal '12', ENV['TESTKEY']
end
def test_rake_explicit_task_library
Rake.add_rakelib 'app/task', 'other'
libs = Rake.application.options.rakelib
assert libs.include?("app/task")
assert libs.include?("other")
end
private
def flags(*sets)
sets.each do |set|
ARGV.clear
@exit = catch(:system_exit) { command_line(*set) }
yield(@app.options) if block_given?
end
end
def command_line(*options)
options.each do |opt| ARGV << opt end
@app = Rake::Application.new
def @app.exit(*args)
throw :system_exit, :exit
end
@app.instance_eval do
args = handle_options
collect_command_line_tasks(args)
end
@tasks = @app.top_level_tasks
@app.options
end
end
|