shanel-autotest 4.2.3

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