Heres an example git hook to run your Rails specs automatically upon commit/push
#!/usr/bin/env ruby
if ENV["SKIP_GLOBAL_GIT_HOOKS"].to_s == "true"
exit 0
end
if ENV["SKIP_TESTS"].to_s != "true"
if Dir.exists?("spec/")
test_cmd = "bundle exec rspec spec"
elsif Dir.exists?("test/")
test_cmd = "bundle exec rake test"
end
if test_cmd
puts "RUNNING TEST SUITE...\n"
if system(test_cmd, out: STDOUT)
test_passed = true
end
if !test_passed
puts "\nCOMMIT FAILED!! See failing test output above."
puts "To skip this check use `SKIP_TESTS=true`"
exit 1
end
end
end
Also since git hooks are not committed to your repository you can either use one of the following techniques to save this git hook:
- If you want to share the hook with all team members working on the project, then use the .githooks technique
- If you want to have this hook available to every project you personally work on, then use the global git hooks technique as described below
- Create a folder within your dotfiles repo for your git hooks and add your git hooks here
- Add the following line to your .bashrc/.zshrc (recommended) or run manually,
[[ -d "$HOME/path/to/your/global_git_hooks_folder" ]] && git config --global core.hooksPath "$HOME/path/to/your/global_git_hooks_folder"
- Now every project you work on will also include the global hooks from the specified folder