Skip to content

Instantly share code, notes, and snippets.

@kurbmedia
Created February 7, 2011 21:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kurbmedia/815311 to your computer and use it in GitHub Desktop.
Save kurbmedia/815311 to your computer and use it in GitHub Desktop.
watchr + growl + rspec
class Growl
attr_accessor :report, :status
def initialize(report, opts = {})
@report, options = report, create_options(opts)
message = create_message
opts = options.inject([]) do |arr, item|
key, value = item.first, item.last
(value.is_a?(Hash) ? arr.push("--#{key} '#{value[@status]}'") : arr.push("--#{key} '#{value}'"); arr;)
end
notifier = `which growlnotify`.chomp
opts.unshift("-w -n RSpec")
system %(#{notifier} #{opts.join(' ')} -m \"#{message}\" &)
end
private
def create_message
examples = @report.instance_variable_get(:@example_count).to_i
failures = @report.instance_variable_get(:@failure_count).to_i
pendings = @report.instance_variable_get(:@pending_count).to_i
duration = @report.instance_variable_get(:@duration)
@status = (failures == 0) ? :pass : :fail
"#{examples} Examples - #{failures} Failures - #{pendings} Pending"
end
def create_options(opts)
{:title => "Test Results",
:image => {:pass => File.expand_path('~/.watchr_images/passed.png'), :fail => File.expand_path('~/.watchr_images/failed.png')},
:priority => {:pass => 0, :fail => 2}
}.merge!(opts)
end
end
# Run me with:
#
# $ watchr specs.watchr
# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
def all_spec_files
Dir['spec/**/*_spec.rb']
end
def run_spec_matching(thing_to_match)
matches = all_spec_files.grep(/#{thing_to_match}/i)
if matches.empty?
puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
else
run matches.join(' ')
end
end
def run(files_to_run)
puts("Running: #{files_to_run}")
system("clear;rspec -cfs #{files_to_run}")
no_int_for_you
end
def run_all_specs
run(all_spec_files.join(' '))
end
# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch('^spec/(.*)_spec\.rb') { |m| run_spec_matching(m[1]) }
watch('^lib/(.*)\.rb') { |m| run_spec_matching(m[1]) }
watch('^app/(.*)\.rb') { |m| run_spec_matching(m[1]) }
watch('^spec/spec_helper\.rb') { run_all_specs }
watch('^spec/support/.*\.rb') { run_all_specs }
# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
def no_int_for_you
@sent_an_int = nil
end
Signal.trap 'INT' do
if @sent_an_int then
puts " A second INT? Ok, I get the message. Shutting down now."
exit
else
puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
@sent_an_int = true
Kernel.sleep 1.5
run_all_specs
end
end
# vim:ft=ruby
config.after :suite do
Growl.new(config.reporter)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment