Git commit-msg exec npm test

Viewed 10

I'd like to verify my commit message using git hook and exec npm test.

For the commit message everythink works but it seems not working for npm test if the test fail.

Commit message that I want to accept :

[FIX-A1325] KEYWORDS : Word1 word2
[FIX-D1325-P4556] KEYWORDS : Word1 word2
[NEW] KEYWORD : Word1 word2
[UPD] KEYWORD : Word1 word2
[DOC] Word1 word2
TMP
[REF] KEYWORD : Word1 word2
[GIT] Word1 word2
[DEL] Word1 word2

My code :

#!/bin/sh

commitMessage=$(cat $1)

pattern="TMP|\[FIX-A[0-9]+\]\s[A-Z]+\s:\s.+|\[FIX-D[0-9]+-P[0-9]+\]\s[A-Z]+\s:\s.+|\[NEW\]\s[A-Z]+\s:\s.+|\[REF\]\s[A-Z]+\s:\s.+|\[DOC\]\s.+|\[UPD\]\s[A-Z]+\s:\s.+|\[GIT\]\s.+|\[DEL\]\s.+"

if [[ ! "$commitMessage" =~ $pattern ]]; then
    echo "Commit message invalid => $commitMessage"
    exit 1
fi

npm test
1 Answers

I think you need to catch the exit code of npm test and exit your script with it like:

#!/bin/sh

commitMessage=$(cat $1)

pattern="TMP|\[FIX-A[0-9]+\]\s[A-Z]+\s:\s.+|\[FIX-D[0-9]+-P[0-9]+\]\s[A-Z]+\s:\s.+|\[NEW\]\s[A-Z]+\s:\s.+|\[REF\]\s[A-Z]+\s:\s.+|\[DOC\]\s.+|\[UPD\]\s[A-Z]+\s:\s.+|\[GIT\]\s.+|\[DEL\]\s.+"

if [[ ! "$commitMessage" =~ $pattern ]]; then
    echo "Commit message invalid => $commitMessage"
    exit 1
fi


npm test

# $? stores exit value of the last command
if [ $? -ne 0 ]; then
 echo "Tests must pass before commit!"
 exit 1
fi
Related