Skip to content

Instantly share code, notes, and snippets.

@kevinmccaughey
Forked from seven1m/spec_watchr.rb
Created June 17, 2012 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinmccaughey/2945278 to your computer and use it in GitHub Desktop.
Save kevinmccaughey/2945278 to your computer and use it in GitHub Desktop.
watchr config for Rails + RSpec projects, uses Growl (Mac) or notify-send (Linux)
# put this in spec/watchr
# use with Spork
# run with `watchr spec/watchr`
# images are from Tango icon theme
# http://commons.wikimedia.org/wiki/Tango_icons#Weather
# for added bonus:
# set keyboard shortcuts Ctrl-Alt-S to run "pkill -USR2 -f spork"
# ...and Ctrl-Alt-T to run "pkill -USR2 -f watchr"
PASS_ICON = File.expand_path('../../public/images/weather-clear.png', __FILE__)
FAIL_ICON = File.expand_path('../../public/images/weather-showers.png', __FILE__)
LINUX = RUBY_PLATFORM =~ /linux/i
def notify(icon, heading, body)
cmd = if LINUX
"notify-send -i #{icon} --hint=int:transient:1 '#{heading}' '#{body}'"
else
"growlnotify --image #{icon} -m '#{body}' '#{heading}'"
end
system(cmd)
end
def run_tests(test, force=false)
if force || File.exist?(test)
puts "-" * 80
puts "Running #{test}..."
cmd = IO.popen("rspec --color --tty --drb --format=doc #{test} 2>&1")
result = ''
until cmd.eof?
line = cmd.gets
result << line
print line
end
if result =~ /(\d+) failure/ and (failures = $1.to_i) > 0
notify(FAIL_ICON, 'Test Failure', "#{failures} test#{failures == 1 ? ' has' : 's have'} failed!")
else
notify(PASS_ICON, 'Test Success', 'All tests passing.')
end
$stdout.write(result)
else
puts "#{test} does not exist."
end
end
watch('spec/(.*/*)_spec\.rb') { |m| run_tests("spec/#{m[1]}_spec.rb") }
watch('app/(.*)\.rb' ) { |m| run_tests("spec/#{m[1]}_spec.rb") }
@interrupt_received = false
Signal.trap 'INT' do
if @interrupt_received
exit 0
else
@interrupt_received = true
puts "\nInterrupt a second time to quit"
Kernel.sleep 1
@interrupt_received = false
run_tests('spec/**/*_spec.rb', :force)
end
end
Signal.trap 'USR2' do
run_tests('spec/**/*_spec.rb', :force)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment