diff --git a/hooks/pre-exit b/hooks/pre-exit index 7d73799..08f97ad 100755 --- a/hooks/pre-exit +++ b/hooks/pre-exit @@ -73,16 +73,22 @@ save-report-url() { echo "Could not get the tests report URL from $json_file. File not found." return fi - # Could be easier with jq, but we don't want to require it + if which jq >/dev/null; then - echo "Using jq to parse the report URL" - report_url=$(jq -r '.run_url' "${json_file}") + echo "Using jq to parse the report URL" + report_url=$(jq -r '.run_url' "${json_file}" 2>&1) # Capture stderr for error reporting + if [[ "$report_url" == "null" || "$report_url" == "" ]]; then + echo "jq parsing failed with the message: $report_url" + echo "Contents of $json_file:" + cat "$json_file" + return + fi else - echo "Not using jq to parse the report URL" - report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}") + echo "jq not installed, attempting to parse with sed" + report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}") fi - if [ "$report_url" == "null" ]; then + if [[ "$report_url" == "null" || "$report_url" == "" ]]; then echo "Could not get the tests report URL from $json_file. 'run_url' property not found." return fi diff --git a/tests/fixtures/response_missing_url.json b/tests/fixtures/response_missing_url.json new file mode 100644 index 0000000..b912e5c --- /dev/null +++ b/tests/fixtures/response_missing_url.json @@ -0,0 +1 @@ +{"id": "2","run_id": "2","queued": 130,"skipped": 0,"errors": []} \ No newline at end of file diff --git a/tests/fixtures/response_null_url.json b/tests/fixtures/response_null_url.json new file mode 100644 index 0000000..a828ee8 --- /dev/null +++ b/tests/fixtures/response_null_url.json @@ -0,0 +1 @@ +{"id": "4","run_id": "4","queued": 150,"skipped": 0,"errors": [],"run_url": null} \ No newline at end of file diff --git a/tests/pre-exit-report-link.bats b/tests/pre-exit-report-link.bats index 3228e1f..9eb05eb 100644 --- a/tests/pre-exit-report-link.bats +++ b/tests/pre-exit-report-link.bats @@ -1,8 +1,8 @@ #!/usr/bin/env bats # To debug stubs, uncomment these lines: -# export CURL_STUB_DEBUG=/dev/tty -# export GIT_STUB_DEBUG=/dev/tty +#export CURL_STUB_DEBUG=/dev/tty +#export GIT_STUB_DEBUG=/dev/tty setup() { load "$BATS_PLUGIN_PATH/load.bash" @@ -53,7 +53,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \* assert_success assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..." assert_output --partial "curl success" - assert_output --partial "Not using jq" + assert_output --partial "jq not installed, attempting to parse with sed" assert_output --partial "annotation success" unstub which @@ -117,6 +117,7 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \* unstub jq unstub curl } + @test "Annotates report link absorbs empty file error" { export CURL_RESP_FILE="response.json" stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'" @@ -141,7 +142,36 @@ COMMON_CURL_OPTIONS='--form \* --form \* --form \* --form \* --form \* --form \* assert_success assert_output --partial "Uploading './tests/fixtures/junit-1.xml'..." assert_output --partial "curl success" - assert_output --partial "'run_url' property not found" + assert_output --partial "jq parsing failed with the message: " + assert_output --partial "Contents of ./tests/fixtures/response_no_url.json:" + assert_output --partial "There are no report URLs to annotate" + + unstub curl +} + +@test "No annotation when 'run_url' property is missing in JSON response" { + export CURL_RESP_FILE="./tests/fixtures/response_missing_url.json" + stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'" + + run "$PWD/hooks/pre-exit" + + assert_success + assert_output --partial "jq parsing failed with the message:" + assert_output --partial "Contents of ./tests/fixtures/response_missing_url.json:" + assert_output --partial "There are no report URLs to annotate" + + unstub curl +} + +@test "No annotation when 'run_url' is null in JSON response" { + export CURL_RESP_FILE="./tests/fixtures/response_null_url.json" + stub curl "-X POST --silent --show-error --max-time 30 --form format=junit ${COMMON_CURL_OPTIONS} \* \* \* -H \* : echo 'curl success'" + + run "$PWD/hooks/pre-exit" + + assert_success + assert_output --partial "jq parsing failed with the message: null" + assert_output --partial "Contents of ./tests/fixtures/response_null_url.json:" assert_output --partial "There are no report URLs to annotate" unstub curl