Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (61 sloc) 1.956 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
# Run me with:
#
# $ watchr specs.watchr

# --------------------------------------------------
# Convenience Methods
# --------------------------------------------------
# [32m [0m
def growl(message)
  growlnotify = `which growlnotify`.chomp
  image = !message.include?('0 failures') ? "~/.watchr_images/failed.png" : "~/.watchr_images/passed.png"

  status = image =~ /failed/ ? '[31m' : '[32m'

  message = message.split('seconds')[1].split(status)[1].split('[0m')[0]

  options = "'Watchr - Chingu specs' -w --image '#{File.expand_path(image)}' -m '#{message}'"
  system %(#{growlnotify} #{options} &)
end

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}")
  result = `rspec -cfs #{files_to_run}`
  print result
  growl result rescue nil
  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('^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
Something went wrong with that request. Please try again.