Be more flexible about paths to call the test suite

This commit is contained in:
southerntofu 2022-02-20 20:15:38 +01:00
parent 71804c4b3a
commit 9b18c0e162
6 changed files with 60 additions and 23 deletions

View File

@ -8,7 +8,33 @@ ORIGDIR="$(pwd)"
cd "$SCRIPTDIR"
export FORGEHOOK="$(pwd)/tests/mock-forgehook.sh"
export WHCK="$ORIGDIR"/"$1"
source tests/helper.bash
if [ -z "$1" ]; then
output="$(findBin "$SCRIPTDIR"/../cli)"
if [ $? -eq 0 ]; then
if [[ "$(echo "$output" | wc -l)" == "1" ]]; then
FIND_WHCK="$output"
else
# Some warnings, print but use result from last line
echo "$output" | head -n -1
FIND_WHCK="$(echo "$output" | tail -n 1)"
fi
else
echo "ERROR: Could not find webhook validation program. Output:"
echo "$output"
exit 1
fi
# cli script needs to be executed to get absolute path to the program
export WHCK="$("$FIND_WHCK")"
else
if WHCK="$(realpath "$ORIGDIR"/"$1")"; then
export WHCK
else
export WHCK="$(realpath "$1")"
fi
fi
bats tests/cli/*.bats

View File

@ -2,28 +2,7 @@
SCRIPTDIR="$(dirname "$0")"
# Used to find executable from several likely locations
# Pass any number of arguments, returns the first one that's
# an executable file. If a file exists but is not executable,
# a warning is printed
# Output is in the form of "WARNING:\nresult" so if exit code is 0 you can safely fetch the last line to find the result
findBin () {
#if [ $# -eq 0 ]; exit; fi
while [ ! $# -eq 0 ]; do
# Resolve symlinks
f="$(readlink -m "$1")"
shift
if [ ! -f "$f" ]; then continue; fi
if [ ! -x "$f" ]; then
echo "WARNING: "$f" exists but is not executable" >&2
continue
fi
echo "$f"
return
done
# nothing found
return 1
}
source "$SCRIPTDIR"/tests/helper.bash
if [ -z "$1" ]; then
output="$(findBin "$SCRIPTDIR"/server "$SCRIPTDIR"/../server)"

View File

@ -8,6 +8,7 @@ function setup {
# Which CLI validator to use?
if [ -z "$WHCK" ]; then WHCK="whck"; fi
echo "$WHCK"
export WHCK_DIR="$(mktemp -d)"
}
@ -17,6 +18,7 @@ function teardown {
}
@test "correct signature works" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"
@ -30,6 +32,7 @@ function teardown {
}
@test "incorrect signature fails" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"

View File

@ -8,6 +8,7 @@ function setup {
# Which CLI validator to use?
if [ -z "$WHCK" ]; then WHCK="whck"; fi
echo "$WHCK"
export WHCK_DIR="$(mktemp -d)"
}
@ -17,6 +18,7 @@ function teardown {
}
@test "correct signature works" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../github.json "$repo")"
secret="$($FORGEHOOK secret $repo)"
@ -30,6 +32,7 @@ function teardown {
}
@test "incorrect signature fails" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"

View File

@ -8,6 +8,7 @@ function setup {
# Which CLI validator to use?
if [ -z "$WHCK" ]; then WHCK="whck"; fi
echo "$WHCK"
export WHCK_DIR="$(mktemp -d)"
}
@ -17,6 +18,7 @@ function teardown {
}
@test "correct signature works" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"
@ -29,6 +31,7 @@ function teardown {
}
@test "incorrect signature fails" {
[ -x "$WHCK" ]
repo="https://tildegit.org/forge/hook.sh"
webhook="$(gen_webhook ../gitea.json "$repo")"
secret="$($FORGEHOOK secret $repo)"

View File

@ -68,3 +68,26 @@ function hash_hmac {
# Don't print (stdin)= ...
echo -n "$data" | openssl dgst "-$digest" -hmac "$key" | awk '{print $2}'
}
# Used to find executable from several likely locations
# Pass any number of arguments, returns the first one that's
# an executable file. If a file exists but is not executable,
# a warning is printed
# Output is in the form of "WARNING:\nresult" so if exit code is 0 you can safely fetch the last line to find the result
findBin () {
#if [ $# -eq 0 ]; exit; fi
while [ ! $# -eq 0 ]; do
# Resolve symlinks
f="$(readlink -m "$1")"
shift
if [ ! -f "$f" ]; then continue; fi
if [ ! -x "$f" ]; then
echo "WARNING: "$f" exists but is not executable" >&2
continue
fi
echo "$f"
return
done
# nothing found
return 1
}