Skip to content

formatter: make JUnit testcase names unique - #2522

Closed
Zakharden wants to merge 1 commit into
terraform-linters:masterfrom
Zakharden:fix/tflint-1608-junit-testcase-names
Closed

formatter: make JUnit testcase names unique#2522
Zakharden wants to merge 1 commit into
terraform-linters:masterfrom
Zakharden:fix/tflint-1608-junit-testcase-names

Conversation

@Zakharden

Copy link
Copy Markdown
Contributor

Fixes #1608.

Summary

The JUnit formatter previously used only the rule name as each lint issue's testcase name, so multiple failures from the same rule produced duplicate testcase names. This keeps the first testcase name unchanged and appends an incrementing suffix to later occurrences in the sorted issue output, for example:

  • terraform_deprecated_interpolation
  • terraform_deprecated_interpolation_2
  • terraform_deprecated_interpolation_3

This intentionally avoids adding source ranges or resource names to testcase names; detailed range context remains in the failure message/body.

Validation

  • gofmt -w formatter/junit.go formatter/junit_test.go
  • go test ./formatter
  • git diff --check HEAD~1 HEAD

@bendrucker bendrucker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review, this should cover errors too, then LGTM

Comment thread formatter/junit.go
for i, issue := range issues.Sort() {
cases[i] = formatter.JUnitTestCase{
Name: issue.Rule.Name(),
Name: uniqueJUnitTestCaseName(issue.Rule.Name(), names),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dedups issue names, but the error testcases appended later (cases = append(cases, errorCases...)) skip it. junitErrors sets Name: diag.Summary directly, so multiple diagnostics sharing a summary (e.g. two "Unsupported argument" errors on different lines) still produce duplicate <testcase> names in the same <testsuite> (#1608).

Consider deduping once over the full cases slice after the errors are appended, rather than rewriting names mid-loop for issues only.

Comment thread formatter/junit.go
fmt.Fprint(f.Stdout, string(out))
}

func uniqueJUnitTestCaseName(name string, counts map[string]int) string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: the generated suffix can collide with a real rule name. If rule foo appears twice it becomes foo_2; a distinct rule literally named foo_2 would then also emit foo_2. Contrived for today's rule names (no numeric suffixes), so not worth complicating the code over, but perhaps worth at least a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

JUnit testcase name not unique per failure

2 participants