Skip to content

Instantly share code, notes, and snippets.

@tjh
Created June 28, 2011 13:07
Show Gist options
  • Save tjh/1051092 to your computer and use it in GitHub Desktop.
Save tjh/1051092 to your computer and use it in GitHub Desktop.
Watchr script for Cucumber, Rspec, and Evergreen
require 'growl'
ENV["WATCHR"] = "1"
$spec_cmd = "rspec --colour --format nested -d"
$evergreen_cmd = "evergreen run"
def growl(message)
if message == 'true' || message.match(/\s0\s(errors|failures)/)
title = 'Watchr: All tests passed'
priority = 4
else
title = 'Watchr: Tests are failing'
priority = 0
end
Growl.notify message, :title => title, :priority => priority
end
def run(cmd)
cmd = "bundle exec #{cmd}"
system('clear')
puts(cmd)
system(cmd)
end
def run_spec(spec)
result = run "#{$spec_cmd} #{spec}"
growl result.to_s.split("\n").last rescue nil
end
def run_all_js_specs
result = run $evergreen_cmd
growl result.to_s.split("\n").last rescue nil
end
def run_all_specs
result = run "#{$spec_cmd} spec/"
growl result.to_s.split("\n").last rescue nil
end
def related_specs(path)
Dir['spec/**/*.rb'].select { |file| file =~ /#{File.basename(path).split(".").first}_spec.rb/ }
end
def run_all_features
run "cucumber"
end
def run_feature(feature)
run "cucumber --tags @focus #{feature}"
end
def run_suite
run_all_specs
run_all_features
end
watch('spec/spec_helper\.rb') { run_all_specs }
watch('spec/support/.*') { run_all_specs }
watch('spec/.*_spec\.rb') { |m| run_spec m[0] }
watch('spec/.*_spec\.(js|coffee)') { |m| run_all_js_specs }
watch('app/javascripts/.*\.js') { |m| run_all_js_specs }
watch('app/.*\.rb') { |m| related_specs(m[0]).map {|tf| run_spec tf } }
watch('lib/.*\.rb') { |m| related_specs(m[0]).map {|tf| run_spec tf } }
watch('features/support/.*') { |m| run_all_features }
watch('features/.*\.feature') { |m| run_feature m[0] }
# Ctrl-\
Signal.trap 'QUIT' do
puts " --- Running all specs ---\n\n"
run_all_specs
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment