Skip to content

Commit

Permalink
Move all JUnit-related code down to where it's used
Browse files Browse the repository at this point in the history
Away from the Views section of the code where it was relevant before
  • Loading branch information
SarahFrench committed Jan 14, 2025
1 parent a734e1e commit 0f1e8fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
16 changes: 4 additions & 12 deletions internal/command/junit/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type TestJUnitXMLFile struct {

type JUnit interface {
Save(*moduletest.Suite) tfdiags.Diagnostics
SetTestSuiteRunner(moduletest.TestSuiteRunner)
}

var _ JUnit = (*TestJUnitXMLFile)(nil)
Expand All @@ -59,21 +58,14 @@ var _ JUnit = (*TestJUnitXMLFile)(nil)
// point of being asked to write a conclusion. Otherwise it will create the
// file at that time. If creating or overwriting the file fails, a subsequent
// call to method Err will return information about the problem.
func NewTestJUnitXMLFile(filename string, configLoader *configload.Loader) *TestJUnitXMLFile {
func NewTestJUnitXMLFile(filename string, configLoader *configload.Loader, testSuiteRunner moduletest.TestSuiteRunner) *TestJUnitXMLFile {
return &TestJUnitXMLFile{
filename: filename,
configLoader: configLoader,
// testSuiteRunner unset at this point
filename: filename,
configLoader: configLoader,
testSuiteRunner: testSuiteRunner,
}
}

// SetTestSuiteRunner is used to update a TestJUnitXMLFile struct to contain
// a pointer to the test runner it's being used in. This allows the generated file to report
// whether skipped files are due to the runner being interrupted or not.
func (v *TestJUnitXMLFile) SetTestSuiteRunner(testSuiteRunner moduletest.TestSuiteRunner) {
v.testSuiteRunner = testSuiteRunner
}

// Save takes in a test suite, generates JUnit XML summarising the test results,
// and saves the content to the filename specified by user
func (v *TestJUnitXMLFile) Save(suite *moduletest.Suite) tfdiags.Diagnostics {
Expand Down
3 changes: 1 addition & 2 deletions internal/command/junit/junit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ func Test_TestJUnitXMLFile_Save(t *testing.T) {
loader, cleanup := configload.NewLoaderForTests(t)
defer cleanup()

j := junit.NewTestJUnitXMLFile(path, loader)
j.SetTestSuiteRunner(tc.runner)
j := junit.NewTestJUnitXMLFile(path, loader, tc.runner)

// Process data & save file
j.Save(&tc.suite)
Expand Down
47 changes: 20 additions & 27 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 @@ -243,9 +219,26 @@ func (c *TestCommand) Run(rawArgs []string) int {
Verbose: args.Verbose,
}

if junitFile != nil {
junitFile.SetTestSuiteRunner(localRunner)
localRunner.JUnit = junitFile
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

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the done var defined on line 166

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the stop var defined on line 167

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the cancel var defined on line 168

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the cancel var defined on line 168

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the done var defined on line 166

Check failure on line 233 in internal/command/test.go

View workflow job for this annotation

GitHub Actions / Code Consistency Checks

this return statement may be reached without using the stop var defined on line 167
}
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.",
))
// Make sure TestCommand's calls loadConfigWithTests before this code, so configLoader is not nil
localRunner.JUnit = junit.NewTestJUnitXMLFile(args.JUnitXMLFile, c.configLoader, localRunner)
}

runner = localRunner
Expand Down

0 comments on commit 0f1e8fd

Please sign in to comment.