Skip to content

Commit 9a0269b

Browse files
author
Aaron Roller
authored
Merge pull request #3 from AutoModality/AM-337/gtest
Added error reporting
2 parents 171d949 + ebaebbd commit 9a0269b

18 files changed

+382
-2
lines changed

.github/workflows/release.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
run: |
1515
[ ${{ steps.xunit-viewer.outputs.report-file }} == "test-reports/index.html" ]
1616
- name: Attach the report
17+
if: always()
1718
uses: actions/upload-artifact@v1
1819
with:
1920
name: test-reports
@@ -36,6 +37,7 @@ jobs:
3637
run: |
3738
[ ${{ steps.xunit-viewer.outputs.report-file }} == "${{ env.RESULTS_PATH }}/index.html" ]
3839
- name: Attach the report
40+
if: always()
3941
uses: actions/upload-artifact@v1
4042
with:
4143
name: alternate-results-path-reports
@@ -56,17 +58,60 @@ jobs:
5658
run: |
5759
[ ${{ steps.xunit-viewer.outputs.report-file }} == "somewhere/custom-report.html" ]
5860
- name: Attach the report
61+
if: always()
5962
uses: actions/upload-artifact@v1
6063
with:
6164
name: alternate-report-path-reports
6265
path: somewhere/custom-report.html
66+
test-show-failure:
67+
runs-on: ubuntu-18.04
68+
continue-on-error: true
69+
name: Demonstrates failure case
70+
steps:
71+
- uses: actions/checkout@v1
72+
- id: xunit-viewer
73+
uses: ./ #use the current project relative to root
74+
with:
75+
results: test/failure
76+
- name: Attach the report
77+
if: always()
78+
uses: actions/upload-artifact@v1
79+
with:
80+
name: ${{ steps.xunit-viewer.outputs.report-name }}
81+
path: ${{ steps.xunit-viewer.outputs.report-dir }}
82+
test-ignore-failure:
83+
runs-on: ubuntu-18.04
84+
name: Demonstrates failure is not reported
85+
steps:
86+
- uses: actions/checkout@v1
87+
- id: xunit-viewer
88+
uses: ./ #use the current project relative to root
89+
with:
90+
results: test/failure
91+
fail: false
92+
- name: Attach the report
93+
if: always()
94+
uses: actions/upload-artifact@v1
95+
with:
96+
name: ${{ steps.xunit-viewer.outputs.report-name }}
97+
path: ${{ steps.xunit-viewer.outputs.report-dir }}
98+
test-detection:
99+
runs-on: ubuntu-18.04
100+
name: Run tests to ensure detection is working
101+
steps:
102+
- uses: actions/checkout@v1
103+
- name: Run tests
104+
run: test/all.sh
63105
release:
64106
runs-on: ubuntu-18.04
65107
name: Release if all tests pass on master branch
66108
needs:
67109
- test-defaults
68110
- test-alternate-results-path
69111
- test-alternate-report-path
112+
- test-detection
113+
- test-show-failure
114+
- test-ignore-failure
70115
steps:
71116
- uses: actions/checkout@v1
72117
- name: Semantic Release

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
.history

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ FROM node:lts-alpine
33

44
# Copies your code file from your action repository to the filesystem path `/` of the container
55
COPY entrypoint.sh /entrypoint.sh
6+
COPY detection.sh /detection.sh
67

78
RUN npm i -g xunit-viewer
89

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ the corresponding Action Run.
2222
Assumes your test reports exist in a folder named `test-reports` and generates the
2323
report at `test-reports/index.html`.
2424

25+
This test will report a failure if any of the tests failed or errored. `fail=false` will disable.
26+
2527
```
2628
name: Test
2729
on: push
@@ -36,7 +38,7 @@ jobs:
3638
- name: Attach the report
3739
uses: actions/upload-artifact@v1
3840
with:
39-
name: test-reports
41+
name: ${{ steps.xunit-viewer.outputs.report-name }}
4042
path: ${{ steps.xunit-viewer.outputs.report-dir }}
4143
4244
```

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@ inputs:
99
description: File/Folder of test results
1010
required: true
1111
default: test-reports
12+
title:
13+
description: HTML title e.g. "My Tests". Default is Repository Name.
14+
required: false
15+
default: ${{ github.repository }}
1216
output:
1317
description: Output filename including optional path
1418
required: false
19+
fail:
20+
description: Return failing exit code when failure or error detected
21+
required: false
22+
default: "true"
1523
outputs:
1624
report-file:
1725
description: "The file path where the report can be found. Equal to 'output' input."
1826
report-dir:
1927
description: "The directory where the report can be found."
28+
report-name:
29+
description: "Use this name with Artifact Upload for unique attachments"
2030
runs:
2131
using: 'docker'
2232
image: 'Dockerfile'
2333
args: # these map in order with entrypoint.sh
2434
- ${{ inputs.results }}
2535
- ${{ inputs.output }}
36+
- ${{ inputs.title }}
37+
- ${{ inputs.fail }}
2638

2739

detection.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
#NOTICE: Shell, not bash, so it runs on Alpine
3+
4+
set -e
5+
6+
file_or_folder="$1"
7+
8+
# detects failures or errors because xunit-viewer unfortunately does not report
9+
# exits with non zero code if any error or failure is found
10+
# uses simple grep to look for failures="0" and errors="0"
11+
12+
13+
suite_success(){
14+
file="$1"
15+
# look for 0 failures and errors
16+
# grep reports non zero code if anything but zero failures/errors
17+
if cat "$file" | grep "<testsuites";then
18+
suite_line=$(cat "$file" | grep "<testsuites")
19+
if echo "$suite_line" | grep "failures=\"0\"";then
20+
if echo "$suite_line" | grep "errors=\"0\"";then
21+
return 0
22+
else
23+
echo "Error found in $file: $suite_line"
24+
return 1
25+
fi
26+
else
27+
echo "Failure found in $file: $suite_line"
28+
return 1
29+
fi
30+
else
31+
echo "Skipping file without suite: $file"
32+
fi
33+
}
34+
35+
echo "Inspecting '$file_or_folder'"
36+
37+
if [ -d "$file_or_folder" ];then
38+
for file in $file_or_folder/*.xml ; do suite_success "$file"; done
39+
elif [ -f "$file_or_folder" ]; then
40+
suite_success "$file_or_folder"
41+
else
42+
echo "Skipping: No file or folder found at $file_or_folder"
43+
fi
44+

entrypoint.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
2+
#NOTICE: Shell, not bash, so it runs on Alpine
23

34
set -e #exit on error
45

@@ -10,6 +11,9 @@ DEFAULT="none" # a way to handle ordered empty arguments of bash
1011
# output may be provided, otherwise will default to results dir
1112
results="${1}"
1213
output="${2}"
14+
title="${3:-Tests}"
15+
fail="${4:-true}"
16+
1317

1418
if [[ -z "$results" || "$results" == "$DEFAULT" ]]; then
1519
echo "result file/folder is required"
@@ -33,7 +37,17 @@ fi
3337
#ensure path exists
3438
mkdir -p "$(dirname "$output")"
3539

36-
xunit-viewer --results="$results" --output="$output"
40+
xunit-viewer --results="$results" --output="$output" --console=true --title="$title"
41+
42+
architecture=$(uname -m)
43+
report_name="test-results-$GITHUB_REPOSITORY-$GITHUB_WORKFLOW-$architecture-$GITHUB_RUN_ID"
44+
report_name_escaped=$(echo "$report_name" | tr /\\:*\<\>\|? -)
3745

3846
echo ::set-output name=report-file::"$output" #reference available to other actions
3947
echo ::set-output name=report-dir::"$report_dir" #for easy attachment of a folder
48+
echo ::set-output name=report-name::"$report_name_escaped" #to provide a globally unique name for downloading results
49+
50+
# report non zero exit code if any failure or error detected
51+
if "$fail" == "true";then
52+
/detection.sh "$results"
53+
fi

test/all.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#!/bin/sh
3+
#NOTICE: Shell, not bash, so it runs on Alpine
4+
5+
set -e
6+
7+
test/one.sh test/success_fixture.xml 0
8+
test/one.sh test/failure_fixture.xml 1
9+
test/one.sh test/error_fixture.xml 1
10+
test/one.sh test 1
11+
test/one.sh test/success 0
12+
test/one.sh test/failure 1
13+
test/one.sh test/error 1
14+
test/one.sh missing.xml 0
15+
test/one.sh test/missing.xml 0

test/error/error_fixture.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites tests="5" failures="0" disabled="0" errors="2" timestamp="2020-06-05T02:02:29" time="0.002" name="AllTests">
3+
<testsuite name="StrCompare" tests="5" failures="2" disabled="0" errors="0" time="0.002">
4+
<testcase name="CStrEqual" status="run" time="0" classname="StrCompare" />
5+
<testcase name="CStrEqual2" status="run" time="0" classname="StrCompare" />
6+
<testcase name="CStrEqual3" status="run" time="0" classname="StrCompare" />
7+
<testcase name="CStrNotEqual" status="run" time="0" classname="StrCompare">
8+
<failure message="/github/workspace/catkin_ws/src/am_super/test/sample_tests.cpp:27&#x0A; Expected: expectVal&#x0A; Which is: &quot;hello gtest&quot;&#x0A;To be equal to: actualValFalse&#x0A; Which is: &quot;hello world&quot;" type=""><![CDATA[/github/workspace/catkin_ws/src/am_super/test/sample_tests.cpp:27
9+
Expected: expectVal
10+
Which is: "hello gtest"
11+
To be equal to: actualValFalse
12+
Which is: "hello world"]]></failure>
13+
</testcase>
14+
<testcase name="CStrNotEqual4" status="run" time="0.002" classname="StrCompare">
15+
<failure message="/github/workspace/catkin_ws/src/am_super/test/sample_tests.cpp:32&#x0A; Expected: expectVal&#x0A; Which is: &quot;hello gtest&quot;&#x0A;To be equal to: actualValFalse&#x0A; Which is: &quot;hello world&quot;" type=""><![CDATA[/github/workspace/catkin_ws/src/am_super/test/sample_tests.cpp:32
16+
Expected: expectVal
17+
Which is: "hello gtest"
18+
To be equal to: actualValFalse
19+
Which is: "hello world"]]></failure>
20+
</testcase>
21+
</testsuite>
22+
</testsuites>

test/error/success_fixture.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites tests="3" failures="0" disabled="0" errors="0" timestamp="2020-06-05T02:02:29" time="0.002" name="AllTests">
3+
<testsuite name="StrCompare" tests="5" failures="2" disabled="0" errors="0" time="0.002">
4+
<testcase name="CStrEqual" status="run" time="0" classname="StrCompare" />
5+
<testcase name="CStrEqual2" status="run" time="0" classname="StrCompare" />
6+
<testcase name="CStrEqual3" status="run" time="0" classname="StrCompare" />
7+
</testsuite>
8+
</testsuites>

0 commit comments

Comments
 (0)