Skip to content

Commit

Permalink
Added autotest rake rule.
Browse files Browse the repository at this point in the history
Added to / clarified several unit tests.
I think the first pass of autotest.rb is done. On to rails!

[git-p4: depot-paths = "//src/ZenTest/dev/": change = 2595]
  • Loading branch information
Ryan Davis committed Jul 19, 2006
1 parent 7298eb9 commit b6dfd37
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 80 deletions.
5 changes: 2 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ $lib = Config::CONFIG['sitelibdir']
$bins = spec.executables
$libs = spec.files.grep(/^lib\//).map { |f| f.sub(/^lib\//, '') }.sort

task :blah do
p $bins
p $libs
task :autotest do
ruby "-Ilib ./bin/autotest"
end

task :install do
Expand Down
93 changes: 38 additions & 55 deletions lib/autotest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.run
new.run
end

attr_accessor :files, :files_to_test, :output if $TESTING
attr_accessor :files, :files_to_test, :output, :last_mtime if $TESTING

def initialize
@files = Hash.new Time.at(0)
Expand All @@ -40,6 +40,8 @@ def run
loop do # ^c handler
begin
get_to_green
rerun_all_tests if @tainted
wait_for_changes
rescue Interrupt
if @wants_to_quit then
break
Expand All @@ -50,49 +52,24 @@ def run
end
end

def get_to_green # TODO: think about inlining this
log_method
loop do
log 'status', 'running all tests'
reset
run_tests # run all tests each full pass
@last_mtime = @files.values.sort.last
run_tests until all_good
def get_to_green
until all_good do # TODO: all_good
run_tests
wait_for_changes unless all_good
end
end

def run_tests # TODO: and possibly rename this get_to_green
def run_tests
log_method
update_files_to_test # failed + changed/affected

cmd = make_test_cmd @files_to_test

log 'data', "@files_to_test = #{@files_to_test.inspect}"
log 'status', 'Testing updated files'
log 'cmd', cmd
cmd = make_test_cmd(@files_to_test)
results = `#{cmd}`

puts results

handle_results(results)
end

def all_good
log_method
unless @files_to_test.empty? then
newest = nil
loop do
update_files_to_test
newest = @files.values.sort.last
break if newest > @last_mtime
#log 'status', "waiting because of #{newest} > #{@last_mtime} in #{@files_to_test.inspect}"
sleep @sleep # TODO unless testing ?
end
@last_mtime = newest
end
@files_to_test.empty?
end

############################################################
# Utility Methods, not essential to reading of logic

Expand Down Expand Up @@ -123,6 +100,10 @@ def add_sigint_handler
end
end

def all_good
@files_to_test.empty?
end

def consolidate_failures(failed)
log_method
filters = Hash.new { |h,k| h[k] = [] }
Expand Down Expand Up @@ -161,28 +142,15 @@ def find_files
end

def handle_results(results)
log_method

# TODO: get rid of this
if results =~ / 0 failures, 0 errors\Z/ then
log 'status', 'Passed'
return
end

failed = results.scan(/^\s+\d+\) (?:Failure|Error):\n(.*?)\((.*?)\)/)

# TODO: get rid of this
if failed.empty? then
log 'status', 'tests exited without a parseable failure or error message.'
log 'status', 'check for a syntax error in your code, or something...'
return
end

log 'data', "@files = #{@files.inspect}"
log 'data', "failed = #{failed.inspect}"
log 'data', "old @files_to_test = #{@files_to_test.inspect}"
@files_to_test = consolidate_failures failed
log 'data', "new @files_to_test = #{@files_to_test.inspect}"
@tainted = true unless @files_to_test.empty?
end

def has_new_files?
previous = @last_mtime
@last_mtime = @files.values.sort.last
@last_mtime > previous
end

def make_test_cmd files_to_test
Expand All @@ -195,7 +163,7 @@ def make_test_cmd files_to_test
end

partial.each do |klass, methods|
cmds << "#{ruby} -I#{@libs} #{klass} -n \"#{Regexp.union(*methods).inspect}\" | unit_diff -u"
cmds << "#{ruby} -I#{@libs} #{klass} -n \"/^(#{Regexp.union(*methods).source})$/\" | unit_diff -u"
end

return cmds.join('; ')
Expand All @@ -208,6 +176,8 @@ def reset
@files_to_test.clear
@last_mtime = Time.at(0)
update_files_to_test # failed + changed/affected
@last_mtime = @files.values.sort.last # FIX
@tainted = false
end

def ruby
Expand All @@ -221,7 +191,12 @@ def ruby
return ruby
end

def update_files_to_test(files=find_files)
def rerun_all_tests
reset
run_tests
end

def update_files_to_test(files=find_files) # TODO: give better name
updated = []

files.each do |filename, mtime|
Expand All @@ -241,10 +216,18 @@ def update_files_to_test(files=find_files)
when %r%^(doc|pkg)/% then
# ignore
else
@output.puts "Dunno! #{filename}" if $v or $TESTING
@output.puts "Dunno! #{filename}" if $TESTING
end
@files[filename] = mtime
end
end
end

def wait_for_changes
log_method
begin
sleep @sleep
update_files_to_test
end until has_new_files?
end
end
62 changes: 40 additions & 22 deletions test/test_autotest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
class TestAutotest < Test::Unit::TestCase
def setup
@a = Autotest.new
@a.files = {
'lib/blah.rb' => 1,
'test/test_blah.rb' => 2,
}
@a.files.default = 0
@a.files.clear
@a.files['lib/blah.rb'] = Time.at(1)
@a.files['test/test_blah.rb'] = Time.at(2)

@a.output = StringIO.new
end

def test_consolidate_failures_experiment
@a.files = {
'lib/autotest.rb' => 1,
'test/test_autotest.rb' => 2,
}
@a.files.default = 0
@a.files.clear
@a.files['lib/autotest.rb'] = Time.at(1)
@a.files['test/test_autotest.rb'] = Time.at(2)

input = [["test_fail1", "TestAutotest"], ["test_fail2", "TestAutotest"], ["test_error1", "TestAutotest"], ["test_error2", "TestAutotest"]]
result = @a.consolidate_failures input
expected = { "test/test_autotest.rb" => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
Expand Down Expand Up @@ -68,26 +66,43 @@ def test_consolidate_failures_red
assert_equal expected, result
end

def test_flunk
# flunk
end

def test_has_new_files_eh
@a.files_to_test.clear
@a.files.clear
@a.files['lib/autotest.rb'] = Time.at(1)
@a.files['test/test_autotest.rb'] = Time.at(2)
@a.last_mtime = Time.at(0)

assert @a.has_new_files?

@a.last_mtime = Time.at(3)
assert ! @a.has_new_files?
end

def test_handle_results
@a.files = {
'lib/autotest.rb' => 1,
'test/test_autotest.rb' => 2,
}
@a.files.default = 0
@a.files_to_test.clear
@a.files.clear
@a.files['lib/autotest.rb'] = Time.at(1)
@a.files['test/test_autotest.rb'] = Time.at(2)
empty = {}
assert_equal empty, @a.files_to_test, "must start empty"

s = "Loaded suite -e
s1 = "Loaded suite -e
Started
............
Finished in 0.001655 seconds.
12 tests, 18 assertions, 0 failures, 0 errors
"

@a.handle_results(s)
empty = {}
assert_equal empty, @a.files_to_test
@a.handle_results(s1)
assert_equal empty, @a.files_to_test, "must stay empty"

s = "
s2 = "
1) Failure:
test_fail1(TestAutotest) [./test/test_autotest.rb:59]:
2) Failure:
Expand All @@ -98,9 +113,12 @@ def test_handle_results
test_error2(TestAutotest):
"

@a.handle_results(s)
@a.handle_results(s2)
expected = { "test/test_autotest.rb" => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
assert_equal expected, @a.files_to_test

@a.handle_results(s1)
assert_equal empty, @a.files_to_test
end

def test_make_test_cmd
Expand All @@ -118,7 +136,7 @@ def test_make_test_cmd
def test_update_files_to_test_dunno
empty = {}

files = { "fooby.rb" => 42 }
files = { "fooby.rb" => Time.at(42) }
@a.update_files_to_test files
result = @a.files_to_test
assert_equal empty, result
Expand Down

0 comments on commit b6dfd37

Please sign in to comment.