Skip to content

Commit

Permalink
More steps to running only failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Stephens committed Oct 1, 2009
1 parent dd0c8c0 commit 916ff82
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 20 deletions.
67 changes: 52 additions & 15 deletions lib/autowatchr.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'erb'

class Autowatchr
class Config
attr_writer :ruby, :include, :lib_dir, :test_dir, :lib_re, :test_re,
:failed_results_re, :completed_re
attr_writer :command, :ruby, :include, :lib_dir, :test_dir, :lib_re,
:test_re, :failed_results_re, :completed_re

def initialize(options = {})
options.each_pair do |key, value|
Expand All @@ -12,6 +14,10 @@ def initialize(options = {})
end
end

def command
@command ||= "<%= ruby %> -I<%= include %> <%= predicate %>"
end

def ruby
@ruby ||= "ruby"
end
Expand Down Expand Up @@ -43,6 +49,10 @@ def failed_results_re
def completed_re
@completed_re ||= /\d+ tests, \d+ assertions, \d+ failures, \d+ errors/
end

def eval_command(predicate)
ERB.new(self.command).result(binding)
end
end

attr_reader :config
Expand All @@ -52,6 +62,7 @@ def initialize(script, options = {})
yield @config if block_given?
@script = script
@test_files = []
@failed_tests = {}

discover_files
run_all_tests
Expand All @@ -68,13 +79,29 @@ def run_lib_file(file)

def run_test_file(files)
files = [files] unless files.is_a?(Array)
file_str = if files.length > 1
"-e \"%w[#{files.join(" ")}].each { |f| require f }\""
else
files[0]
end

cmd = "%s -I%s %s" % [ @config.ruby, @config.include, file_str ]
passing = []
commands = []
files.each do |file|
tests = @failed_tests[file]
if tests && !tests.empty?
predicate = %!#{file} -n "/^(#{tests.join("|")})$/"!
commands << @config.eval_command(predicate)
else
passing << file
end
end

if !passing.empty?
predicate = if passing.length > 1
"-e \"%w[#{passing.join(" ")}].each { |f| require f }\""
else
passing[0]
end
commands.unshift(@config.eval_command(predicate))
end

cmd = commands.join("; ")
puts cmd

# straight outta autotest
Expand All @@ -98,6 +125,10 @@ def run_test_file(files)
handle_results(results.join)
end

def classname_to_path(s)
File.join(@config.test_dir, underscore(s)+".rb")
end

private
def discover_files
@test_files = Dir.glob("#{@config.test_dir}/**/*").grep(/#{@config.test_re}/)
Expand All @@ -114,15 +145,21 @@ def start_watching_files

def handle_results(results)
failed = results.scan(@config.failed_results_re)
debug_p failed.inspect, "failed"
completed = results =~ @config.completed_re
debug_p completed.inspect

#self.files_to_test = consolidate_failures failed if completed

#color = completed && self.files_to_test.empty? ? :green : :red
#hook color unless $TESTING
failed.each do |(test_name, class_name)|
key = classname_to_path(class_name)
@failed_tests[key] ||= []
@failed_tests[key] << test_name
end
end

#self.tainted = true unless self.files_to_test.empty?
# File vendor/rails/activesupport/lib/active_support/inflector.rb, line 206
def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
2 changes: 1 addition & 1 deletion test.watchr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'lib/autowatchr'

Autowatchr.new(self) do |config|
config.test_regexp = "^#{config.test_dir}/test_\\w+.rb"
config.test_re = "^#{config.test_dir}/test_\\w+.rb"
end
6 changes: 3 additions & 3 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'mocha'
require 'pp'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
Expand All @@ -23,7 +24,6 @@ def silence_stream(stream)
end

def debug_p(obj, label = nil)
$stderr.print "#{label}: " if label
$stderr.write obj.inspect
$stderr.puts
$stderr.puts "#{label}: " if label
$stderr.puts obj.inspect
end
11 changes: 10 additions & 1 deletion test/test_autowatchr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,23 @@ def test_running_multiple_test_files

def test_runs_all_test_files_on_start
result = fake_result("all")
expected_cmd = %!| /usr/local/bin/ruby -I.:#{@lib_dir}:#{@test_dir} -e "%w[#{@test_dir}/test_foo.rb #{@test_dir}/test_bar.rb].each { |f| require f }"!
files = Dir.glob("#{@test_dir}/**/test_*.rb").join(" ")
expected_cmd = %!| /usr/local/bin/ruby -I.:#{@lib_dir}:#{@test_dir} -e "%w[#{files}].each { |f| require f }"!
Autowatchr.any_instance.expects(:open).with(expected_cmd, "r").yields(result)

silence_stream(STDOUT) do
new_autowatchr
end
end

def test_mapping_test_classes_to_test_files
aw = nil
silence_stream(STDOUT) do
aw = new_autowatchr
end
assert_equal "#{@test_dir}/test_foo.rb", aw.classname_to_path("TestFoo")
end

def test_only_runs_failing_tests
result = fake_result("all")
Autowatchr.any_instance.stubs(:open).yields(result)
Expand Down

0 comments on commit 916ff82

Please sign in to comment.