Skip to content

Commit

Permalink
Test Collector Plugin Issue - error parsing report url
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivanna Lisetska authored and Ivanna Lisetska committed May 8, 2024
1 parent 6d4447d commit 2c1b285
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
18 changes: 12 additions & 6 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/response_missing_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": "2","run_id": "2","queued": 130,"skipped": 0,"errors": []}
1 change: 1 addition & 0 deletions tests/fixtures/response_null_url.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": "4","run_id": "4","queued": 150,"skipped": 0,"errors": [],"run_url": null}
38 changes: 34 additions & 4 deletions tests/pre-exit-report-link.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'"
Expand All @@ -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
Expand Down

0 comments on commit 2c1b285

Please sign in to comment.