Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promote JUnit output 'terraform test' feature from experimental status, make incompatibility with remote test execution explicit via flag validation #36324

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/NEW FEATURES-20250115-110818.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: NEW FEATURES
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.'
time: 2025-01-15T11:08:18.566206Z
custom:
Issue: "36324"
7 changes: 7 additions & 0 deletions internal/command/arguments/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func ParseTest(args []string) (*Test, tfdiags.Diagnostics) {
err.Error()))
}

if len(test.JUnitXMLFile) > 0 && len(test.CloudRunSource) > 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Incompatible command-line flags",
"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."))
}

switch {
case jsonOutput:
test.ViewType = ViewJSON
Expand Down
18 changes: 18 additions & 0 deletions internal/command/arguments/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ func TestParseTest(t *testing.T) {
),
},
},
"incompatible flags: -junit-xml and -cloud-run": {
args: []string{"-junit-xml=./output.xml", "-cloud-run=foobar"},
want: &Test{
CloudRunSource: "foobar",
JUnitXMLFile: "./output.xml",
Filter: nil,
TestDirectory: "tests",
ViewType: ViewHuman,
Vars: &Vars{},
},
wantDiags: tfdiags.Diagnostics{
tfdiags.Sourceless(
tfdiags.Error,
"Incompatible command-line flags",
"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.",
),
},
},
}

cmpOpts := cmpopts.IgnoreUnexported(Operation{}, Vars{}, State{})
Expand Down
36 changes: 10 additions & 26 deletions internal/command/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,6 @@ func (c *TestCommand) Run(rawArgs []string) int {
return 1
}

var junitFile junit.JUnit
if args.JUnitXMLFile != "" {
// JUnit XML output is currently experimental, so that we can gather
// feedback on exactly how we should map the test results to this
// JUnit-oriented format before anyone starts depending on it for real.
if !c.AllowExperimentalFeatures {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"JUnit XML output is not available",
"The -junit-xml option is currently experimental and therefore available only in alpha releases of Terraform CLI.",
))
view.Diagnostics(nil, nil, diags)
return 1
}
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Warning,
"JUnit XML output is experimental",
"The -junit-xml option is currently experimental and therefore subject to breaking changes or removal, even in patch releases.",
))

// This line must happen after the TestCommand's calls loadConfigWithTests and has the configLoader field set
junitFile = junit.NewTestJUnitXMLFile(args.JUnitXMLFile, c.configLoader)
}

// Users can also specify variables via the command line, so we'll parse
// all that here.
var items []arguments.FlagNameValue
Expand Down Expand Up @@ -224,7 +200,8 @@ func (c *TestCommand) Run(rawArgs []string) int {
Streams: c.Streams,
}
} else {
runner = &local.TestSuiteRunner{

localRunner := &local.TestSuiteRunner{
Config: config,
// The GlobalVariables are loaded from the
// main configuration directory
Expand All @@ -235,14 +212,21 @@ func (c *TestCommand) Run(rawArgs []string) int {
TestingDirectory: args.TestDirectory,
Opts: opts,
View: view,
JUnit: junitFile,
Stopped: false,
Cancelled: false,
StoppedCtx: stopCtx,
CancelledCtx: cancelCtx,
Filter: args.Filter,
Verbose: args.Verbose,
}

// JUnit output is only compatible with local test execution
if args.JUnitXMLFile != "" {
// Make sure TestCommand's calls loadConfigWithTests before this code, so configLoader is not nil
localRunner.JUnit = junit.NewTestJUnitXMLFile(args.JUnitXMLFile, c.configLoader)
}

runner = localRunner
}

var testDiags tfdiags.Diagnostics
Expand Down
Loading