Skip to content

Commit 7a9b8ba

Browse files
authored
Merge pull request #372 from dflook/junit-xml
Add a junit-xml-path output for the test action
2 parents b03136d + daec8e1 commit 7a9b8ba

File tree

8 files changed

+102
-4
lines changed

8 files changed

+102
-4
lines changed

.github/workflows/test-test.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,68 @@ jobs:
1919
- name: Test
2020
uses: ./terraform-test
2121
id: test
22+
env:
23+
TERRAFORM_VERSION: 1.10.0
24+
with:
25+
path: tests/workflows/test-test/local
26+
27+
- name: Check Passed
28+
env:
29+
FAILURE_REASON: ${{ steps.test.outputs.failure-reason }}
30+
JUNIT_XML_PATH: ${{ steps.test.outputs.junit-xml-path }}
31+
run: |
32+
if [[ "$FAILURE_REASON" != "" ]]; then
33+
echo "::error:: failure-reason not set correctly"
34+
exit 1
35+
fi
36+
37+
if [[ "$JUNIT_XML_PATH" != "" ]]; then
38+
echo "::error:: junit-xml-path should not be set"
39+
exit 1
40+
fi
41+
42+
junit:
43+
runs-on: ubuntu-24.04
44+
name: Junit support
45+
steps:
46+
- name: Checkout
47+
uses: actions/checkout@v4
48+
with:
49+
persist-credentials: false
50+
51+
- name: Test
52+
uses: ./terraform-test
53+
id: test
54+
env:
55+
TERRAFORM_VERSION: 1.11.0
2256
with:
2357
path: tests/workflows/test-test/local
2458

2559
- name: Check Passed
2660
env:
2761
FAILURE_REASON: ${{ steps.test.outputs.failure-reason }}
62+
JUNIT_XML_PATH: ${{ steps.test.outputs.junit-xml-path }}
2863
run: |
2964
if [[ "$FAILURE_REASON" != "" ]]; then
3065
echo "::error:: failure-reason not set correctly"
3166
exit 1
3267
fi
68+
69+
if [[ "$JUNIT_XML_PATH" == "" ]]; then
70+
echo "::error:: junit-xml-path should be set"
71+
exit 1
72+
fi
73+
74+
# Check the output looks right
75+
if [[ ! -f "$JUNIT_XML_PATH" ]]; then
76+
echo "::error:: junit-xml-path does not point to a file"
77+
exit 1
78+
fi
79+
80+
if [[ "$(grep -c '<testsuites' "$JUNIT_XML_PATH")" -ne 1 ]]; then
81+
echo "::error:: junit-xml-path does not contain a testsuites tag"
82+
exit 1
83+
fi
3384
3485
filter:
3586
runs-on: ubuntu-24.04

.github/workflows/test-version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ jobs:
611611
run: |
612612
echo "The terraform version was $DETECTED_TERRAFORM_VERSION"
613613
614-
if [[ "$DETECTED_TERRAFORM_VERSION" != *"1.10"* ]]; then
614+
if [[ "$DETECTED_TERRAFORM_VERSION" != *"1.11"* ]]; then
615615
echo "::error:: Latest version was not used"
616616
exit 1
617617
fi

docs-gen/action.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import textwrap
22
from dataclasses import dataclass, field
33
from textwrap import indent
4-
from typing import Callable
4+
from typing import Callable, Type
55

66

77
def heading(text: str, level: int=1) -> str:
@@ -95,6 +95,7 @@ class Output:
9595
type: str = None
9696
aliases: list[str] = field(default_factory=list)
9797
meta_output: bool = False
98+
available_in: list[Type[Terraform] | Type[OpenTofu]] = field(default_factory=lambda: [Terraform, OpenTofu])
9899

99100
def markdown(self, tool: Tool) -> str:
100101
if self.meta_output:
@@ -182,6 +183,7 @@ def assert_ordering(self):
182183
"plan_path",
183184
"json_plan_path",
184185
"text_plan_path",
186+
"junit-xml-path",
185187
"to_add",
186188
"failure-reason",
187189
"lock-info",
@@ -231,6 +233,8 @@ def markdown(self, tool: Tool) -> str:
231233
s += text_chunk(self.outputs_intro)
232234

233235
for output in self.outputs:
236+
if tool not in output.available_in:
237+
continue
234238
s += text_chunk(output.markdown(tool))
235239

236240
if self.environment_variables:
@@ -273,10 +277,10 @@ def action_yaml(self, tool: Tool) -> str:
273277

274278
s += '\n'
275279

276-
if [output for output in self.outputs if not output.meta_output]:
280+
if [output for output in self.outputs if not output.meta_output and tool in output.available_in]:
277281
s += 'outputs:\n'
278282

279-
for output in self.outputs:
283+
for output in (output for output in self.outputs if not output.meta_output and tool in output.available_in):
280284
if output.meta_output:
281285
continue
282286

docs-gen/actions/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from inputs.var_file import var_file
1313
from inputs.variables import variables
1414
from outputs.failure_reason import failure_reason
15+
from outputs.junit_xml import junit_xml_path
1516

1617
test = Action(
1718
'test',
@@ -31,6 +32,7 @@
3132
var_file
3233
],
3334
outputs=[
35+
junit_xml_path,
3436
dataclasses.replace(failure_reason, description='''
3537
When the job outcome is `failure`, this output may be set. The value may be one of:
3638

docs-gen/outputs/junit_xml.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from action import Output, Terraform
2+
3+
junit_xml_path = Output(
4+
name='junit-xml-path',
5+
type='string',
6+
description='''
7+
A test report in JUnit XML format.
8+
9+
The path is relative to the Actions workspace.
10+
11+
This will only be available when using Terraform 1.11.0 or later.
12+
''',
13+
available_in = [Terraform]
14+
)

image/entrypoints/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function set-test-args() {
2626
TEST_ARGS="$TEST_ARGS -filter=$file"
2727
done
2828
fi
29+
30+
if [[ "$TOOL_COMMAND_NAME" == "terraform" && $TERRAFORM_VER_MAJOR -ge 1 && $TERRAFORM_VER_MINOR -ge 11 ]]; then
31+
TEST_ARGS="$TEST_ARGS -junit-xml=$STEP_TMP_DIR/test-result.xml"
32+
fi
2933
}
3034

3135
function test() {
@@ -45,6 +49,12 @@ function test() {
4549

4650
cat "$STEP_TMP_DIR/terraform_test.stderr"
4751

52+
if [[ -f "$STEP_TMP_DIR/test-result.xml" ]]; then
53+
mkdir -p "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR"
54+
cp "$STEP_TMP_DIR/test-result.xml" "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/test-result.xml"
55+
set_output junit-xml-path "$WORKSPACE_TMP_DIR/test-result.xml"
56+
fi
57+
4858
if [[ $TEST_EXIT -eq 0 ]]; then
4959
# Workaround a bit of stupidity in the terraform test command
5060
if grep -q "Success! 0 passed, 0 failed." "$STEP_TMP_DIR/terraform_test.stdout"; then

terraform-test/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ If the tests fail, the job will stop with a failure status.
8181

8282
## Outputs
8383

84+
* `junit-xml-path`
85+
86+
A test report in JUnit XML format.
87+
88+
The path is relative to the Actions workspace.
89+
90+
This will only be available when using Terraform 1.11.0 or later.
91+
92+
- Type: string
93+
8494
* `failure-reason`
8595

8696
When the job outcome is `failure`, this output may be set. The value may be one of:

terraform-test/action.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ inputs:
3131
required: false
3232

3333
outputs:
34+
junit-xml-path:
35+
description: |
36+
A test report in JUnit XML format.
37+
38+
The path is relative to the Actions workspace.
39+
40+
This will only be available when using Terraform 1.11.0 or later.
3441
failure-reason:
3542
description: |
3643
When the job outcome is `failure`, this output may be set. The value may be one of:

0 commit comments

Comments
 (0)