Last active
November 8, 2019 07:22
-
-
Save FLamparski/661ac4aa4339ab0561e9dfa42aac7230 to your computer and use it in GitHub Desktop.
Git pre-commit hook for Fish to check if you're committing Jasmine tests that contain fdescribe or fit
This file contains 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/local/bin/fish | |
# should be 0 at the end of the file | |
set num_errors 0 | |
# is the file a JS test? | |
function is_test -a file | |
echo $file | egrep '(src\/test\/js|\.spec\.js$)' > /dev/null | |
return $status | |
end | |
# print a warning header | |
function print_warning -a string file | |
printf '** Remaining %s in %s%s' $string (set_color -o yellow) $file | |
set_color normal | |
echo | |
end | |
# check if the staged file contains the offending pattern | |
function would_commit_offending_pattern -a pattern file | |
git show :$file | grep $pattern > /dev/null | |
return $status | |
end | |
# check a file against a given pattern. if found, | |
# print warning and increment num_errors. | |
function ban_pattern -a description pattern file | |
if would_commit_offending_pattern $pattern $file | |
print_warning $description $file | |
grep -nH --color -C3 $pattern $file | |
echo | |
set num_errors (math $num_errors + 1) | |
end | |
end | |
for file in (git diff --cached --name-only) | |
# check for 'fdescribe' and 'fit' in JS tests | |
if is_test $file | |
ban_pattern 'fdescribe()' 'fdescribe\s*(' $file | |
ban_pattern 'fit()' 'fit\s*(' $file | |
end | |
end | |
if [ $num_errors -gt 0 ] | |
echo 'Aborting commit.' | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment