44package vacuum_report
55
66import (
7- "github.com/stretchr/testify/assert "
7+ "strings "
88 "testing"
99 "time"
10+
11+ "github.com/daveshanley/vacuum/model"
12+ "github.com/stretchr/testify/assert"
13+ "go.yaml.in/yaml/v4"
1014)
1115
1216func TestBuildJUnitReport (t * testing.T ) {
@@ -18,3 +22,150 @@ func TestBuildJUnitReport(t *testing.T) {
1822 data := BuildJUnitReport (j .ResultSet , f , []string {"test" , "args" })
1923 assert .GreaterOrEqual (t , len (data ), 407 )
2024}
25+
26+ func TestBuildJUnitReportWithConfig_DefaultOnlyErrorsAreFailures (t * testing.T ) {
27+ // Create results with different severities
28+ results := []model.RuleFunctionResult {
29+ {
30+ Rule : & model.Rule {Id : "error-rule" , Severity : model .SeverityError , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
31+ Message : "This is an error" ,
32+ StartNode : & yaml.Node {Line : 1 , Column : 1 },
33+ EndNode : & yaml.Node {Line : 1 , Column : 10 },
34+ },
35+ {
36+ Rule : & model.Rule {Id : "warn-rule" , Severity : model .SeverityWarn , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
37+ Message : "This is a warning" ,
38+ StartNode : & yaml.Node {Line : 2 , Column : 1 },
39+ EndNode : & yaml.Node {Line : 2 , Column : 10 },
40+ },
41+ {
42+ Rule : & model.Rule {Id : "info-rule" , Severity : model .SeverityInfo , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
43+ Message : "This is info" ,
44+ StartNode : & yaml.Node {Line : 3 , Column : 1 },
45+ EndNode : & yaml.Node {Line : 3 , Column : 10 },
46+ },
47+ {
48+ Rule : & model.Rule {Id : "hint-rule" , Severity : model .SeverityHint , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
49+ Message : "This is a hint" ,
50+ StartNode : & yaml.Node {Line : 4 , Column : 1 },
51+ EndNode : & yaml.Node {Line : 4 , Column : 10 },
52+ },
53+ }
54+
55+ resultSet := model .NewRuleResultSet (results )
56+ config := JUnitConfig {FailOnWarn : false } // Default: only errors are failures
57+
58+ data := BuildJUnitReportWithConfig (resultSet , time .Now (), []string {"test.yaml" }, config )
59+ output := string (data )
60+
61+ // Should have 4 tests but only 1 failure (the error)
62+ assert .Contains (t , output , `tests="4"` )
63+ assert .Contains (t , output , `failures="1"` )
64+
65+ // Error should have failure element
66+ assert .Contains (t , output , `<failure message="This is an error" type="ERROR"` )
67+
68+ // Warning, info, hint should NOT have failure elements
69+ assert .NotContains (t , output , `<failure message="This is a warning"` )
70+ assert .NotContains (t , output , `<failure message="This is info"` )
71+ assert .NotContains (t , output , `<failure message="This is a hint"` )
72+
73+ // But all should still have testcase elements with properties
74+ assert .Contains (t , output , `classname="warn-rule"` )
75+ assert .Contains (t , output , `classname="info-rule"` )
76+ assert .Contains (t , output , `classname="hint-rule"` )
77+ }
78+
79+ func TestBuildJUnitReportWithConfig_FailOnWarn (t * testing.T ) {
80+ // Create results with different severities
81+ results := []model.RuleFunctionResult {
82+ {
83+ Rule : & model.Rule {Id : "error-rule" , Severity : model .SeverityError , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
84+ Message : "This is an error" ,
85+ StartNode : & yaml.Node {Line : 1 , Column : 1 },
86+ EndNode : & yaml.Node {Line : 1 , Column : 10 },
87+ },
88+ {
89+ Rule : & model.Rule {Id : "warn-rule" , Severity : model .SeverityWarn , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
90+ Message : "This is a warning" ,
91+ StartNode : & yaml.Node {Line : 2 , Column : 1 },
92+ EndNode : & yaml.Node {Line : 2 , Column : 10 },
93+ },
94+ {
95+ Rule : & model.Rule {Id : "info-rule" , Severity : model .SeverityInfo , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
96+ Message : "This is info" ,
97+ StartNode : & yaml.Node {Line : 3 , Column : 1 },
98+ EndNode : & yaml.Node {Line : 3 , Column : 10 },
99+ },
100+ }
101+
102+ resultSet := model .NewRuleResultSet (results )
103+ config := JUnitConfig {FailOnWarn : true } // Warnings are also failures
104+
105+ data := BuildJUnitReportWithConfig (resultSet , time .Now (), []string {"test.yaml" }, config )
106+ output := string (data )
107+
108+ // Should have 3 tests and 2 failures (error + warning)
109+ assert .Contains (t , output , `tests="3"` )
110+ assert .Contains (t , output , `failures="2"` )
111+
112+ // Error and warning should have failure elements
113+ assert .Contains (t , output , `<failure message="This is an error" type="ERROR"` )
114+ assert .Contains (t , output , `<failure message="This is a warning" type="WARN"` )
115+
116+ // Info should NOT have failure element
117+ assert .NotContains (t , output , `<failure message="This is info"` )
118+ }
119+
120+ func TestBuildJUnitReportWithConfig_InfoAndHintNeverFailures (t * testing.T ) {
121+ // Even with FailOnWarn, info and hint should never be failures
122+ results := []model.RuleFunctionResult {
123+ {
124+ Rule : & model.Rule {Id : "info-rule" , Severity : model .SeverityInfo , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
125+ Message : "This is info" ,
126+ StartNode : & yaml.Node {Line : 1 , Column : 1 },
127+ EndNode : & yaml.Node {Line : 1 , Column : 10 },
128+ },
129+ {
130+ Rule : & model.Rule {Id : "hint-rule" , Severity : model .SeverityHint , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
131+ Message : "This is a hint" ,
132+ StartNode : & yaml.Node {Line : 2 , Column : 1 },
133+ EndNode : & yaml.Node {Line : 2 , Column : 10 },
134+ },
135+ }
136+
137+ resultSet := model .NewRuleResultSet (results )
138+ config := JUnitConfig {FailOnWarn : true } // Even with this on, info/hint should not be failures
139+
140+ data := BuildJUnitReportWithConfig (resultSet , time .Now (), []string {"test.yaml" }, config )
141+ output := string (data )
142+
143+ // Should have 2 tests and 0 failures
144+ assert .Contains (t , output , `tests="2"` )
145+ assert .Contains (t , output , `failures="0"` )
146+
147+ // Neither should have failure elements
148+ failureCount := strings .Count (output , "<failure" )
149+ assert .Equal (t , 0 , failureCount , "Info and hint should never create failure elements" )
150+ }
151+
152+ func TestBuildJUnitReport_BackwardsCompatibility (t * testing.T ) {
153+ // The original BuildJUnitReport function should maintain backwards compatibility
154+ // by treating warnings as failures (FailOnWarn: true)
155+ results := []model.RuleFunctionResult {
156+ {
157+ Rule : & model.Rule {Id : "warn-rule" , Severity : model .SeverityWarn , RuleCategory : model .RuleCategories [model .CategoryInfo ]},
158+ Message : "This is a warning" ,
159+ StartNode : & yaml.Node {Line : 1 , Column : 1 },
160+ EndNode : & yaml.Node {Line : 1 , Column : 10 },
161+ },
162+ }
163+
164+ resultSet := model .NewRuleResultSet (results )
165+ data := BuildJUnitReport (resultSet , time .Now (), []string {"test.yaml" })
166+ output := string (data )
167+
168+ // Original function should still treat warnings as failures for backwards compatibility
169+ assert .Contains (t , output , `failures="1"` )
170+ assert .Contains (t , output , `<failure message="This is a warning" type="WARN"` )
171+ }
0 commit comments