Skip to content

Commit 8e1d366

Browse files
authored
Promote JUnit output 'terraform test' feature from experimental status, make incompatibility with remote test execution explicit via flag validation (#36324)
* Promote JUnit reports for `terraform test` out of experimental status * Make JUnit output explicitly for local execution only * Refactor how local test runner is passed JUnit data * Add change file * Add test for incompatible flags
1 parent 408f323 commit 8e1d366

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: NEW FEATURES
2+
body: '`terraform test`: The `-junit-xml` option for the terraform test command is now generally available. This option allows the command to create a test report in JUnit XML format. Feedback during the experimental phase helped map terraform test concepts to the JUnit XML format, and new additons may happen in future releases.'
3+
time: 2025-01-15T11:08:18.566206Z
4+
custom:
5+
Issue: "36324"

internal/command/arguments/test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ func ParseTest(args []string) (*Test, tfdiags.Diagnostics) {
6666
err.Error()))
6767
}
6868

69+
if len(test.JUnitXMLFile) > 0 && len(test.CloudRunSource) > 0 {
70+
diags = diags.Append(tfdiags.Sourceless(
71+
tfdiags.Error,
72+
"Incompatible command-line flags",
73+
"The -junit-xml option is currently not compatible with remote test execution via the -cloud-run flag. If you are interested in JUnit XML output for remotely-executed tests please open an issue in GitHub."))
74+
}
75+
6976
switch {
7077
case jsonOutput:
7178
test.ViewType = ViewJSON

internal/command/arguments/test_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@ func TestParseTest(t *testing.T) {
137137
),
138138
},
139139
},
140+
"incompatible flags: -junit-xml and -cloud-run": {
141+
args: []string{"-junit-xml=./output.xml", "-cloud-run=foobar"},
142+
want: &Test{
143+
CloudRunSource: "foobar",
144+
JUnitXMLFile: "./output.xml",
145+
Filter: nil,
146+
TestDirectory: "tests",
147+
ViewType: ViewHuman,
148+
Vars: &Vars{},
149+
},
150+
wantDiags: tfdiags.Diagnostics{
151+
tfdiags.Sourceless(
152+
tfdiags.Error,
153+
"Incompatible command-line flags",
154+
"The -junit-xml option is currently not compatible with remote test execution via the -cloud-run flag. If you are interested in JUnit XML output for remotely-executed tests please open an issue in GitHub.",
155+
),
156+
},
157+
},
140158
}
141159

142160
cmpOpts := cmpopts.IgnoreUnexported(Operation{}, Vars{}, State{})

internal/command/test.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,6 @@ func (c *TestCommand) Run(rawArgs []string) int {
121121
return 1
122122
}
123123

124-
var junitFile junit.JUnit
125-
if args.JUnitXMLFile != "" {
126-
// JUnit XML output is currently experimental, so that we can gather
127-
// feedback on exactly how we should map the test results to this
128-
// JUnit-oriented format before anyone starts depending on it for real.
129-
if !c.AllowExperimentalFeatures {
130-
diags = diags.Append(tfdiags.Sourceless(
131-
tfdiags.Error,
132-
"JUnit XML output is not available",
133-
"The -junit-xml option is currently experimental and therefore available only in alpha releases of Terraform CLI.",
134-
))
135-
view.Diagnostics(nil, nil, diags)
136-
return 1
137-
}
138-
diags = diags.Append(tfdiags.Sourceless(
139-
tfdiags.Warning,
140-
"JUnit XML output is experimental",
141-
"The -junit-xml option is currently experimental and therefore subject to breaking changes or removal, even in patch releases.",
142-
))
143-
144-
// This line must happen after the TestCommand's calls loadConfigWithTests and has the configLoader field set
145-
junitFile = junit.NewTestJUnitXMLFile(args.JUnitXMLFile, c.configLoader)
146-
}
147-
148124
// Users can also specify variables via the command line, so we'll parse
149125
// all that here.
150126
var items []arguments.FlagNameValue
@@ -224,7 +200,8 @@ func (c *TestCommand) Run(rawArgs []string) int {
224200
Streams: c.Streams,
225201
}
226202
} else {
227-
runner = &local.TestSuiteRunner{
203+
204+
localRunner := &local.TestSuiteRunner{
228205
Config: config,
229206
// The GlobalVariables are loaded from the
230207
// main configuration directory
@@ -235,14 +212,21 @@ func (c *TestCommand) Run(rawArgs []string) int {
235212
TestingDirectory: args.TestDirectory,
236213
Opts: opts,
237214
View: view,
238-
JUnit: junitFile,
239215
Stopped: false,
240216
Cancelled: false,
241217
StoppedCtx: stopCtx,
242218
CancelledCtx: cancelCtx,
243219
Filter: args.Filter,
244220
Verbose: args.Verbose,
245221
}
222+
223+
// JUnit output is only compatible with local test execution
224+
if args.JUnitXMLFile != "" {
225+
// Make sure TestCommand's calls loadConfigWithTests before this code, so configLoader is not nil
226+
localRunner.JUnit = junit.NewTestJUnitXMLFile(args.JUnitXMLFile, c.configLoader)
227+
}
228+
229+
runner = localRunner
246230
}
247231

248232
var testDiags tfdiags.Diagnostics

0 commit comments

Comments
 (0)