diff --git a/test_cli.sh b/test_cli.sh index bf441d0..8fc2cae 100755 --- a/test_cli.sh +++ b/test_cli.sh @@ -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 diff --git a/test_web.sh b/test_web.sh index 605d939..3c29a08 100755 --- a/test_web.sh +++ b/test_web.sh @@ -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)" diff --git a/tests/cli/gitea.bats b/tests/cli/gitea.bats index 8be658d..cc1f887 100644 --- a/tests/cli/gitea.bats +++ b/tests/cli/gitea.bats @@ -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)" diff --git a/tests/cli/github.bats b/tests/cli/github.bats index 55d2bdd..6d23d99 100644 --- a/tests/cli/github.bats +++ b/tests/cli/github.bats @@ -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)" diff --git a/tests/cli/gitlab.bats b/tests/cli/gitlab.bats index 8b678f4..afe193a 100644 --- a/tests/cli/gitlab.bats +++ b/tests/cli/gitlab.bats @@ -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)" diff --git a/tests/helper.bash b/tests/helper.bash index e62003d..0e9be09 100755 --- a/tests/helper.bash +++ b/tests/helper.bash @@ -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 +}