Skip to content

Commit 6d047d0

Browse files
author
Tharlaw
committed
test: add functional tests
1 parent 302d21d commit 6d047d0

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ update:
1616

1717
.PHONY: test
1818
test:
19-
./scripts/run_unit_tests.sh
19+
./scripts/run_unit_tests.sh
20+
./scripts/run_functional_tests.sh $$HOME/.git-templates/hooks/commit-msg

sailr.sh

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
release_tag=master
44
sailr_repo="https://github.com/craicoverflow/sailr/tree/$release_tag"
@@ -48,10 +48,10 @@ function set_config_values() {
4848
function build_regex() {
4949
set_config_values
5050

51-
regexp="^("
51+
regexp="^[.0-9]+$|"
5252

5353
if $revert; then
54-
regexp="${regexp}revert: )?(\w+)("
54+
regexp="${regexp}^([Rr]evert|[Mm]erge):? )?.*$|^("
5555
fi
5656

5757
for type in "${types[@]}"
@@ -64,19 +64,25 @@ function build_regex() {
6464
regexp="${regexp}.{$min_length,$max_length}$"
6565
}
6666

67+
6768
# Print out a standard error message which explains
6869
# how the commit message should be structured
6970
function print_error() {
70-
echo -e "\n\e[1m\e[31m[INVALID COMMIT MESSAGE]"
71+
commit_message=$1
72+
regular_expression=$2
73+
echo -e "\n\e[31m[Invalid Commit Message]"
7174
echo -e "------------------------\033[0m\e[0m"
72-
echo -e "\e[1mValid types:\e[0m \e[34m${types[@]}\033[0m"
73-
echo -e "\e[1mMax length (first line):\e[0m \e[34m$max_length\033[0m"
74-
echo -e "\e[1mMin length (first line):\e[0m \e[34m$min_length\033[0m\n"
75+
echo -e "Valid types: \e[36m${types[@]}\033[0m"
76+
echo -e "Max length (first line): \e[36m$max_length\033[0m"
77+
echo -e "Min length (first line): \e[36m$min_length\033[0m\n"
78+
echo -e "\e[37mRegex: \e[33m$regular_expression\033[0m"
79+
echo -e "\e[37mActual commit message: \e[33m\"$commit_message\"\033[0m"
80+
echo -e "\e[37mActual length: \e[33m$(echo $commit_message | wc -c)\033[0m\n"
7581
}
7682

7783
set_config
7884

79-
# # check if the repo has a sailr config file
85+
# check if the repo has a sailr config file
8086
check_sailr_config
8187

8288
# make sure jq is installed
@@ -92,4 +98,4 @@ if [[ ! $START_LINE =~ $regexp ]]; then
9298
# commit message is invalid according to config - block commit
9399
print_error
94100
exit 1
95-
fi
101+
fi

scripts/run_functional_tests.sh

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/bin/bash
2+
3+
COMMIT_MSG_SCRIPT=$1
4+
5+
# build ci docs feat fix perf refactor style test chore
6+
allowed_commit_messages=("build: change build details" \
7+
"ci: make continuous integration change" \
8+
"docs: updates some documentation" \
9+
"feat: implements a new feature" \
10+
"fix: correct typo in function" \
11+
"perf: improve perf" \
12+
"refactor: implement a refactor" \
13+
"style: make a style change" \
14+
"test(ui): add a new test" \
15+
"chore: complete a simple chore" \
16+
"Revert \"test: add a new test\" " \
17+
"Revert: \"refactor(labels): implement a refactor\" " \
18+
"revert: \"feat: implements a new feature\" " \
19+
"Merge branch 'feature-branch'" \
20+
"merge branch 'Feature-Branch'" \
21+
"0.1.0")
22+
23+
disallowed_commit_messages=("implements a new feature" \
24+
"" \
25+
"docs: implements a new feature that we have been greatly anticipating")
26+
27+
TMPFILE=$(mktemp)
28+
ERROR_FILE=$(mktemp)
29+
COUNT=0
30+
for allowed_commit_message in "${allowed_commit_messages[@]}"
31+
do
32+
echo "$allowed_commit_message" > $TMPFILE
33+
$COMMIT_MSG_SCRIPT $TMPFILE
34+
# Track the number of failed tests
35+
if [[ "$?" != "0" ]]
36+
then
37+
COUNT=$(echo "$COUNT + 1" | bc -l)
38+
echo "---"
39+
fi
40+
done
41+
42+
for disallowed_commit_message in "${disallowed_commit_messages[@]}"
43+
do
44+
echo "$disallowed_commit_message" > $TMPFILE
45+
# Since it passed, there is no error to be seen.
46+
$COMMIT_MSG_SCRIPT $TMPFILE >> $TMPFILE
47+
# Track the number of failed tests
48+
if [[ "$?" == "0" ]]
49+
then
50+
echo -e "\e[1;32mFailed: $disallowed_commit_message\003\e[0m" >> $ERROR_FILE
51+
COUNT=$(echo "$COUNT + 1" | bc -l)
52+
fi
53+
done
54+
55+
# Print summary of the number of failed tests
56+
cat $ERROR_FILE
57+
if [[ $COUNT -eq 0 ]]
58+
then
59+
echo -e "\e[1;32m All tests passed!\033\e[0m"
60+
elif [[ $COUNT -eq 1 ]]
61+
then
62+
echo -e "\e[1;32m $COUNT test failed\033\e[0m"
63+
elif [[ $COUNT -gt 1 ]]
64+
then
65+
echo -e "\e[1;32m $COUNT tests failed\033\e[0m"
66+
fi
67+
68+
# Cleanup
69+
rm $TMPFILE
70+
rm $ERROR_FILE

0 commit comments

Comments
 (0)