Skip to content

Instantly share code, notes, and snippets.

@ariera
Created August 11, 2011 09:41
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ariera/1139285 to your computer and use it in GitHub Desktop.
Save ariera/1139285 to your computer and use it in GitHub Desktop.
A keyboard shortcut to run tests with watchr

A keyboard shortcut to run tests with watchr

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:

Dependencies

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

A config file for watchr

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.

A keyboard shortcut

Basically I followed this super-user post instructions with the difference that I'm using /usr/bin/ruby as shell instead of /bin/bash:

  1. Start Applications » Automator
  2. Select "Service" for the template of the new Automator workflow
  3. In the top of the right pane, select "Service receives no input in any application"
  4. Drag action "Run Shell Script" from the left pane into the workflow on the right pane
  5. 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

  1. Optional: click the Run button to test
  2. 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:

  1. Open System Preferences » Keyboard » pane Keyboard Shortcuts
  2. Select "Services" in the left pane
  3. Scroll down to General in the right pane
  4. Double-click to the right of the Automator workflow you just created
  5. Press the keys you want to use, and switch panes to ensure the new shortcut is saved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment