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
zenspider 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
Expand Up @@ -87,9 +87,8 @@ $lib = Config::CONFIG['sitelibdir']
$bins = spec.executables $bins = spec.executables
$libs = spec.files.grep(/^lib\//).map { |f| f.sub(/^lib\//, '') }.sort $libs = spec.files.grep(/^lib\//).map { |f| f.sub(/^lib\//, '') }.sort


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


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


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


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


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


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

cmd = make_test_cmd(@files_to_test)
cmd = make_test_cmd @files_to_test

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


puts results puts results


handle_results(results) handle_results(results)
end 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 # Utility Methods, not essential to reading of logic


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


def all_good
@files_to_test.empty?
end

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


def handle_results(results) 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(.*?)\((.*?)\)/) 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 @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 end


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


partial.each do |klass, methods| 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 end


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


def ruby def ruby
Expand All @@ -221,7 +191,12 @@ def ruby
return ruby return ruby
end 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 = [] updated = []


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

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


def test_consolidate_failures_experiment def test_consolidate_failures_experiment
@a.files = { @a.files.clear
'lib/autotest.rb' => 1, @a.files['lib/autotest.rb'] = Time.at(1)
'test/test_autotest.rb' => 2, @a.files['test/test_autotest.rb'] = Time.at(2)
}
@a.files.default = 0
input = [["test_fail1", "TestAutotest"], ["test_fail2", "TestAutotest"], ["test_error1", "TestAutotest"], ["test_error2", "TestAutotest"]] input = [["test_fail1", "TestAutotest"], ["test_fail2", "TestAutotest"], ["test_error1", "TestAutotest"], ["test_error2", "TestAutotest"]]
result = @a.consolidate_failures input result = @a.consolidate_failures input
expected = { "test/test_autotest.rb" => %w( test_fail1 test_fail2 test_error1 test_error2 ) } 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 assert_equal expected, result
end 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 def test_handle_results
@a.files = { @a.files_to_test.clear
'lib/autotest.rb' => 1, @a.files.clear
'test/test_autotest.rb' => 2, @a.files['lib/autotest.rb'] = Time.at(1)
} @a.files['test/test_autotest.rb'] = Time.at(2)
@a.files.default = 0 empty = {}
assert_equal empty, @a.files_to_test, "must start empty"


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


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


s = " s2 = "
1) Failure: 1) Failure:
test_fail1(TestAutotest) [./test/test_autotest.rb:59]: test_fail1(TestAutotest) [./test/test_autotest.rb:59]:
2) Failure: 2) Failure:
Expand All @@ -98,9 +113,12 @@ def test_handle_results
test_error2(TestAutotest): 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 ) } expected = { "test/test_autotest.rb" => %w( test_fail1 test_fail2 test_error1 test_error2 ) }
assert_equal expected, @a.files_to_test assert_equal expected, @a.files_to_test

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


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


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

0 comments on commit b6dfd37

Please sign in to comment.