Skip to content

Commit e4912fa

Browse files
author
Kevin Vu
committed
testing: include file-level errors in JUnit skipped elements
Fixes #37801
1 parent 620264e commit e4912fa

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

internal/command/junit/junit.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func junitXMLTestReport(suite *moduletest.Suite, suiteRunnerStopped bool, source
209209
// Depending on run status, add either of: "skipped", "failure", or "error" elements
210210
switch run.Status {
211211
case moduletest.Skip:
212-
testCase.Skipped = skipDetails(i, file, suiteRunnerStopped)
212+
testCase.Skipped = skipDetails(i, file, suiteRunnerStopped, sources)
213213

214214
case moduletest.Fail:
215215
// When the test fails we only use error diags that originate from failing assertions
@@ -300,8 +300,9 @@ func failureMessage(failedAssertions tfdiags.Diagnostics, checkCount int) string
300300
// Test can be skipped due to:
301301
// 1. terraform test recieving an interrupt from users; all unstarted tests will be skipped
302302
// 2. A previous run in a file has failed, causing subsequent run blocks to be skipped
303+
// 3. File-level errors (e.g., invalid variable references) causing all tests to be skipped
303304
// The returned value is used to set content in the "skipped" element
304-
func skipDetails(runIndex int, file *moduletest.File, suiteStopped bool) *withMessage {
305+
func skipDetails(runIndex int, file *moduletest.File, suiteStopped bool, sources map[string][]byte) *withMessage {
305306
if suiteStopped {
306307
// Test suite experienced an interrupt
307308
// This block only handles graceful Stop interrupts, as Cancel interrupts will prevent a JUnit file being produced at all
@@ -323,6 +324,14 @@ func skipDetails(runIndex int, file *moduletest.File, suiteStopped bool) *withMe
323324
}
324325
}
325326
}
327+
328+
// Check for file-level error diagnostics that caused tests to be skipped
329+
if file.Diagnostics.HasErrors() {
330+
return &withMessage{
331+
Message: "Testcase skipped due to file-level errors",
332+
Body: getDiagString(file.Diagnostics, sources),
333+
}
334+
}
326335
}
327336

328337
// Unhandled case: This results in <skipped></skipped> with no attributes or body

internal/command/junit/junit_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/terraform/internal/command/junit"
1313
"github.com/hashicorp/terraform/internal/configs/configload"
1414
"github.com/hashicorp/terraform/internal/moduletest"
15+
"github.com/hashicorp/terraform/internal/tfdiags"
1516
)
1617

1718
// This test cannot access sources when contructing output for XML files. Due to this, the majority of testing
@@ -114,6 +115,47 @@ func Test_TestJUnitXMLFile_Save(t *testing.T) {
114115
<skipped></skipped>
115116
</testcase>
116117
</testsuite>
118+
</testsuites>`),
119+
},
120+
"<skipped> element includes file-level error diagnostics when tests are skipped due to file errors": {
121+
filename: "output.xml",
122+
runner: &local.TestSuiteRunner{},
123+
suite: func() moduletest.Suite {
124+
file := &moduletest.File{
125+
Name: "file1.tftest.hcl",
126+
Status: moduletest.Error,
127+
Runs: []*moduletest.Run{
128+
{
129+
Name: "my_test",
130+
Status: moduletest.Skip,
131+
},
132+
},
133+
}
134+
// Simulate file-level error diagnostic (e.g., invalid variable reference)
135+
var diags tfdiags.Diagnostics
136+
diags = diags.Append(tfdiags.Sourceless(
137+
tfdiags.Error,
138+
"Invalid reference",
139+
"You can only reference global variables within the test file variables block.",
140+
))
141+
file.AppendDiagnostics(diags)
142+
return moduletest.Suite{
143+
Status: moduletest.Error,
144+
Files: map[string]*moduletest.File{
145+
"file1.tftest.hcl": file,
146+
},
147+
}
148+
}(),
149+
expectedOuput: []byte(`<?xml version="1.0" encoding="UTF-8"?><testsuites>
150+
<testsuite name="file1.tftest.hcl" tests="1" skipped="1" failures="0" errors="0">
151+
<testcase name="my_test" classname="file1.tftest.hcl">
152+
<skipped message="Testcase skipped due to file-level errors"><![CDATA[
153+
Error: Invalid reference
154+
155+
You can only reference global variables within the test file variables block.
156+
]]></skipped>
157+
</testcase>
158+
</testsuite>
117159
</testsuites>`),
118160
},
119161
}

0 commit comments

Comments
 (0)