Last active
April 17, 2018 09:08
-
-
Save adillera/6aea75f5eb19e347120e756e793bbb80 to your computer and use it in GitHub Desktop.
This pre-commit hooks run RSpec before every commit. Put both files inside .git/hooks. Ruby portion of the pre-commit hook. This handles running the spec and analyzing the spec log created. The pre-commit file is a direct port from the source at: https://gist.github.com/zewelor/2834349. It loads RVM and calls the rspec commit hook. For non-RVM u…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Run the RSPec githook. | |
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then | |
# First try to load from a user install | |
source "$HOME/.rvm/scripts/rvm" | |
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then | |
# Then try to load from a root install | |
source "/usr/local/rvm/scripts/rvm" | |
else | |
printf "ERROR: An RVM installation was not found.\n" | |
fi | |
rvm reload | |
ruby .git/hooks/pre-commit.rspec.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Run RSpec before any commits | |
# Adapted from https://gist.github.com/zewelor/2834349 | |
@no_commit_message = "Well, this is awkward...\n" | |
@no_commit_message = "#{ @no_commit_message }I cannot let you commit these files" | |
@no_commit_message = "#{ @no_commit_message } because of two reasons:\n" | |
@no_commit_message = "#{ @no_commit_message }Either you still have pending tests" | |
@no_commit_message = "#{ @no_commit_message } waiting to be written or worst," | |
@no_commit_message = "#{ @no_commit_message } you still have failing tests not yet" | |
@no_commit_message = "#{ @no_commit_message } fixed.\n" | |
@no_commit_message = "#{ @no_commit_message }So create or fix those tests first!\n" | |
@no_commit_message = "#{ @no_commit_message }OR you can run your commit with" | |
@no_commit_message = "#{ @no_commit_message } the --no-verify option and you can" | |
@no_commit_message = "#{ @no_commit_message } skip this process." | |
def run_rspec | |
format = "txt".to_sym | |
html_path = "tmp/git_hook_spec_results.#{ format }" | |
`touch #{ html_path }` | |
if `rake -T spec`.empty? | |
puts 'Running RSpec' | |
`rspec -c -o #{ html_path } spec/ 1> /dev/null 2>/dev/null` | |
else | |
puts 'Running rake spec' | |
`rake spec SPEC_OPTS="-c -o #{ html_path } 1> /dev/null 2>/dev/null"` | |
end | |
# Check if there are errors found | |
results = open(html_path).read | |
examples = results.match(/(\d+) examples?/)[0].to_i rescue 0 | |
failures = results.match(/(\d+) failures?/)[0].to_i rescue 0 | |
pending = results.match(/(\d+) pending/)[0].to_i rescue 0 | |
if failures.zero? && pending.zero? | |
puts "0 failures... #{ examples } specs ran, #{ pending } pending." | |
else | |
puts results | |
puts @no_commit_message | |
exit 1 | |
end | |
end | |
# Run Everything | |
run_rspec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment