Skip to content

Instantly share code, notes, and snippets.

@anhkind
Last active December 22, 2015 06:49
Show Gist options
  • Select an option

  • Save anhkind/6434042 to your computer and use it in GitHub Desktop.

Select an option

Save anhkind/6434042 to your computer and use it in GitHub Desktop.
Git pre-commit hook with rspec
#!/usr/bin/env ruby
exit 0 if ENV["SKIP_TEST"] # set environment SKIP_TEST to skip rspec
require 'pty'
html_path = "/tmp/rspec_results.html"
begin
puts "Running rspec ..."
PTY.spawn( "rspec spec --format h > /tmp/rspec_results.html" ) do |stdin, stdout, pid|
begin
stdin.each { |line| print line }
rescue Errno::EIO
end
end
rescue PTY::ChildExited
puts "Child process exit!"
end
# find out if there were any errors
html = open(html_path).read
examples = html.match(/(\d+) examples/)[0].to_i rescue 0
errors = html.match(/(\d+) errors/)[0].to_i rescue 0
if errors == 0 then
errors = html.match(/(\d+) failure/)[0].to_i rescue 0
end
pending = html.match(/(\d+) pending/)[0].to_i rescue 0
# report
puts "#{examples} examples, #{errors} failures, #{pending} pending"
puts "View spec results at #{File.expand_path(html_path)} for more details."
if errors.zero?
exit 0
else
puts "RSpec failed! The recent changes could not be committed!"
`open #{File.expand_path(html_path)}`
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment