From c37172a5b7d9bc8a7f538f5a356f13f30feb2e98 Mon Sep 17 00:00:00 2001 From: Gavin Chappell Date: Tue, 8 Feb 2022 13:50:17 +0000 Subject: [PATCH] Prevent adding both skipped and failure elements (#1123) * Prevent adding both skipped and failure elements This is a potential fix to prevent both `skipped` and `failure` elements on a skipped violation in JUnit output format Fixes: #1122 * rebase to fix tests, and remove violation details from skip message --- pkg/writer/junit_xml.go | 14 +++++++++----- pkg/writer/junit_xml_test.go | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/writer/junit_xml.go b/pkg/writer/junit_xml.go index 0d2717fe8..c268268ae 100644 --- a/pkg/writer/junit_xml.go +++ b/pkg/writer/junit_xml.go @@ -164,18 +164,22 @@ func violationsToTestCases(violations []*results.Violation, isSkipped bool) []JU for _, v := range violations { var testCase JUnitTestCase if isSkipped { - testCase = JUnitTestCase{Failure: new(JUnitFailure), SkipMessage: new(JUnitSkipMessage)} - testCase.SkipMessage.Message = v.Comment + testCase = JUnitTestCase{SkipMessage: new(JUnitSkipMessage)} + // since junitXML doesn't contain the attributes we want to show as violations + // we would add details of violations in the skip message, with any provided skip comment + if v.Comment != "" { + testCase.SkipMessage.Message = v.Comment + } } else { testCase = JUnitTestCase{Failure: new(JUnitFailure)} + // since junitXML doesn't contain the attributes we want to show as violations + // we would add details of violations in the failure message + testCase.Failure.Message = getViolationString(*v) } testCase.Classname = v.File testCase.Name = fmt.Sprintf(testNameFormatFailed, v.ResourceName, v.LineNumber, v.RuleID) testCase.Severity = v.Severity testCase.Category = v.Category - // since junitXML doesn't contain the attributes we want to show as violations - // we would add details of violations in the failure message - testCase.Failure.Message = getViolationString(*v) testCases = append(testCases, testCase) } return testCases diff --git a/pkg/writer/junit_xml_test.go b/pkg/writer/junit_xml_test.go index c563edbdc..67e0c4883 100644 --- a/pkg/writer/junit_xml_test.go +++ b/pkg/writer/junit_xml_test.go @@ -39,7 +39,6 @@ func TestJUnitXMLWriter(t *testing.T) { -