Skip to content

Instantly share code, notes, and snippets.

@cableray
Created September 3, 2015 20:34
Show Gist options
  • Save cableray/b2f6676c4cb5463466f7 to your computer and use it in GitHub Desktop.
Save cableray/b2f6676c4cb5463466f7 to your computer and use it in GitHub Desktop.
my focus prevention pre-commit hook
#!/usr/bin/env ruby
# vim: set syntax=ruby
begin
require 'colorize'
rescue LoadError
class String
def fake_color(*)
self
end
alias underline fake_color
alias yellow fake_color
alias red fake_color
end
end
require 'stringio'
flag = false
buffer = StringIO.new
files = `git diff --cached --name-only --diff-filter=ACM | grep "_spec\.rb$"`.split("\n")
files.each do |file|
results = `git diff --cached #{file} | grep "^\+[^+]"`.split("\n").grep(/(describe|it|example|specify|context)\b.*focus/).map { |r| r.sub(/^\+[\s\t]*/, '') }
if results.length > 0
buffer.puts '',
file.to_s.underline.yellow,
results.map { |r| `grep -n #{Regexp.escape(r).inspect} #{file}`}
flag = true
end
end
if flag
puts "Please remove :focus from your tests before committing:".red
buffer.rewind
print buffer.read
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment