Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created December 22, 2011 05:20
Show Gist options
  • Save shuhei/1509029 to your computer and use it in GitHub Desktop.
Save shuhei/1509029 to your computer and use it in GitHub Desktop.
Pre-commit hook for RSpec
#!/usr/bin/env ruby
def bash_source(file)
`bash -c "source #{file}"`
end
# Make sure to use RVM
user_rvm = "#{ENV['HOME']}/.rvm/scripts/rvm"
system_rvm = "/usr/local/rvm/scripts/rvm"
if File.exists?(user_rvm)
bash_source user_rvm
elsif File.exists?(system_rvm)
bash_source system_rvm
else
puts "No RVM!"
puts '***** COMMIT FAILED *****'
exit 1
end
`rvm reload`
# Run RSpec showing results on stdout and writing them into a temporary file.
out_path = '__spec_results__'
system "rspec -c -fp -fp -o #{out_path}"
last_line = File.open(out_path).read.split("\n").last
File.delete out_path
failed = (last_line =~ /, 0 failures/).nil?
if failed
puts '***** COMMIT FAILED *****'
exit 1
end
@shuhei
Copy link
Author

shuhei commented Dec 22, 2011

This script should be placed in .git/hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment