Skip to content

Instantly share code, notes, and snippets.

@shigeya
Forked from boxedup/tests.watchr
Created January 27, 2011 01:02
Show Gist options
  • Save shigeya/797853 to your computer and use it in GitHub Desktop.
Save shigeya/797853 to your computer and use it in GitHub Desktop.
# rspec continuous runner script using watchr, growl and spork
# run this script via 'watchr'
# gist at: https://gist.github.com/797853/
# based on: https://gist.github.com/276317/
# you may want to install image file for growl.
if RUBY_VERSION >= "1.9" # multibyte encoding only supported in Ruby 1.9
Encoding.default_external = "UTF-8"
end
$image_dir = "~/.rc/images" # directory for image file or nil
$common_opts = "--drb" # options for both rspec and cucumber
$rspec_opts = $common_opts # options for rspec
$cucumber_opts = $common_opts # options for cucumber
ENV["WATCHR"] = "1"
def clear_screen
# system 'clear'
puts ""
puts ""
puts ""
puts "-" * 80
end
def growl(message)
growlnotify = `which growlnotify`.chomp
title = "Watchr Spec Results"
image_opt = ""
if $image_dir != nil and File.directory?(File.expand_path($image_dir))
image_path = message.include?(', 0 failures') ? "#{$image_dir}/success.png" : "#{$image_dir}/failed.png"
image_opt = "--image '" + File.expand_path(image_path)+ "'"
end
options = "-w -n Watchr #{image_opt} -m '#{message}' '#{title}'"
system "#{growlnotify} #{options} &"
end
def growl_stat(result)
r = result.split("\n")
count = r.pop
elapsed = r.pop
count.gsub!(/\x1b\[(\d|;)+m/,'') # removing escape characters generated by --tty --color output
growl count + "\n" + elapsed
end
def run(cmd)
puts(cmd)
`#{cmd}`
end
def run_test_file(file)
clear_screen
result = run(%Q(rspec #{$rspec_opts} #{file}))
growl_stat result
puts result
end
def run_all_specs
clear_screen
spec_files = Dir['spec/**/*_spec.rb'].join(" ")
result = run "ruby -S bundle exec rspec #{$rspec_opts} #{spec_files}"
growl_stat result
puts result
end
def run_model_specs
clear_screen
model_spec_files = Dir['spec/models/*_spec.rb'].join(" ")
result = run "ruby -S bundle exec rspec #{$rspec_opts} #{model_spec_files}"
growl_stat result
puts result
end
def run_all_features
clear_screen
# system("cucumber")
end
def related_spec_files(path)
Dir['spec/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_spec.rb/ }
end
def related_factory_files(path)
path.gsub(/factory/, 'spec')
end
def run_suite
run_all_specs
run_all_features
end
watch('spec/spec_helper\.rb') { run_all_tests }
watch('spec/.*_spec\.rb') { |m| run_test_file(m[0]) }
watch('spec/.*_factory\.rb') { run_model_specs }
watch('app/.*\.rb') { |m| related_spec_files(m[0]).map {|tf| run_test_file(tf) } }
watch('features/.*/.*\.feature') { run_all_features }
@interrupted = false
run_all_specs
# Ctrl-\
Signal.trap 'QUIT' do
puts " --- Running all tests ---\n\n"
run_all_specs
end
# 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
@interrupted = false
end
end
# Local Variables:
# mode: ruby
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment