Category Archives: git

I have a rails project that is too wily for autotest, but I still want a simple way to make sure my tests are passing. Using git hooks is a simple solution to this problem.

Git Hooks?
Hooks (more info here) are little scripts that git triggers after certain commands. They are located in your projects GIT_DIR/hooks directory and are disabled by default. To enable them just chmod +x the hook you want to run.

How is this helpful for testing?
You can run your specs from the GIT_DIR/hooks/pre-commit hook! The script is run every time you call git commit. If the script exits with a non-zero status your changes won’t be committed and you can scurry to fix your specs. Here is a little example of how I’m using in one of my apps


#!/usr/bin/env ruby

html_path = "spec_results.html"

`spec -f h:#{html_path} -f p spec` # run the spec. send progress to screen. save html results to html_path

# find out how many errors were found
html = open(html_path).read
examples = html.match(/(\d+) examples/)[0].to_i rescue 0
failures = html.match(/(\d+) failures/)[0].to_i rescue 0
pending = html.match(/(\d+) pending/)[0].to_i rescue 0

if failures.zero?
  puts "0 failures! #{examples} run, #{pending} pending"
else
  puts "\aDID NOT COMMIT YOUR FILES!"
  puts "View spec results at #{File.expand_path(html_path)}"
  puts
  puts "#{failures} failures! #{examples} run, #{pending} pending"
  exit 1
end