rake 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rake might be problematic. Click here for more details.

File without changes
@@ -10,12 +10,14 @@ require 'test/unit'
10
10
  require 'rake'
11
11
  require 'test/rake_test_setup'
12
12
  require 'test/capture_stdout'
13
+ require 'test/in_environment'
13
14
 
14
15
  TESTING_REQUIRE = [ ]
15
16
 
16
17
  ######################################################################
17
18
  class TestApplication < Test::Unit::TestCase
18
19
  include CaptureStdout
20
+ include InEnvironment
19
21
 
20
22
  def setup
21
23
  @app = Rake::Application.new
@@ -39,12 +41,61 @@ class TestApplication < Test::Unit::TestCase
39
41
  end
40
42
 
41
43
  def test_display_tasks_with_long_comments
44
+ in_environment('RAKE_COLUMNS' => '80') do
45
+ @app.options.show_task_pattern = //
46
+ @app.last_description = "1234567890" * 8
47
+ @app.define_task(Rake::Task, "t")
48
+ out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
49
+ assert_match(/^rake t/, out)
50
+ assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
51
+ end
52
+ end
53
+
54
+ def test_display_tasks_with_task_name_wider_than_tty_display
55
+ in_environment('RAKE_COLUMNS' => '80') do
56
+ @app.options.show_task_pattern = //
57
+ description = "something short"
58
+ task_name = "task name" * 80
59
+ @app.last_description = "something short"
60
+ @app.define_task(Rake::Task, task_name )
61
+ out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
62
+ # Ensure the entire task name is output and we end up showing no description
63
+ assert_match(/rake #{task_name} # .../, out)
64
+ end
65
+ end
66
+
67
+ def test_display_tasks_with_very_long_task_name_to_a_non_tty_shows_name_and_comment
42
68
  @app.options.show_task_pattern = //
43
- @app.last_description = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
69
+ @app.tty_output = false
70
+ description = "something short"
71
+ task_name = "task name" * 80
72
+ @app.last_description = "something short"
73
+ @app.define_task(Rake::Task, task_name )
74
+ out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
75
+ # Ensure the entire task name is output and we end up showing no description
76
+ assert_match(/rake #{task_name} # #{description}/, out)
77
+ end
78
+
79
+ def test_display_tasks_with_long_comments_to_a_non_tty_shows_entire_comment
80
+ @app.options.show_task_pattern = //
81
+ @app.tty_output = false
82
+ @app.last_description = "1234567890" * 8
44
83
  @app.define_task(Rake::Task, "t")
45
84
  out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
46
85
  assert_match(/^rake t/, out)
47
- assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
86
+ assert_match(/# #{@app.last_description}/, out)
87
+ end
88
+
89
+ def test_display_tasks_with_long_comments_to_a_non_tty_with_columns_set_truncates_comments
90
+ in_environment("RAKE_COLUMNS" => '80') do
91
+ @app.options.show_task_pattern = //
92
+ @app.tty_output = false
93
+ @app.last_description = "1234567890" * 8
94
+ @app.define_task(Rake::Task, "t")
95
+ out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
96
+ assert_match(/^rake t/, out)
97
+ assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
98
+ end
48
99
  end
49
100
 
50
101
  def test_display_tasks_with_full_descriptions
@@ -58,8 +109,7 @@ class TestApplication < Test::Unit::TestCase
58
109
  end
59
110
 
60
111
  def test_finding_rakefile
61
- assert @app.instance_eval { have_rakefile }
62
- assert_equal "rakefile", @app.rakefile.downcase
112
+ assert_equal "rakefile", @app.instance_eval { have_rakefile }
63
113
  end
64
114
 
65
115
  def test_not_finding_rakefile
@@ -69,52 +119,114 @@ class TestApplication < Test::Unit::TestCase
69
119
  end
70
120
 
71
121
  def test_load_rakefile
72
- original_dir = Dir.pwd
73
- Dir.chdir("test/data/unittest")
74
- @app.instance_eval do
75
- handle_options
76
- options.silent = true
77
- load_rakefile
78
- end
79
- assert_equal "rakefile", @app.rakefile.downcase
80
- assert_match(%r(unittest$), Dir.pwd)
81
- ensure
82
- Dir.chdir(original_dir)
122
+ in_environment("PWD" => "test/data/unittest") do
123
+ @app.instance_eval do
124
+ handle_options
125
+ options.silent = true
126
+ load_rakefile
127
+ end
128
+ assert_equal "rakefile", @app.rakefile.downcase
129
+ assert_match(%r(unittest$), Dir.pwd)
130
+ end
83
131
  end
84
132
 
85
133
  def test_load_rakefile_from_subdir
86
- original_dir = Dir.pwd
87
- Dir.chdir("test/data/unittest/subdir")
88
- @app.instance_eval do
89
- handle_options
90
- options.silent = true
91
- load_rakefile
134
+ in_environment("PWD" => "test/data/unittest/subdir") do
135
+ @app.instance_eval do
136
+ handle_options
137
+ options.silent = true
138
+ load_rakefile
139
+ end
140
+ assert_equal "rakefile", @app.rakefile.downcase
141
+ assert_match(%r(unittest$), Dir.pwd)
92
142
  end
93
- assert_equal "rakefile", @app.rakefile.downcase
94
- assert_match(%r(unittest$), Dir.pwd)
95
- ensure
96
- Dir.chdir(original_dir)
97
143
  end
98
144
 
99
145
  def test_load_rakefile_not_found
100
- original_dir = Dir.pwd
101
- Dir.chdir("/")
102
- @app.instance_eval do
103
- handle_options
104
- options.silent = true
146
+ in_environment("PWD" => "/", "RAKE_SYSTEM" => 'not_exist') do
147
+ @app.instance_eval do
148
+ handle_options
149
+ options.silent = true
150
+ end
151
+ ex = assert_raise(RuntimeError) do
152
+ @app.instance_eval do raw_load_rakefile end
153
+ end
154
+ assert_match(/no rakefile found/i, ex.message)
105
155
  end
106
- ex = assert_raise(RuntimeError) do
107
- @app.instance_eval do raw_load_rakefile end
156
+ end
157
+
158
+ def test_load_from_system_rakefile
159
+ in_environment('RAKE_SYSTEM' => 'test/data/sys') do
160
+ @app.options.rakelib = []
161
+ @app.instance_eval do
162
+ handle_options
163
+ options.silent = true
164
+ options.load_system = true
165
+ load_rakefile
166
+ end
167
+ assert_equal "test/data/sys", @app.system_dir
168
+ assert_nil @app.rakefile
169
+ end
170
+ end
171
+
172
+ def test_load_from_system_rakefile_on_unix
173
+ flexmock(@app, :windows? => false,
174
+ :win32_system_dir => nil,
175
+ :load => nil)
176
+ flexmock(File).should_receive(:expand_path).with("~").and_return("/HOME")
177
+ flexmock(File).should_receive(:expand_path).and_return { |fn| fn }
178
+
179
+ in_environment('RAKE_SYSTEM' => nil) do
180
+ @app.options.rakelib = []
181
+ @app.instance_eval do
182
+ handle_options
183
+ options.silent = true
184
+ options.load_system = true
185
+ load_rakefile
186
+ end
187
+ assert_equal "/HOME/.rake", @app.system_dir
188
+ end
189
+ end
190
+
191
+ def test_windows
192
+ assert ! (@app.windows? && @app.unix?)
193
+ end
194
+
195
+ def test_load_from_system_rakefile_on_windows
196
+ flexmock(@app, :windows? => true,
197
+ :standard_system_dir => "XX")
198
+ flexmock(@app).should_receive(:directory?).with("/AD/Rake").and_return(true)
199
+ flexmock(@app).should_receive(:load).and_return { |fn| puts "LOADING #{fn}" }
200
+ in_environment('RAKE_SYSTEM' => nil, 'APPDATA' => '/AD') do
201
+ @app.options.rakelib = []
202
+ @app.instance_eval do
203
+ handle_options
204
+ options.silent = true
205
+ options.load_system = true
206
+ load_rakefile
207
+ end
208
+ assert_equal "/AD/Rake", @app.system_dir
108
209
  end
109
- assert_match(/no rakefile found/i, ex.message)
110
- ensure
111
- Dir.chdir(original_dir)
112
210
  end
113
211
 
114
- def test_not_caring_about_finding_rakefile
115
- @app.instance_eval do @rakefiles = [''] end
116
- assert(@app.instance_eval do have_rakefile end)
117
- assert_equal '', @app.rakefile
212
+ def test_load_from_system_rakefile_on_windows_with_no_appdata
213
+ flexmock(@app, :windows? => true,
214
+ :standard_system_dir => "XX"
215
+ )
216
+ flexmock(File).should_receive(:exists?).with("/AD/Rake").and_return(false)
217
+ out = capture_stderr do
218
+ assert_raise(SystemExit) do
219
+ in_environment('RAKE_SYSTEM' => nil, 'APPDATA' => "/AD") do
220
+ @app.options.rakelib = []
221
+ @app.instance_eval do
222
+ handle_options
223
+ options.silent = true
224
+ options.load_system = true
225
+ load_rakefile
226
+ end
227
+ end
228
+ end
229
+ end
118
230
  end
119
231
 
120
232
  def test_loading_imports
@@ -147,7 +259,9 @@ class TestApplication < Test::Unit::TestCase
147
259
  @app.instance_eval do
148
260
  intern(Rake::Task, "default").enhance { ran = true }
149
261
  end
150
- @app.run
262
+ in_environment("PWD" => "test/data/default") do
263
+ @app.run
264
+ end
151
265
  assert ran
152
266
  end
153
267
 
@@ -215,7 +329,6 @@ class TestApplication < Test::Unit::TestCase
215
329
  ensure
216
330
  ARGV.clear
217
331
  end
218
-
219
332
  end
220
333
 
221
334
 
@@ -227,6 +340,7 @@ class TestApplicationOptions < Test::Unit::TestCase
227
340
  clear_argv
228
341
  RakeFileUtils.verbose_flag = false
229
342
  RakeFileUtils.nowrite_flag = false
343
+ TESTING_REQUIRE.clear
230
344
  end
231
345
 
232
346
  def teardown
@@ -243,36 +357,23 @@ class TestApplicationOptions < Test::Unit::TestCase
243
357
 
244
358
  def test_default_options
245
359
  opts = command_line
246
- assert_nil opts.show_task_pattern
360
+ assert_nil opts.classic_namespace
247
361
  assert_nil opts.dryrun
248
- assert_nil opts.trace
362
+ assert_nil opts.full_description
363
+ assert_nil opts.ignore_system
364
+ assert_nil opts.load_system
249
365
  assert_nil opts.nosearch
250
- assert_nil opts.silent
366
+ assert_equal ['rakelib'], opts.rakelib
251
367
  assert_nil opts.show_prereqs
368
+ assert_nil opts.show_task_pattern
252
369
  assert_nil opts.show_tasks
253
- assert_nil opts.classic_namespace
370
+ assert_nil opts.silent
371
+ assert_nil opts.trace
254
372
  assert_equal ['rakelib'], opts.rakelib
255
373
  assert ! RakeFileUtils.verbose_flag
256
374
  assert ! RakeFileUtils.nowrite_flag
257
375
  end
258
376
 
259
- def test_bad_options
260
- assert_raise GetoptLong::InvalidOption do
261
- capture_stderr do
262
- flags('--bad', '-t') do |opts|
263
- end
264
- end
265
- end
266
- end
267
-
268
- def test_trace_option
269
- flags('--trace', '-t') do |opts|
270
- assert opts.trace
271
- assert RakeFileUtils.verbose_flag
272
- assert ! RakeFileUtils.nowrite_flag
273
- end
274
- end
275
-
276
377
  def test_dry_run
277
378
  flags('--dry-run', '-n') do |opts|
278
379
  assert opts.dryrun
@@ -282,16 +383,6 @@ class TestApplicationOptions < Test::Unit::TestCase
282
383
  end
283
384
  end
284
385
 
285
- def test_help
286
- flags('--help', '-H', '-h') do |opts|
287
- assert_match(/\Arake/, @out)
288
- assert_match(/\boptions\b/, @out)
289
- assert_match(/\btargets\b/, @out)
290
- assert_equal :exit, @exit
291
- assert_equal :exit, @exit
292
- end
293
- end
294
-
295
386
  def test_describe
296
387
  flags('--describe') do |opts|
297
388
  assert opts.full_description
@@ -300,40 +391,60 @@ class TestApplicationOptions < Test::Unit::TestCase
300
391
  end
301
392
  end
302
393
 
303
- def test_libdir
304
- flags(['--libdir', 'xx'], ['-I', 'xx'], ['-Ixx']) do |opts|
305
- $:.include?('xx')
394
+ def test_describe_with_pattern
395
+ flags('--describe=X') do |opts|
396
+ assert opts.full_description
397
+ assert opts.show_tasks
398
+ assert_equal(/X/.to_s, opts.show_task_pattern.to_s)
306
399
  end
307
- ensure
308
- $:.delete('xx')
309
400
  end
310
401
 
311
- def test_nosearch
312
- flags('--nosearch', '-N') do |opts|
313
- assert opts.nosearch
402
+ def test_execute
403
+ $xyzzy = 0
404
+ flags('--execute=$xyzzy=1', '-e $xyzzy=1') do |opts|
405
+ assert_equal 1, $xyzzy
406
+ assert_equal :exit, @exit
407
+ $xyzzy = 0
314
408
  end
315
409
  end
316
410
 
317
- def test_show_prereqs
318
- flags('--prereqs', '-P') do |opts|
319
- assert opts.show_prereqs
411
+ def test_execute_and_continue
412
+ $xyzzy = 0
413
+ flags('--execute-continue=$xyzzy=1', '-E $xyzzy=1') do |opts|
414
+ assert_equal 1, $xyzzy
415
+ assert_not_equal :exit, @exit
416
+ $xyzzy = 0
320
417
  end
321
418
  end
322
419
 
323
- def test_quiet
324
- flags('--quiet', '-q') do |opts|
325
- assert ! RakeFileUtils.verbose_flag
326
- assert ! opts.silent
420
+ def test_execute_and_print
421
+ $xyzzy = 0
422
+ flags('--execute-print=$xyzzy="pugh"', '-p $xyzzy="pugh"') do |opts|
423
+ assert_equal 'pugh', $xyzzy
424
+ assert_equal :exit, @exit
425
+ assert_match(/^pugh$/, @out)
426
+ $xyzzy = 0
327
427
  end
328
428
  end
329
429
 
330
- def test_silent
331
- flags('--silent', '-s') do |opts|
332
- assert ! RakeFileUtils.verbose_flag
333
- assert opts.silent
430
+ def test_help
431
+ flags('--help', '-H', '-h') do |opts|
432
+ assert_match(/\Arake/, @out)
433
+ assert_match(/\boptions\b/, @out)
434
+ assert_match(/\btargets\b/, @out)
435
+ assert_equal :exit, @exit
436
+ assert_equal :exit, @exit
334
437
  end
335
438
  end
336
439
 
440
+ def test_libdir
441
+ flags(['--libdir', 'xx'], ['-I', 'xx'], ['-Ixx']) do |opts|
442
+ $:.include?('xx')
443
+ end
444
+ ensure
445
+ $:.delete('xx')
446
+ end
447
+
337
448
  def test_rakefile
338
449
  flags(['--rakefile', 'RF'], ['--rakefile=RF'], ['-f', 'RF'], ['-fRF']) do |opts|
339
450
  assert_equal ['RF'], @app.instance_eval { @rakefiles }
@@ -364,6 +475,58 @@ class TestApplicationOptions < Test::Unit::TestCase
364
475
  assert_match(/test\/missing/, ex.message)
365
476
  end
366
477
 
478
+ def test_prereqs
479
+ flags('--prereqs', '-P') do |opts|
480
+ assert opts.show_prereqs
481
+ end
482
+ end
483
+
484
+ def test_quiet
485
+ flags('--quiet', '-q') do |opts|
486
+ assert ! RakeFileUtils.verbose_flag
487
+ assert ! opts.silent
488
+ end
489
+ end
490
+
491
+ def test_no_search
492
+ flags('--nosearch', '--no-search', '-N') do |opts|
493
+ assert opts.nosearch
494
+ end
495
+ end
496
+
497
+ def test_silent
498
+ flags('--silent', '-s') do |opts|
499
+ assert ! RakeFileUtils.verbose_flag
500
+ assert opts.silent
501
+ end
502
+ end
503
+
504
+ def test_system
505
+ flags('--system', '-g') do |opts|
506
+ assert opts.load_system
507
+ end
508
+ end
509
+
510
+ def test_no_system
511
+ flags('--no-system', '-G') do |opts|
512
+ assert opts.ignore_system
513
+ end
514
+ end
515
+
516
+ def test_trace
517
+ flags('--trace', '-t') do |opts|
518
+ assert opts.trace
519
+ assert RakeFileUtils.verbose_flag
520
+ assert ! RakeFileUtils.nowrite_flag
521
+ end
522
+ end
523
+
524
+ def test_trace_rules
525
+ flags('--rules') do |opts|
526
+ assert opts.trace_rules
527
+ end
528
+ end
529
+
367
530
  def test_tasks
368
531
  flags('--tasks', '-T') do |opts|
369
532
  assert opts.show_tasks
@@ -403,13 +566,13 @@ class TestApplicationOptions < Test::Unit::TestCase
403
566
 
404
567
  def test_bad_option
405
568
  capture_stderr do
406
- ex = assert_raise(GetoptLong::InvalidOption) do
569
+ ex = assert_raise(OptionParser::InvalidOption) do
407
570
  flags('--bad-option')
408
571
  end
409
572
  if ex.message =~ /^While/ # Ruby 1.9 error message
410
573
  assert_match(/while parsing/i, ex.message)
411
574
  else # Ruby 1.8 error message
412
- assert_match(/unrecognized option/i, ex.message)
575
+ assert_match(/(invalid|unrecognized) option/i, ex.message)
413
576
  assert_match(/--bad-option/, ex.message)
414
577
  end
415
578
  end
@@ -436,6 +599,7 @@ class TestApplicationOptions < Test::Unit::TestCase
436
599
 
437
600
  def flags(*sets)
438
601
  sets.each do |set|
602
+ ARGV.clear
439
603
  @out = capture_stdout {
440
604
  @exit = catch(:system_exit) { opts = command_line(*set) }
441
605
  }
@@ -450,8 +614,7 @@ class TestApplicationOptions < Test::Unit::TestCase
450
614
  throw :system_exit, :exit
451
615
  end
452
616
  @app.instance_eval do
453
- handle_options
454
- collect_tasks
617
+ collect_tasks handle_options
455
618
  end
456
619
  @tasks = @app.top_level_tasks
457
620
  @app.options
@@ -500,3 +663,52 @@ class TestTaskArgumentParsing < Test::Unit::TestCase
500
663
  end
501
664
 
502
665
  end
666
+
667
+ class TestTaskArgumentParsing < Test::Unit::TestCase
668
+ include InEnvironment
669
+
670
+ def test_terminal_width_using_env
671
+ app = Rake::Application.new
672
+ in_environment('RAKE_COLUMNS' => '1234') do
673
+ assert_equal 1234, app.terminal_width
674
+ end
675
+ end
676
+
677
+ def test_terminal_width_using_stty
678
+ app = Rake::Application.new
679
+ flexmock(app,
680
+ :unix? => true,
681
+ :dynamic_width_stty => 1235,
682
+ :dynamic_width_tput => 0)
683
+ in_environment('RAKE_COLUMNS' => nil) do
684
+ assert_equal 1235, app.terminal_width
685
+ end
686
+ end
687
+
688
+ def test_terminal_width_using_tput
689
+ app = Rake::Application.new
690
+ flexmock(app,
691
+ :unix? => true,
692
+ :dynamic_width_stty => 0,
693
+ :dynamic_width_tput => 1236)
694
+ in_environment('RAKE_COLUMNS' => nil) do
695
+ assert_equal 1236, app.terminal_width
696
+ end
697
+ end
698
+
699
+ def test_terminal_width_using_hardcoded_80
700
+ app = Rake::Application.new
701
+ flexmock(app, :unix? => false)
702
+ in_environment('RAKE_COLUMNS' => nil) do
703
+ assert_equal 80, app.terminal_width
704
+ end
705
+ end
706
+
707
+ def test_terminal_width_with_failure
708
+ app = Rake::Application.new
709
+ flexmock(app).should_receive(:unix?).and_throw(RuntimeError)
710
+ in_environment('RAKE_COLUMNS' => nil) do
711
+ assert_equal 80, app.terminal_width
712
+ end
713
+ end
714
+ end