151 lines
2.8 KiB
Bash
151 lines
2.8 KiB
Bash
#!/bin/sh
|
|
#
|
|
|
|
#
|
|
# Stash unstaged changes so testing is on the staged changes only
|
|
#
|
|
echo "Stashing changes (to only test the staged changes)"
|
|
STASH_NAME="pre-commit-$(date +%s)"
|
|
git stash save -q --keep-index $STASH_NAME
|
|
if [ $? -eq 0 ]; then
|
|
echo " - done"
|
|
else
|
|
echo " - Stash failed! Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
#
|
|
# ESLINT Testing
|
|
#
|
|
STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".jsx\{0,1\}$")
|
|
ESLINT="$(git rev-parse --show-toplevel)/node_modules/.bin/eslint"
|
|
RUN_LINT=true
|
|
PASS_LINT=true
|
|
|
|
if [[ "$STAGED_JS_FILES" = "" ]]; then
|
|
echo "ESLint - nothing to test"
|
|
RUN_LINT=false
|
|
PASS_LINT=true
|
|
else
|
|
echo "Running ESLint:"
|
|
|
|
# Check for eslint
|
|
if [[ ! -x "$ESLINT" ]]; then
|
|
echo " - Failed to find " "$ESLINT"
|
|
echo " - Please install node modules (npm install)"
|
|
fi
|
|
|
|
for FILE in $STAGED_JS_FILES
|
|
do
|
|
"$ESLINT" "$FILE"
|
|
|
|
if [[ "$?" == 0 ]]; then
|
|
echo " - Passed: $FILE"
|
|
else
|
|
echo " - Failed: $FILE"
|
|
PASS_LINT=false
|
|
fi
|
|
done
|
|
fi
|
|
|
|
#
|
|
# Unit testing
|
|
#
|
|
GULP="$(git rev-parse --show-toplevel)/node_server/node_modules/.bin/gulp"
|
|
RUN_UNIT=true
|
|
PASS_UNIT=false
|
|
|
|
echo "Running Unit Tests:"
|
|
|
|
# Check for gulp
|
|
if [[ ! -x "$GULP" ]]; then
|
|
echo " - Failed to find " "$GULP"
|
|
echo " - Please install node modules (npm install)"
|
|
fi
|
|
|
|
"$GULP" --cwd ./node_server test
|
|
|
|
if [ $? -eq 0 ]; then
|
|
PASS_UNIT=true;
|
|
fi
|
|
|
|
#
|
|
# NSP testing
|
|
#
|
|
STAGED_NPM_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "package\(-lock\)\?\.json$")
|
|
NSP="$(git rev-parse --show-toplevel)/node_modules/.bin/nsp"
|
|
RUN_NSP=true
|
|
PASS_NSP=false
|
|
|
|
if [[ "$STAGED_NPM_FILES" = "" ]]; then
|
|
echo "NSP - nothing to test"
|
|
RUN_NSP=false
|
|
PASS_NSP=true
|
|
else
|
|
|
|
echo "Running NSP:"
|
|
|
|
# Check for eslint
|
|
if [[ ! -x "$NSP" ]]; then
|
|
echo " - Failed to find " "$NSP"
|
|
echo " - Please install node modules (npm install)"
|
|
fi
|
|
|
|
# For simplicity just run NSP in both directories if anything has changed
|
|
"$NSP" check
|
|
if [ $? -eq 0 ]; then
|
|
# Passed the top level. Repeat for the node_server directory
|
|
cd node_server
|
|
"$NSP" check
|
|
if [ $? -eq 0 ]; then
|
|
PASS_NSP=true
|
|
fi
|
|
|
|
# Return back to the main level
|
|
cd ..
|
|
fi
|
|
fi
|
|
|
|
#
|
|
# Restore the stashed changes
|
|
#
|
|
echo "Re-applying stashed changes"
|
|
git stash pop -q
|
|
|
|
#
|
|
# Output results and set exit code
|
|
#
|
|
EXIT_CODE=0
|
|
echo
|
|
echo "### Pre-commit testing results ###"
|
|
echo
|
|
|
|
if ! $RUN_LINT; then
|
|
echo "* ESLint: not run"
|
|
elif ! $PASS_LINT; then
|
|
echo "* ESLint: FAIL!"
|
|
EXIT_CODE=1
|
|
else
|
|
echo "* ESLint: pass"
|
|
fi
|
|
|
|
if ! $RUN_UNIT; then
|
|
echo "* Unit tests: not run"
|
|
elif ! $PASS_UNIT; then
|
|
echo "* Unit tests: FAIL!"
|
|
EXIT_CODE=1
|
|
else
|
|
echo "* Unit tests: pass"
|
|
fi
|
|
|
|
if ! $RUN_NSP; then
|
|
echo "* Node security project: not run"
|
|
elif ! $PASS_NSP; then
|
|
echo "* Node security project: FAIL!"
|
|
EXIT_CODE=1
|
|
else
|
|
echo "* Node security project: pass"
|
|
fi
|
|
|
|
exit $EXIT_CODE
|