Skip to content

Commit e954fbf

Browse files
committed
test: add unit tests for conflicting markers analyzer and custom configurations
1 parent 31fcd74 commit e954fbf

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package conflictingmarkers_test
17+
18+
import (
19+
"testing"
20+
21+
"golang.org/x/tools/go/analysis/analysistest"
22+
"sigs.k8s.io/kube-api-linter/pkg/analysis/conflictingmarkers"
23+
)
24+
25+
func TestConflictingMarkersAnalyzer(t *testing.T) {
26+
testdata := analysistest.TestData()
27+
28+
config := &conflictingmarkers.ConflictingMarkersConfig{
29+
Conflicts: []conflictingmarkers.ConflictSet{
30+
{
31+
Name: "test_conflict",
32+
Sets: [][]string{{"marker1", "marker2"}, {"marker3", "marker4"}},
33+
Description: "Test markers conflict with each other",
34+
},
35+
{
36+
Name: "three_way_conflict",
37+
Sets: [][]string{{"marker5", "marker6"}, {"marker7", "marker8"}, {"marker9", "marker10"}},
38+
Description: "Three-way conflict between marker sets",
39+
},
40+
},
41+
}
42+
43+
initializer := conflictingmarkers.Initializer()
44+
45+
analyzer, err := initializer.Init(config)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
analysistest.Run(t, testdata, analyzer, "a")
51+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package a
2+
3+
type TestStruct struct {
4+
// Valid field with only marker1
5+
// +marker1
6+
ValidMarker1Field string `json:"validMarker1Field"`
7+
8+
// Valid field with marker1 that has a value
9+
// +marker1:=someValue
10+
ValidMarker1WithValueField string `json:"validMarker1WithValueField"`
11+
12+
// Valid field with marker1 and marker2 (both in same conflict set)
13+
// +marker1
14+
// +marker2
15+
ValidMarker1And2Field string `json:"validMarker1And2Field"`
16+
17+
// Conflict: marker1 vs marker3 (both with values)
18+
// +marker1:=value1
19+
// +marker3:=value2
20+
ConflictWithValuesField string `json:"conflictWithValuesField"` // want "field ConflictWithValuesField has conflicting markers: test_conflict: \\{\\[marker1\\], \\[marker3\\]\\}. Test markers conflict with each other"
21+
22+
// Conflict: marker1 vs marker3 (mixed with and without values)
23+
// +marker1
24+
// +marker3:=someValue
25+
ConflictMixedField string `json:"conflictMixedField"` // want "field ConflictMixedField has conflicting markers: test_conflict: \\{\\[marker1\\], \\[marker3\\]\\}. Test markers conflict with each other"
26+
27+
// Multiple conflicts with multiple markers in each set:
28+
// +marker1
29+
// +marker2
30+
// +marker3
31+
// +marker4
32+
MultipleConflictsField string `json:"multipleConflictsField"` // want "field MultipleConflictsField has conflicting markers: test_conflict: \\{\\[marker1 marker2\\], \\[marker3 marker4\\]\\}. Test markers conflict with each other"
33+
34+
// Three-way conflict: marker5 vs marker7 vs marker9
35+
// +marker5
36+
// +marker7
37+
// +marker9
38+
ThreeWayConflictField string `json:"threeWayConflictField"` // want "field ThreeWayConflictField has conflicting markers: three_way_conflict: \\{\\[marker5\\], \\[marker7\\], \\[marker9\\]\\}. Three-way conflict between marker sets"
39+
40+
// Three-way conflict with values
41+
// +marker6:=value1
42+
// +marker8:=value2
43+
// +marker10:=value3
44+
ThreeWayConflictWithValuesField string `json:"threeWayConflictWithValuesField"` // want "field ThreeWayConflictWithValuesField has conflicting markers: three_way_conflict: \\{\\[marker6\\], \\[marker8\\], \\[marker10\\]\\}. Three-way conflict between marker sets"
45+
46+
// Valid field with markers from same set in three-way conflict
47+
// +marker5
48+
// +marker6
49+
ValidThreeWaySameSetField string `json:"validThreeWaySameSetField"`
50+
51+
// Three-way conflict with multiple markers from each set
52+
// +marker5
53+
// +marker6
54+
// +marker7
55+
// +marker8
56+
// +marker9
57+
// +marker10
58+
ThreeWayMultipleMarkersField string `json:"threeWayMultipleMarkersField"` // want "field ThreeWayMultipleMarkersField has conflicting markers: three_way_conflict: \\{\\[marker5 marker6\\], \\[marker7 marker8\\], \\[marker10 marker9\\]\\}. Three-way conflict between marker sets"
59+
60+
}

0 commit comments

Comments
 (0)