When developing in rails I use watchr to run the tests each time a file is saved, but lots of times I find my self adding a whitespace or a newline and saving just to trigger watchr and run the tests.
I wanted to have is a simple keyboard shortcut (like F15
) to tell watchr to run the last spec, and mpartel (thx! : ) gave me an idea on how to do it, so here it is:
Obviously you need watchr, but we're also going to need pgrep
that will help us find out the pid of the watchr process. So go ahead and do
sudo port install proctools
or
brew install proctools
Now you need a .watchr
config file that you can put in you RAILS_ROOT
. Mine looks like this:
@last_file ||= ""
def run_spec(file)
@last_file = file
unless File.exist?(file)
puts "#{file} does not exist"
return
end
puts "Running #{file}"
system "rspec --color --drb --tag current --format Fuubar #{file}"
puts
end
watch("spec/.*/*_spec\.rb") do |match|
run_spec match[0]
end
watch("app/(.*/.*)\.rb") do |match|
run_spec %{spec/#{match[1]}_spec.rb}
end
Signal.trap("USR1") do
run_spec @last_file
end
Now you can launch watchr with watchr .watchr
and it will be listening for the "USR1" signal to run the last spec.
Basically I followed this super-user post instructions with the difference that I'm using /usr/bin/ruby
as shell instead of /bin/bash
:
- Start Applications » Automator
- Select "Service" for the template of the new Automator workflow
- In the top of the right pane, select "Service receives no input in any application"
- Drag action "Run Shell Script" from the left pane into the workflow on the right pane
- Chose
/usr/bin/ruby
as shell and replace the command cat with the following code:
pid = `/usr/local/bin/pgrep -f watchr`.to_i
Process.kill "USR1", pid
NOTE: if you installed proctools with homebrew the path to pgrep will be /usr/local/bin/pgrep
but with macports it is /opt/local/bin/pgrep
- Optional: click the Run button to test
- Hit Cmd-S to save. The name you type will be the name in the Services menu. The workflow will be saved in ~/Library/Services.
To assign a keyboard shortcut, in 10.6 and 10.7:
- Open System Preferences » Keyboard » pane Keyboard Shortcuts
- Select "Services" in the left pane
- Scroll down to General in the right pane
- Double-click to the right of the Automator workflow you just created
- Press the keys you want to use, and switch panes to ensure the new shortcut is saved