ZenTest 4.11.2 → 4.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,560 +0,0 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
- require 'rubygems'
4
- require 'minitest/autorun'
5
-
6
- require 'stringio'
7
- require 'autotest'
8
-
9
- # NOT TESTED:
10
- # class_run
11
- # add_sigint_handler
12
- # all_good
13
- # get_to_green
14
- # reset
15
- # ruby
16
- # run
17
- # run_tests
18
-
19
- class Autotest
20
- attr_reader :exception_list
21
-
22
- def self.clear_hooks
23
- HOOKS.clear
24
- end
25
-
26
- def self.reset_options
27
- @@options = {}
28
- end
29
- end
30
-
31
- class TestAutotest < Minitest::Test
32
-
33
- alias :deny :refute
34
-
35
- RUBY = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
36
-
37
- def setup
38
- @test_class = 'TestBlah'
39
- @test = 'test/test_blah.rb'
40
- @other_test = 'test/test_blah_other.rb'
41
- @impl = 'lib/blah.rb'
42
- @inner_test = 'test/outer/test_inner.rb'
43
- @outer_test = 'test/test_outer.rb'
44
- @inner_test_class = "TestOuter::TestInner"
45
-
46
- klassname = self.class.name.sub(/^Test/, '')
47
- klassname.sub!(/^(\w+)(Autotest)$/, '\2::\1') unless klassname == "Autotest"
48
- @a = klassname.split(/::/).inject(Object) { |k,n| k.const_get(n) }.new
49
- @a.output = StringIO.new
50
- @a.last_mtime = Time.at(2)
51
-
52
- @files = {}
53
- @files[@impl] = Time.at(1)
54
- @files[@test] = Time.at(2)
55
-
56
- @a.find_order = @files.keys.sort
57
- end
58
-
59
- def test_add_exception
60
- current = util_exceptions
61
- @a.add_exception 'blah'
62
-
63
- actual = util_exceptions
64
- expect = current + ["blah"]
65
-
66
- assert_equal expect, actual
67
- end
68
-
69
- def test_add_mapping
70
- current = util_mappings
71
- @a.add_mapping(/blah/) do 42 end
72
-
73
- actual = util_mappings
74
- expect = current + [/blah/]
75
-
76
- assert_equal expect, actual
77
- end
78
-
79
- def test_add_mapping_front
80
- current = util_mappings
81
- @a.add_mapping(/blah/, :front) do 42 end
82
-
83
- actual = util_mappings
84
- expect = [/blah/] + current
85
-
86
- assert_equal expect, actual
87
- end
88
-
89
- def test_clear_exceptions
90
- test_add_exception
91
- @a.clear_exceptions
92
-
93
- actual = util_exceptions
94
- expect = []
95
-
96
- assert_equal expect, actual
97
- end
98
-
99
- def test_clear_mapping
100
- @a.clear_mappings
101
-
102
- actual = util_mappings
103
- expect = []
104
-
105
- assert_equal expect, actual
106
- end
107
-
108
- def test_consolidate_failures_experiment
109
- @files.clear
110
- @files[@impl] = Time.at(1)
111
- @files[@test] = Time.at(2)
112
-
113
- @a.find_order = @files.keys.sort
114
-
115
- input = [['test_fail1', @test_class],
116
- ['test_fail2', @test_class],
117
- ['test_error1', @test_class],
118
- ['test_error2', @test_class]]
119
- result = @a.consolidate_failures input
120
- expected = { @test => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
121
- assert_equal expected, result
122
- end
123
-
124
- def test_consolidate_failures_green
125
- result = @a.consolidate_failures([])
126
- expected = {}
127
- assert_equal expected, result
128
- end
129
-
130
- def test_consolidate_failures_multiple_possibilities
131
- @files[@other_test] = Time.at(42)
132
- result = @a.consolidate_failures([['test_unmatched', @test_class]])
133
- expected = { @test => ['test_unmatched']}
134
- assert_equal expected, result
135
- expected = ""
136
- assert_equal expected, @a.output.string
137
- end
138
-
139
- def test_consolidate_failures_nested_classes
140
- @files.clear
141
- @files['lib/outer.rb'] = Time.at(5)
142
- @files['lib/outer/inner.rb'] = Time.at(5)
143
- @files[@inner_test] = Time.at(5)
144
- @files[@outer_test] = Time.at(5)
145
-
146
- @a.find_order = @files.keys.sort
147
-
148
- result = @a.consolidate_failures([['test_blah1', @inner_test_class]])
149
- expected = { @inner_test => ['test_blah1'] }
150
- assert_equal expected, result
151
- expected = ""
152
- assert_equal expected, @a.output.string
153
- end
154
-
155
- def test_consolidate_failures_no_match
156
- result = @a.consolidate_failures([['test_blah1', @test_class], ['test_blah2', @test_class], ['test_blah1', 'TestUnknown']])
157
- expected = {@test => ['test_blah1', 'test_blah2']}
158
- assert_equal expected, result
159
- expected = "Unable to map class TestUnknown to a file\n"
160
- assert_equal expected, @a.output.string
161
- end
162
-
163
- def test_consolidate_failures_red
164
- result = @a.consolidate_failures([['test_blah1', @test_class], ['test_blah2', @test_class]])
165
- expected = {@test => ['test_blah1', 'test_blah2']}
166
- assert_equal expected, result
167
- end
168
-
169
- def test_exceptions
170
- @a.clear_exceptions
171
- test_add_exception
172
- assert_equal(/blah/, @a.exceptions)
173
- end
174
-
175
- def test_exceptions_nil
176
- @a.clear_exceptions
177
- assert_nil @a.exceptions
178
- end
179
-
180
- # TODO: lots of filename edgecases for find_files_to_test
181
- def test_find_files_to_test
182
- @a.last_mtime = Time.at(0)
183
- assert @a.find_files_to_test(@files)
184
-
185
- @a.last_mtime = @files.values.sort.last + 1
186
- deny @a.find_files_to_test(@files)
187
- end
188
-
189
- def test_find_files_to_test_dunno
190
- empty = {}
191
-
192
- files = { "fooby.rb" => Time.at(42) }
193
- @a.options[:verbose] = true
194
-
195
- capture_io do
196
- assert @a.find_files_to_test(files) # we find fooby,
197
- assert_equal empty, @a.files_to_test # but it isn't something to test
198
- end
199
- assert_equal "No tests matched fooby.rb\n", @a.output.string
200
- end
201
-
202
- def test_find_files_to_test_lib
203
- # ensure we add test_blah.rb when blah.rb updates
204
- util_find_files_to_test(@impl, @test => [])
205
- end
206
-
207
- def test_find_files_to_test_no_change
208
- empty = {}
209
-
210
- # ensure world is virginal
211
- assert_equal empty, @a.files_to_test
212
-
213
- # ensure we do nothing when nothing changes...
214
- files = { @impl => @files[@impl] } # same time
215
- deny @a.find_files_to_test(files)
216
- assert_equal empty, @a.files_to_test
217
- assert_equal "", @a.output.string
218
-
219
- files = { @impl => @files[@impl] } # same time
220
- assert(! @a.find_files_to_test(files))
221
- assert_equal empty, @a.files_to_test
222
- assert_equal "", @a.output.string
223
- end
224
-
225
- def test_find_files_to_test_test
226
- # ensure we add test_blah.rb when test_blah.rb itself updates
227
- util_find_files_to_test(@test, @test => [])
228
- end
229
-
230
- def test_reorder_alpha
231
- @a.order = :alpha
232
- expected = @files.sort
233
-
234
- assert_equal expected, @a.reorder(@files)
235
- end
236
-
237
- def test_reorder_reverse
238
- @a.order = :reverse
239
- expected = @files.sort.reverse
240
-
241
- assert_equal expected, @a.reorder(@files)
242
- end
243
-
244
- def test_reorder_random
245
- @a.order = :random
246
-
247
- srand 42
248
- expected, size = @files.dup, @files.size
249
- expected = expected.sort_by { rand(size) }
250
-
251
- srand 42
252
- result = @a.reorder(@files.dup)
253
-
254
- assert_equal expected, result
255
- end
256
-
257
- def test_reorder_natural
258
- srand 42
259
-
260
- @files['lib/untested_blah.rb'] = Time.at(2)
261
- @a.find_order = @files.keys.sort_by { rand }
262
-
263
- @a.order = :natural
264
- expected = @a.find_order.map { |f| [f, @files[f]] }
265
-
266
- assert_equal expected, @a.reorder(@files)
267
- end
268
-
269
- def test_handle_results
270
- skip "this is minitest 4 output... needs to work in both"
271
-
272
- @a.files_to_test.clear
273
- @files.clear
274
- @files[@impl] = Time.at(1)
275
- @files[@test] = Time.at(2)
276
-
277
- @a.find_order = @files.keys.sort
278
-
279
- empty = {}
280
- assert_equal empty, @a.files_to_test, "must start empty"
281
-
282
- s1 = "Loaded suite -e
283
- Started
284
- ............
285
- Finished in 0.001655 seconds.
286
-
287
- 12 tests, 18 assertions, 0 failures, 0 errors
288
- "
289
-
290
- @a.handle_results(s1)
291
- assert_equal empty, @a.files_to_test, "must stay empty"
292
-
293
- exp = { "tests" => 12, "assertions" => 18, "failures" => 0, "errors" => 0 }
294
- assert_equal exp, @a.latest_results
295
-
296
- # when colours are in the error message
297
- color_error = "
298
- 1) \e[31mFailure:\e[0m
299
- test_fail1(#{@test_class}) [#{@test}:59]:
300
- 2) \e[31mFailure:\e[0m
301
- test_fail2(#{@test_class}) [#{@test}:60]:
302
- 3) \e[31mError:\e[0m
303
- test_error1(#{@test_class}):
304
- 3) \e[31mError:\e[0m
305
- test_error2(#{@test_class}):
306
-
307
- 12 tests, 18 assertions, 2 failures, 2 errors
308
- "
309
- @a.handle_results(color_error)
310
- assert @a.tainted
311
-
312
-
313
- s2 = "
314
- 1) Failure:
315
- test_fail1(#{@test_class}) [#{@test}:59]:
316
- 2) Failure:
317
- test_fail2(#{@test_class}) [#{@test}:60]:
318
- 3) Error:
319
- test_error1(#{@test_class}):
320
- 3) Error:
321
- test_error2(#{@test_class}):
322
-
323
- 12 tests, 18 assertions, 2 failures, 2 errors
324
- "
325
-
326
- @a.handle_results(s2)
327
- expected = { @test => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
328
- assert_equal expected, @a.files_to_test
329
- assert @a.tainted
330
- exp = { "tests" => 12, "assertions" => 18, "failures" => 2, "errors" => 2 }
331
- assert_equal exp, @a.latest_results
332
-
333
- @a.handle_results(s1)
334
- assert_equal empty, @a.files_to_test
335
-
336
- s3 = '
337
- /opt/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit #{@test}].each { |f| require f }" | unit_diff -u
338
- -e:1:in `require\': ./#{@test}:23: parse error, unexpected tIDENTIFIER, expecting \'}\' (SyntaxError)
339
- settings_fields.each {|e| assert_equal e, version.send e.intern}
340
- ^ from -e:1
341
- from -e:1:in `each\'
342
- from -e:1
343
- '
344
- @a.files_to_test[@test] = Time.at(42)
345
- @files[@test] = []
346
- expected = { @test => Time.at(42) }
347
- assert_equal expected, @a.files_to_test
348
- @a.handle_results(s3)
349
- assert_equal expected, @a.files_to_test
350
- assert @a.tainted
351
- @a.tainted = false
352
-
353
- @a.handle_results(s1)
354
- assert_equal empty, @a.files_to_test
355
- deny @a.tainted
356
- end
357
-
358
- def test_handle_results_minitest5
359
- @a.files_to_test.clear
360
- @files.clear
361
- @files[@impl] = Time.at(1)
362
- @files[@test] = Time.at(2)
363
-
364
- @a.find_order = @files.keys.sort
365
-
366
- empty = {}
367
- assert_equal empty, @a.files_to_test, "must start empty"
368
-
369
- s2 = "
370
- 1) Error:
371
- #{@test_class}#test_error1:
372
- 1) Failure:
373
- #{@test_class}#test_fail1 [./test/test_autotest.rb:375]:
374
-
375
- 12 tests, 18 assertions, 1 failures, 1 errors
376
- "
377
-
378
- @a.handle_results(s2)
379
- expected = { @test => %w( test_error1 test_fail1 ) }
380
- assert_equal expected, @a.files_to_test
381
- assert @a.tainted
382
- exp = { "tests" => 12, "assertions" => 18, "failures" => 1, "errors" => 1 }
383
- assert_equal exp, @a.latest_results
384
- end
385
-
386
- def test_hook_overlap_returning_false
387
- util_reset_hooks_returning false
388
-
389
- @a.hook :blah
390
-
391
- assert @a.instance_variable_get(:@blah1), "Hook1 should work on blah"
392
- assert @a.instance_variable_get(:@blah2), "Hook2 should work on blah"
393
- assert @a.instance_variable_get(:@blah3), "Hook3 should work on blah"
394
- end
395
-
396
- def test_hook_overlap_returning_true
397
- util_reset_hooks_returning true
398
-
399
- @a.hook :blah
400
-
401
- assert @a.instance_variable_get(:@blah1), "Hook1 should work on blah"
402
- deny @a.instance_variable_get(:@blah2), "Hook2 should NOT work on blah"
403
- deny @a.instance_variable_get(:@blah3), "Hook3 should NOT work on blah"
404
- end
405
-
406
- def test_hook_response
407
- Autotest.clear_hooks
408
- deny @a.hook(:blah)
409
-
410
- Autotest.add_hook(:blah) { false }
411
- deny @a.hook(:blah)
412
-
413
- Autotest.add_hook(:blah) { false }
414
- deny @a.hook(:blah)
415
-
416
- Autotest.add_hook(:blah) { true }
417
- assert @a.hook(:blah)
418
- end
419
-
420
- def test_make_test_cmd
421
- f = {
422
- @test => [],
423
- 'test/test_fooby.rb' => [ 'test_something1', 'test_something2' ]
424
- }
425
-
426
- pre = "#{RUBY} -I.:lib:test -rubygems"
427
- req = ".each { |f| require f }\""
428
-
429
- expected =
430
- [ "#{pre} -e \"gem 'minitest'",
431
- "%w[minitest/autorun #{@test}]#{req}",
432
- "#{pre} test/test_fooby.rb -n \"/^(test_something1|test_something2)$/\""
433
- ].join("; ")
434
-
435
- result = @a.make_test_cmd f
436
- assert_equal expected.gsub(/ /, "\n"), result.gsub(/ /, "\n")
437
- end
438
-
439
- def test_make_test_cmd_unit_diff
440
- @a.unit_diff = "unit_diff -u"
441
- f = {
442
- @test => [],
443
- 'test/test_fooby.rb' => [ 'test_something1', 'test_something2' ]
444
- }
445
-
446
- pre = "#{RUBY} -I.:lib:test -rubygems"
447
- req = ".each { |f| require f }\""
448
- post = "| unit_diff -u"
449
-
450
- expected = [ "#{pre} -e \"gem 'minitest'",
451
- "%w[minitest/autorun #{@test}]#{req} #{post}",
452
- "#{pre} test/test_fooby.rb -n \"/^(test_something1|test_something2)$/\" #{post}" ].join("; ")
453
-
454
- result = @a.make_test_cmd f
455
- assert_equal expected, result
456
- end
457
-
458
- def test_path_to_classname
459
- # non-rails
460
- util_path_to_classname 'TestBlah', 'test/test_blah.rb'
461
- util_path_to_classname 'TestOuter::TestInner', 'test/outer/test_inner.rb'
462
- util_path_to_classname 'TestRuby2Ruby', 'test/test_ruby2ruby.rb'
463
- end
464
-
465
- def test_remove_exception
466
- test_add_exception
467
- current = util_exceptions
468
- @a.remove_exception 'blah'
469
-
470
- actual = util_exceptions
471
- expect = current - ["blah"]
472
-
473
- assert_equal expect, actual
474
- end
475
-
476
- def test_remove_mapping
477
- current = util_mappings
478
- @a.remove_mapping(/^lib\/.*\.rb$/)
479
-
480
- actual = util_mappings
481
- expect = current - [/^lib\/.*\.rb$/]
482
-
483
- assert_equal expect, actual
484
- end
485
-
486
- def test_test_files_for
487
- assert_equal [@test], @a.test_files_for(@impl)
488
- assert_equal [@test], @a.test_files_for(@test)
489
-
490
- assert_equal [], @a.test_files_for('test/test_unknown.rb')
491
- assert_equal [], @a.test_files_for('lib/unknown.rb')
492
- assert_equal [], @a.test_files_for('unknown.rb')
493
- assert_equal [], @a.test_files_for('test_unknown.rb')
494
- end
495
-
496
- def test_testlib
497
- assert_equal "minitest/autorun", @a.testlib
498
-
499
- @a.testlib = "MONKEY"
500
- assert_equal "MONKEY", @a.testlib
501
-
502
- f = { @test => [], "test/test_fooby.rb" => %w(first second) }
503
- assert_match @a.testlib, @a.make_test_cmd(f)
504
- end
505
-
506
- def test_runner_accepts_rc_options
507
- exp = {:rc=>["autotest_rc"], :args=>["--rc", "autotest_rc"]}
508
- assert_equal exp, Autotest.parse_options(['--rc', 'autotest_rc'])
509
- assert_kind_of Autotest, Autotest.new
510
- ensure
511
- Autotest.reset_options
512
- end
513
-
514
- def util_exceptions
515
- @a.exception_list.sort_by { |r| r.to_s }
516
- end
517
-
518
- def util_find_files_to_test(f, expected)
519
- t = @a.last_mtime
520
- files = { f => t + 1 }
521
-
522
- capture_io do
523
- assert @a.find_files_to_test(files)
524
- assert_equal expected, @a.files_to_test
525
- end
526
- assert_equal t, @a.last_mtime
527
- assert_equal "", @a.output.string
528
- end
529
-
530
- def util_mappings
531
- @a.test_mappings.map { |k,v| k }
532
- end
533
-
534
- def util_path_to_classname(e,i)
535
- assert_equal e, @a.path_to_classname(i)
536
- end
537
-
538
- def util_reset_hooks_returning val
539
- Autotest.clear_hooks
540
-
541
- @a.instance_variable_set :@blah1, false
542
- @a.instance_variable_set :@blah2, false
543
- @a.instance_variable_set :@blah3, false
544
-
545
- Autotest.add_hook(:blah) do |at|
546
- at.instance_variable_set :@blah1, true
547
- val
548
- end
549
-
550
- Autotest.add_hook(:blah) do |at|
551
- at.instance_variable_set :@blah2, true
552
- val
553
- end
554
-
555
- Autotest.add_hook(:blah) do |at|
556
- at.instance_variable_set :@blah3, true
557
- val
558
- end
559
- end
560
- end