Skip to content

Commit f8cec65

Browse files
add better coverage to pkg/machinery/marker
To ensure that current code implementation is well tested and changes will not break it
1 parent 26c4b55 commit f8cec65

File tree

1 file changed

+105
-14
lines changed

1 file changed

+105
-14
lines changed

pkg/machinery/marker_test.go

Lines changed: 105 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
. "github.com/onsi/gomega"
2222
)
2323

24-
var _ = Describe("NerMarkerFor", func() {
24+
var _ = Describe("NewMarkerFor", func() {
2525
DescribeTable("should create valid markers for known extensions",
2626
func(path, comment string) { Expect(NewMarkerFor(path, "").comment).To(Equal(comment)) },
2727
Entry("for go files", "file.go", "//"),
@@ -39,8 +39,10 @@ var _ = Describe("Marker", func() {
3939
Context("String", func() {
4040
DescribeTable("should return the right string representation",
4141
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
42-
Entry("for go files", Marker{prefix: kbPrefix, comment: "//", value: "test"}, "// +kubebuilder:scaffold:test"),
43-
Entry("for yaml files", Marker{prefix: kbPrefix, comment: "#", value: "test"}, "# +kubebuilder:scaffold:test"),
42+
Entry("for go files", Marker{prefix: kbPrefix, comment: "//", value: "test"},
43+
"// +kubebuilder:scaffold:test"),
44+
Entry("for yaml files", Marker{prefix: kbPrefix, comment: "#", value: "test"},
45+
"# +kubebuilder:scaffold:test"),
4446
)
4547
})
4648
})
@@ -49,9 +51,81 @@ var _ = Describe("NewMarkerFor", func() {
4951
Context("String", func() {
5052
DescribeTable("should return the right string representation",
5153
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
52-
Entry("for yaml files", NewMarkerFor("test.yaml", "test"), "# +kubebuilder:scaffold:test"),
54+
Entry("for yaml files", NewMarkerFor("test.yaml", "test"),
55+
"# +kubebuilder:scaffold:test"),
56+
)
57+
})
58+
})
59+
60+
var _ = Describe("NewMarkerForImports", func() {
61+
Context("String", func() {
62+
DescribeTable("should return the correct string representation for import markers",
63+
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
64+
Entry("for go import marker", NewMarkerFor("test.go", "import \"my/package\""),
65+
"// +kubebuilder:scaffold:import \"my/package\""),
66+
Entry("for go import marker with alias", NewMarkerFor("test.go",
67+
"import alias \"my/package\""), "// +kubebuilder:scaffold:import alias \"my/package\""),
68+
Entry("for multiline go import marker", NewMarkerFor("test.go",
69+
"import (\n\"my/package\"\n)"), "// +kubebuilder:scaffold:import (\n\"my/package\"\n)"),
70+
Entry("for multiline go import marker with alias", NewMarkerFor("test.go",
71+
"import (\nalias \"my/package\"\n)"), "// +kubebuilder:scaffold:import (\nalias \"my/package\"\n)"),
72+
)
73+
})
74+
75+
It("should detect import in Go file", func() {
76+
line := "// +kubebuilder:scaffold:import \"my/package\""
77+
marker := NewMarkerFor("test.go", "import \"my/package\"")
78+
Expect(marker.EqualsLine(line)).To(BeTrue())
79+
})
80+
81+
It("should detect import with alias in Go file", func() {
82+
line := "// +kubebuilder:scaffold:import alias \"my/package\""
83+
marker := NewMarkerFor("test.go", "import alias \"my/package\"")
84+
Expect(marker.EqualsLine(line)).To(BeTrue())
85+
})
86+
87+
It("should detect multiline import in Go file", func() {
88+
line := "// +kubebuilder:scaffold:import (\n\"my/package\"\n)"
89+
marker := NewMarkerFor("test.go", "import (\n\"my/package\"\n)")
90+
Expect(marker.EqualsLine(line)).To(BeTrue())
91+
})
92+
93+
It("should detect multiline import with alias in Go file", func() {
94+
line := "// +kubebuilder:scaffold:import (\nalias \"my/package\"\n)"
95+
marker := NewMarkerFor("test.go",
96+
"import (\nalias \"my/package\"\n)")
97+
Expect(marker.EqualsLine(line)).To(BeTrue())
98+
})
99+
})
100+
101+
var _ = Describe("NewMarkerForImports with different formatting", func() {
102+
Context("String", func() {
103+
DescribeTable("should handle variations in spacing and formatting for import markers",
104+
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
105+
Entry("go import marker with extra spaces",
106+
NewMarkerFor("test.go", "import \"my/package\""),
107+
"// +kubebuilder:scaffold:import \"my/package\""),
108+
Entry("go import marker with spaces around alias",
109+
NewMarkerFor("test.go", "import alias \"my/package\""),
110+
"// +kubebuilder:scaffold:import alias \"my/package\""),
111+
Entry("go import marker with newline",
112+
NewMarkerFor("test.go", "import \n\"my/package\""),
113+
"// +kubebuilder:scaffold:import \n\"my/package\""),
53114
)
54115
})
116+
117+
It("should detect import with spaces in Go file", func() {
118+
line := "// +kubebuilder:scaffold:import \"my/package\""
119+
marker := NewMarkerFor("test.go", "import \"my/package\"")
120+
Expect(marker.EqualsLine(line)).To(BeTrue())
121+
})
122+
123+
It("should detect import with alias and spaces in Go file", func() {
124+
line := "// +kubebuilder:scaffold:import alias \"my/package\""
125+
marker := NewMarkerFor("test.go",
126+
"import alias \"my/package\"")
127+
Expect(marker.EqualsLine(line)).To(BeTrue())
128+
})
55129
})
56130

57131
var _ = Describe("NewMarkerWithPrefixFor", func() {
@@ -60,26 +134,43 @@ var _ = Describe("NewMarkerWithPrefixFor", func() {
60134
func(marker Marker, str string) { Expect(marker.String()).To(Equal(str)) },
61135

62136
Entry("for yaml files",
63-
NewMarkerWithPrefixFor("custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
137+
NewMarkerWithPrefixFor("custom:scaffold",
138+
"test.yaml", "test"), "# +custom:scaffold:test"),
64139
Entry("for yaml files",
65-
NewMarkerWithPrefixFor("+custom:scaffold", "test.yaml", "test"), "# +custom:scaffold:test"),
140+
NewMarkerWithPrefixFor("+custom:scaffold",
141+
"test.yaml", "test"), "# +custom:scaffold:test"),
66142
Entry("for yaml files",
67-
NewMarkerWithPrefixFor("custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
143+
NewMarkerWithPrefixFor("custom:scaffold:",
144+
"test.yaml", "test"), "# +custom:scaffold:test"),
68145
Entry("for yaml files",
69-
NewMarkerWithPrefixFor("+custom:scaffold:", "test.yaml", "test"), "# +custom:scaffold:test"),
146+
NewMarkerWithPrefixFor("+custom:scaffold:",
147+
"test.yaml", "test"), "# +custom:scaffold:test"),
70148
Entry("for yaml files",
71-
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.yaml", "test"), "# +custom:scaffold:test"),
149+
NewMarkerWithPrefixFor(" +custom:scaffold: ",
150+
"test.yaml", "test"), "# +custom:scaffold:test"),
72151

73152
Entry("for go files",
74-
NewMarkerWithPrefixFor("custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
153+
NewMarkerWithPrefixFor("custom:scaffold",
154+
"test.go", "test"), "// +custom:scaffold:test"),
75155
Entry("for go files",
76-
NewMarkerWithPrefixFor("+custom:scaffold", "test.go", "test"), "// +custom:scaffold:test"),
156+
NewMarkerWithPrefixFor("+custom:scaffold",
157+
"test.go", "test"), "// +custom:scaffold:test"),
77158
Entry("for go files",
78-
NewMarkerWithPrefixFor("custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
159+
NewMarkerWithPrefixFor("custom:scaffold:",
160+
"test.go", "test"), "// +custom:scaffold:test"),
79161
Entry("for go files",
80-
NewMarkerWithPrefixFor("+custom:scaffold:", "test.go", "test"), "// +custom:scaffold:test"),
162+
NewMarkerWithPrefixFor("+custom:scaffold:",
163+
"test.go", "test"), "// +custom:scaffold:test"),
81164
Entry("for go files",
82-
NewMarkerWithPrefixFor(" +custom:scaffold: ", "test.go", "test"), "// +custom:scaffold:test"),
165+
NewMarkerWithPrefixFor(" +custom:scaffold: ",
166+
"test.go", "test"), "// +custom:scaffold:test"),
83167
)
84168
})
85169
})
170+
171+
var _ = Describe("NewMarkerFor with unsupported extensions", func() {
172+
It("should panic for unsupported extensions", func() {
173+
Expect(func() { NewMarkerFor("file.txt", "test") }).To(Panic())
174+
Expect(func() { NewMarkerFor("file.md", "test") }).To(Panic())
175+
})
176+
})

0 commit comments

Comments
 (0)