Skip to content

Instantly share code, notes, and snippets.

@Kmaschta
Last active November 16, 2022 19:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kmaschta/98a508a43f902628209e57a06f73f88e to your computer and use it in GitHub Desktop.
Save Kmaschta/98a508a43f902628209e57a06f73f88e to your computer and use it in GitHub Desktop.
Pre-commit hook (warn if you have it.only, describe.only, fit or fdescribe)
#!/bin/bash
SRC=$(git rev-parse --show-toplevel)
EXCLUDE="--exclude-dir 'node_modules' --exclude-dir '.git'"
#==========================================================
# Check if I forgot to remove 'only' keyword from tests.
# To make sure that before commit run all tests
only_command="grep -c -h -r $EXCLUDE -E \"(describe|it)\.only\" $SRC | awk -F ':' '{x +=\$0}; END {print x}'"
fonly_command="grep -c -h -r $EXCLUDE -E \"f(it|describe)\(\" $SRC | awk -F ':' '{x +=\$0}; END {print x}'"
only=`eval $only_command`
fonly=`eval $fonly_command`
if (( $((only + fonly)) > 0 ))
then
echo 'Remove ONLY from tests.'
# Output list of found only entries
eval "grep -r -n $EXCLUDE -E \"(describe|it)\.only\" $SRC"
eval "grep -r -n $EXCLUDE -E \"f(it|describe)\(\" $SRC"
exit 1
fi
@Kmaschta
Copy link
Author

Kmaschta commented Jul 20, 2016

Install it and do not forget execution permisions.

curl https://gist.githubusercontent.com/Kmaschta/98a508a43f902628209e57a06f73f88e/raw/pre-commit > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

@greg0ire
Copy link

greg0ire commented Nov 3, 2016

Why not limit the grep to what is being commited (or rather the "add" part of the commit)?

@Kmaschta
Copy link
Author

Kmaschta commented Nov 3, 2016

Because I didn't know how to that before I've read your code.
Your Junk Checker is definitely better than my little script! But it does the job without install a plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment