Skip to content

Instantly share code, notes, and snippets.

@bmabey
Forked from dkubb/git-pre-commit.rb
Created November 1, 2008 17:56
Show Gist options
  • Save bmabey/21547 to your computer and use it in GitHub Desktop.
Save bmabey/21547 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# git pre-commit hook
#
# This will ensure 100% code coverage and all specs passing before allowing a commit
# to the git repo.
#
# Place the file at .git/hooks/pre-commit and use chmod a+x to make it executable.
system('rake clean >/dev/null 2>&1')
path = ENV['PATH'].split(':')[1..-1].map { |p| File.expand_path(p) }.detect do |path|
File.file?(File.join(path, 'rcov'))
end
raise "rcov not found in any path" if path.nil?
output = %x{#{path}/rcov -t --no-html spec/{requests,models}/*_spec.rb 2>&1}
raise "Error: #{output}" if $? != 0
rspec, rcov = output.split("\n").last(2)
raise "Cannot parse rspec '#{rspec}'" unless rspec_match = rspec.match(/\A(\d+)\sexamples?,\s(\d+)\sfailures?(?:,\s(\d)\spendings?)?\z/)
raise "Cannot parse rcov '#{rcov}'" unless rcov_match = rcov.match(/\A(100\.0|[1-9]?\d\.\d)%/)
examples, failures, pending = rspec_match.captures.map { |n| n.to_i }
coverage = rcov_match[1].to_f
if failures.nonzero? || coverage != 100
puts "Bad: #{coverage}% coverage, #{failures} failures, #{pending} pending specs"
exit 1
elsif pending.nonzero?
puts "Good: 100% coverage, no failures, #{pending} pending specs"
else
puts 'Perfect: 100% coverage, no failures, no pending specs'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment