Skip to content

Commit d214ed2

Browse files
committed
Add pre-commit hook with unit test for ruleset test URLs
This adds a simple shell script which runs all changed rulesets by https-everywhere-checker. It happens every now and then that people submit rulesets that fail only because of subtle SSL misconfigurations (typically broken certificate chains), and this helps to catch those.
1 parent 46eefb1 commit d214ed2

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎install-dev-dependencies.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ cd test/chromium
3838
pip install --user -r requirements.txt
3939
cd -
4040

41-
# Install a hook to run tests before pushing.
41+
# Install git hooks to run tests before committing and pushing.
4242
ln -sf ../../test.sh .git/hooks/pre-push
43+
ln -sf ../../test/commit.sh .git/hooks/pre-commit

‎test/commit.sh‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
# Run https-everywhere-checker for each changed ruleset
3+
4+
# Folder paths, relative to parent
5+
RULESETFOLDER="src/chrome/content/rules"
6+
RULETESTFOLDER="test/rules"
7+
8+
# Go to git repo root; taken from ../test.sh. Note that
9+
# $GIT_DIR is .git in this case.
10+
if [ -n "$GIT_DIR" ]
11+
then
12+
# $GIT_DIR is set, so we're running as a hook.
13+
cd $GIT_DIR
14+
cd ..
15+
else
16+
# Git command exists? Cool, let's CD to the right place.
17+
git rev-parse && cd "$(git rev-parse --show-toplevel)"
18+
fi
19+
20+
RULESETS_CHANGED=$(git diff --name-only HEAD | grep $RULESETFOLDER | grep '.xml')
21+
22+
# Only run test if something has changed.
23+
if [ "$RULESETS_CHANGED" ]; then
24+
echo >&2 "Ruleset database has changed. Testing test URLs in all changed rulesets."
25+
26+
# Make a list of all changed rulesets, but exclude those
27+
# that do not exist.
28+
for RULESET in $RULESETS_CHANGED; do
29+
# First check if the given ruleset actually exists
30+
if [ ! -f $RULESET ]; then
31+
echo >&2 "Skipped $RULESET; file not found."
32+
continue
33+
fi
34+
TO_BE_TESTED="$TO_BE_TESTED $RULESET"
35+
done
36+
37+
if [ "$TO_BE_TESTED" ]; then
38+
# Do the actual test, using https-everywhere-checker.
39+
TESTOUTPUT=$(python $RULETESTFOLDER/src/https_everywhere_checker/check_rules.py $RULETESTFOLDER/http.checker.config $TO_BE_TESTED 2>&1)
40+
echo >&2 "$TESTOUTPUT"
41+
# Unfortunately, no specific exit codes are available for connection
42+
# failures, so we catch those with grep.
43+
if [[ "$TESTOUTPUT" =~ "ERROR" ]]; then
44+
echo >&2 "Test URL test failed."
45+
exit 1
46+
fi
47+
fi
48+
echo >&2 "Test URL test succeeded."
49+
fi
50+
51+
exit 0

‎test/rules‎

0 commit comments

Comments
 (0)