Skip to content

Instantly share code, notes, and snippets.

@jonpaul
Created June 14, 2011 02:12
Show Gist options
  • Save jonpaul/1024175 to your computer and use it in GitHub Desktop.
Save jonpaul/1024175 to your computer and use it in GitHub Desktop.
Auto test + spork via watchr
#!/usr/bin/env watchr
#RUN_ALL_TESTS||=true
#watchr runs through this file a few times
#ensure it only gets set once
unless defined?(GROWL)
$stderr.sync=true
$stdout.sync=true
ENV["WATCHR"] = "1"
GROWL=`which growlnotify`.chomp
IMAGE_DIR=File.expand_path("~/.watchr_images/")
SPORK=true
#assumes we are running from the rails root
#Dir.chdir(File.dirname(__FILE__))
end
def growl(message,title=nil,image=nil)
return if GROWL.empty?
title ||= "Watchr Test Results"
message.gsub! /\[[0-9]+?m/, ''
image ||= message.include?('0 failures, 0 errors') ? "#{IMAGE_DIR}/pass.png" : "#{IMAGE_DIR}/fail.png"
options = "-n Watchr --image '#{image}' -m '#{message}' '#{title}'"
run "#{GROWL} #{options}"
end
def clear
#system('clear')
end
def run(cmd,verbose=true)
puts
puts("# #{cmd}")
puts
ret=[]
IO.popen(cmd) do |output|
while line = output.gets do
puts line if verbose
ret << line
end
end
ret #join("\n")
end
def run_test_file(file)
clear
if defined?(SPORK)
result = run %{testdrb #{file}}
else
result = run %{ruby -I"lib:test" -rubygems #{file}}
end
growl units_message(result)
#puts result
end
def run_all_tests
clear
result = run "rake test"
growl units_message(result)
#puts result
end
def units_message(result)
#result.last
#we have messages turned off
#need to parse results to get something close
failures=result.count { |s| s =~/Failure/ }
errors=result.count { |s| s =~/Error/ }
"#{failures} failures, #{errors} errors"
end
def run_all_features
return if !cucumber?
clear
result=run("rake cucumber:all")
growl 'cucumbered','spork'
end
def related_test_files(path)
Dir['test/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_test.rb/ }
end
def run_suite
run_all_tests
run_all_features
end
def cucumber?
Dir.entries('.').include? 'features'
end
watch('test/.*/.*_test\.rb') { |m| run_test_file(m[0]) }
#watch('app/.*/.*\.rb') { |m| related_test_files(m[0]).map {|tf| run_test_file(tf) } }
watch('features/.*/.*\.feature') { run_all_features }
watch('test/test_helper\.rb') { run_all_tests } if defined?(RUN_ALL_TEST)
if defined?(RUN_ALL_TESTS)
clear
# Ctrl-\
Signal.trap 'QUIT' do
puts " --- Running all tests ---\n\n"
run_all_tests
end
@interrupted = false
# Ctrl-C
Signal.trap 'INT' do
if @interrupted then
@wants_to_quit = true
abort("\n")
else
puts "Interrupt a second time to quit"
@interrupted = true
Kernel.sleep 1.5
# raise Interrupt, nil # let the run loop catch it
run_suite
end
end
run_suite
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment