grosser-autotest 4.0.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.
data/test/helper.rb ADDED
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH << File.join(File.dirname('..'),'lib')
2
+ $TESTING = true
3
+
4
+ require 'stringio'
5
+ require 'rubygems'
6
+ require 'test/unit'
@@ -0,0 +1,454 @@
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_clear_exceptions
78
+ test_add_exception
79
+ @a.clear_exceptions
80
+
81
+ actual = util_exceptions
82
+ expect = []
83
+
84
+ assert_equal expect, actual
85
+ end
86
+
87
+ def test_clear_mapping
88
+ @a.clear_mappings
89
+
90
+ actual = util_mappings
91
+ expect = []
92
+
93
+ assert_equal expect, actual
94
+ end
95
+
96
+ def test_consolidate_failures_experiment
97
+ @files.clear
98
+ @files[@impl] = Time.at(1)
99
+ @files[@test] = Time.at(2)
100
+
101
+ @a.find_order = @files.keys.sort
102
+
103
+ input = [['test_fail1', @test_class], ['test_fail2', @test_class], ['test_error1', @test_class], ['test_error2', @test_class]]
104
+ result = @a.consolidate_failures input
105
+ expected = { @test => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
106
+ assert_equal expected, result
107
+ end
108
+
109
+ def test_consolidate_failures_green
110
+ result = @a.consolidate_failures([])
111
+ expected = {}
112
+ assert_equal expected, result
113
+ end
114
+
115
+ def test_consolidate_failures_multiple_possibilities
116
+ @files[@other_test] = Time.at(42)
117
+ result = @a.consolidate_failures([['test_unmatched', @test_class]])
118
+ expected = { @test => ['test_unmatched']}
119
+ assert_equal expected, result
120
+ expected = ""
121
+ assert_equal expected, @a.output.string
122
+ end
123
+
124
+ def test_consolidate_failures_nested_classes
125
+ @files.clear
126
+ @files['lib/outer.rb'] = Time.at(5)
127
+ @files['lib/outer/inner.rb'] = Time.at(5)
128
+ @files[@inner_test] = Time.at(5)
129
+ @files[@outer_test] = Time.at(5)
130
+
131
+ @a.find_order = @files.keys.sort
132
+
133
+ result = @a.consolidate_failures([['test_blah1', @inner_test_class]])
134
+ expected = { @inner_test => ['test_blah1'] }
135
+ assert_equal expected, result
136
+ expected = ""
137
+ assert_equal expected, @a.output.string
138
+ end
139
+
140
+ def test_consolidate_failures_no_match
141
+ result = @a.consolidate_failures([['test_blah1', @test_class], ['test_blah2', @test_class], ['test_blah1', 'TestUnknown']])
142
+ expected = {@test => ['test_blah1', 'test_blah2']}
143
+ assert_equal expected, result
144
+ expected = "Unable to map class TestUnknown to a file\n"
145
+ assert_equal expected, @a.output.string
146
+ end
147
+
148
+ def test_consolidate_failures_red
149
+ result = @a.consolidate_failures([['test_blah1', @test_class], ['test_blah2', @test_class]])
150
+ expected = {@test => ['test_blah1', 'test_blah2']}
151
+ assert_equal expected, result
152
+ end
153
+
154
+ def test_exceptions
155
+ @a.clear_exceptions
156
+ test_add_exception
157
+ assert_equal(/blah/, @a.exceptions)
158
+ end
159
+
160
+ def test_exceptions_nil
161
+ @a.clear_exceptions
162
+ assert_nil @a.exceptions
163
+ end
164
+
165
+ # TODO: lots of filename edgecases for find_files_to_test
166
+ def test_find_files_to_test
167
+ @a.last_mtime = Time.at(0)
168
+ assert @a.find_files_to_test(@files)
169
+
170
+ @a.last_mtime = @files.values.sort.last + 1
171
+ deny @a.find_files_to_test(@files)
172
+ end
173
+
174
+ def test_find_files_to_test_dunno
175
+ empty = {}
176
+
177
+ files = { "fooby.rb" => Time.at(42) }
178
+ assert @a.find_files_to_test(files) # we find fooby,
179
+ assert_equal empty, @a.files_to_test # but it isn't something to test
180
+ assert_equal "No tests matched fooby.rb\n", @a.output.string
181
+ end
182
+
183
+ def test_find_files_to_test_lib
184
+ # ensure we add test_blah.rb when blah.rb updates
185
+ util_find_files_to_test(@impl, @test => [])
186
+ end
187
+
188
+ def test_find_files_to_test_no_change
189
+ empty = {}
190
+
191
+ # ensure world is virginal
192
+ assert_equal empty, @a.files_to_test
193
+
194
+ # ensure we do nothing when nothing changes...
195
+ files = { @impl => @files[@impl] } # same time
196
+ deny @a.find_files_to_test(files)
197
+ assert_equal empty, @a.files_to_test
198
+ assert_equal "", @a.output.string
199
+
200
+ files = { @impl => @files[@impl] } # same time
201
+ assert(! @a.find_files_to_test(files))
202
+ assert_equal empty, @a.files_to_test
203
+ assert_equal "", @a.output.string
204
+ end
205
+
206
+ def test_find_files_to_test_test
207
+ # ensure we add test_blah.rb when test_blah.rb itself updates
208
+ util_find_files_to_test(@test, @test => [])
209
+ end
210
+
211
+ def test_reorder_alpha
212
+ @a.order = :alpha
213
+ expected = @files.sort
214
+
215
+ assert_equal expected, @a.reorder(@files)
216
+ end
217
+
218
+ def test_reorder_reverse
219
+ @a.order = :reverse
220
+ expected = @files.sort.reverse
221
+
222
+ assert_equal expected, @a.reorder(@files)
223
+ end
224
+
225
+ def test_reorder_random
226
+ @a.order = :random
227
+
228
+ srand 42
229
+ expected, size = @files.dup, @files.size
230
+ expected = expected.sort_by { rand(size) }
231
+
232
+ srand 42
233
+ result = @a.reorder(@files.dup)
234
+
235
+ assert_equal expected, result
236
+ end
237
+
238
+ def test_reorder_natural
239
+ srand 42
240
+
241
+ @files['lib/untested_blah.rb'] = Time.at(2)
242
+ @a.find_order = @files.keys.sort_by { rand }
243
+
244
+ @a.order = :natural
245
+ expected = @a.find_order.map { |f| [f, @files[f]] }
246
+
247
+ assert_equal expected, @a.reorder(@files)
248
+ end
249
+
250
+ def test_handle_results
251
+ @a.files_to_test.clear
252
+ @files.clear
253
+ @files[@impl] = Time.at(1)
254
+ @files[@test] = Time.at(2)
255
+
256
+ @a.find_order = @files.keys.sort
257
+
258
+ empty = {}
259
+ assert_equal empty, @a.files_to_test, "must start empty"
260
+
261
+ s1 = "Loaded suite -e
262
+ Started
263
+ ............
264
+ Finished in 0.001655 seconds.
265
+
266
+ 12 tests, 18 assertions, 0 failures, 0 errors
267
+ "
268
+
269
+ @a.handle_results(s1)
270
+ assert_equal empty, @a.files_to_test, "must stay empty"
271
+
272
+ s2 = "
273
+ 1) Failure:
274
+ test_fail1(#{@test_class}) [#{@test}:59]:
275
+ 2) Failure:
276
+ test_fail2(#{@test_class}) [#{@test}:60]:
277
+ 3) Error:
278
+ test_error1(#{@test_class}):
279
+ 3) Error:
280
+ test_error2(#{@test_class}):
281
+
282
+ 12 tests, 18 assertions, 2 failures, 2 errors
283
+ "
284
+
285
+ @a.handle_results(s2)
286
+ expected = { @test => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
287
+ assert_equal expected, @a.files_to_test
288
+ assert @a.tainted
289
+
290
+ @a.handle_results(s1)
291
+ assert_equal empty, @a.files_to_test
292
+
293
+ s3 = '
294
+ /opt/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit #{@test}].each { |f| require f }" | unit_diff -u
295
+ -e:1:in `require\': ./#{@test}:23: parse error, unexpected tIDENTIFIER, expecting \'}\' (SyntaxError)
296
+ settings_fields.each {|e| assert_equal e, version.send e.intern}
297
+ ^ from -e:1
298
+ from -e:1:in `each\'
299
+ from -e:1
300
+ '
301
+ @a.files_to_test[@test] = Time.at(42)
302
+ @files[@test] = []
303
+ expected = { @test => Time.at(42) }
304
+ assert_equal expected, @a.files_to_test
305
+ @a.handle_results(s3)
306
+ assert_equal expected, @a.files_to_test
307
+ assert @a.tainted
308
+ @a.tainted = false
309
+
310
+ @a.handle_results(s1)
311
+ assert_equal empty, @a.files_to_test
312
+ deny @a.tainted
313
+ end
314
+
315
+ def test_hook_overlap_returning_false
316
+ util_reset_hooks_returning false
317
+
318
+ @a.hook :blah
319
+
320
+ assert @a.instance_variable_get(:@blah1), "Hook1 should work on blah"
321
+ assert @a.instance_variable_get(:@blah2), "Hook2 should work on blah"
322
+ assert @a.instance_variable_get(:@blah3), "Hook3 should work on blah"
323
+ end
324
+
325
+ def test_hook_overlap_returning_true
326
+ util_reset_hooks_returning true
327
+
328
+ @a.hook :blah
329
+
330
+ assert @a.instance_variable_get(:@blah1), "Hook1 should work on blah"
331
+ deny @a.instance_variable_get(:@blah2), "Hook2 should NOT work on blah"
332
+ deny @a.instance_variable_get(:@blah3), "Hook3 should NOT work on blah"
333
+ end
334
+
335
+ def test_hook_response
336
+ Autotest.clear_hooks
337
+ deny @a.hook(:blah)
338
+
339
+ Autotest.add_hook(:blah) { false }
340
+ deny @a.hook(:blah)
341
+
342
+ Autotest.add_hook(:blah) { false }
343
+ deny @a.hook(:blah)
344
+
345
+ Autotest.add_hook(:blah) { true }
346
+ assert @a.hook(:blah)
347
+ end
348
+
349
+ def test_make_test_cmd
350
+ f = {
351
+ @test => [],
352
+ 'test/test_fooby.rb' => [ 'test_something1', 'test_something2' ]
353
+ }
354
+
355
+ expected = [ "#{RUBY} -I.:lib:test -rubygems -e \"%w[test/unit #{@test}].each { |f| require f }\" | unit_diff -u",
356
+ "#{RUBY} -I.:lib:test test/test_fooby.rb -n \"/^(test_something1|test_something2)$/\" | unit_diff -u" ].join("; ")
357
+
358
+ result = @a.make_test_cmd f
359
+ assert_equal expected, result
360
+ end
361
+
362
+ def test_path_to_classname
363
+ # non-rails
364
+ util_path_to_classname 'TestBlah', 'test/test_blah.rb'
365
+ util_path_to_classname 'TestOuter::TestInner', 'test/outer/test_inner.rb'
366
+ util_path_to_classname 'TestRuby2Ruby', 'test/test_ruby2ruby.rb'
367
+ end
368
+
369
+ def test_remove_exception
370
+ test_add_exception
371
+ current = util_exceptions
372
+ @a.remove_exception 'blah'
373
+
374
+ actual = util_exceptions
375
+ expect = current - ["blah"]
376
+
377
+ assert_equal expect, actual
378
+ end
379
+
380
+ def test_remove_mapping
381
+ current = util_mappings
382
+ @a.remove_mapping(/^lib\/.*\.rb$/)
383
+
384
+ actual = util_mappings
385
+ expect = current - [/^lib\/.*\.rb$/]
386
+
387
+ assert_equal expect, actual
388
+ end
389
+
390
+ def test_test_files_for
391
+ assert_equal [@test], @a.test_files_for(@impl)
392
+ assert_equal [@test], @a.test_files_for(@test)
393
+
394
+ assert_equal [], @a.test_files_for('test/test_unknown.rb')
395
+ assert_equal [], @a.test_files_for('lib/unknown.rb')
396
+ assert_equal [], @a.test_files_for('unknown.rb')
397
+ assert_equal [], @a.test_files_for('test_unknown.rb')
398
+ end
399
+
400
+ def test_testlib
401
+ assert_equal "test/unit", @a.testlib
402
+
403
+ @a.testlib = "MONKEY"
404
+ assert_equal "MONKEY", @a.testlib
405
+
406
+ f = { @test => [], "test/test_fooby.rb" => %w(first second) }
407
+ assert_match @a.testlib, @a.make_test_cmd(f)
408
+ end
409
+
410
+ def util_exceptions
411
+ @a.exception_list.sort_by { |r| r.to_s }
412
+ end
413
+
414
+ def util_find_files_to_test(f, expected)
415
+ t = @a.last_mtime
416
+ files = { f => t + 1 }
417
+
418
+ assert @a.find_files_to_test(files)
419
+ assert_equal expected, @a.files_to_test
420
+ assert_equal t, @a.last_mtime
421
+ assert_equal "", @a.output.string
422
+ end
423
+
424
+ def util_mappings
425
+ @a.test_mappings.map { |k,v| k }.sort_by { |x| x.to_s }
426
+ end
427
+
428
+ def util_path_to_classname(e,i)
429
+ assert_equal e, @a.path_to_classname(i)
430
+ end
431
+
432
+ def util_reset_hooks_returning val
433
+ Autotest.clear_hooks
434
+
435
+ @a.instance_variable_set :@blah1, false
436
+ @a.instance_variable_set :@blah2, false
437
+ @a.instance_variable_set :@blah3, false
438
+
439
+ Autotest.add_hook(:blah) do |at|
440
+ at.instance_variable_set :@blah1, true
441
+ val
442
+ end
443
+
444
+ Autotest.add_hook(:blah) do |at|
445
+ at.instance_variable_set :@blah2, true
446
+ val
447
+ end
448
+
449
+ Autotest.add_hook(:blah) do |at|
450
+ at.instance_variable_set :@blah3, true
451
+ val
452
+ end
453
+ end
454
+ end