-
Notifications
You must be signed in to change notification settings - Fork 399
/
collect_test.go
222 lines (176 loc) · 8.35 KB
/
collect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package markers_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "sigs.k8s.io/controller-tools/pkg/markers"
)
type fieldPath struct {
typ string
field string
}
var _ = Describe("Collecting", func() {
var col *Collector
var markersByType map[string]MarkerValues
var docsByType map[string]string
var markersByField map[fieldPath]MarkerValues
var docsByField map[fieldPath]string
JustBeforeEach(func() {
By("setting up the registry and collector")
reg := &Registry{}
mustDefine(reg, "testing:pkglvl", DescribesPackage, "")
mustDefine(reg, "testing:typelvl", DescribesType, "")
mustDefine(reg, "testing:fieldlvl", DescribesField, "")
mustDefine(reg, "testing:eitherlvl", DescribesType, "")
mustDefine(reg, "testing:eitherlvl", DescribesPackage, "")
col = &Collector{Registry: reg}
By("gathering markers/docs by type/field name")
markersByType = make(map[string]MarkerValues)
docsByType = make(map[string]string)
markersByField = make(map[fieldPath]MarkerValues)
docsByField = make(map[fieldPath]string)
err := EachType(col, fakePkg, func(info *TypeInfo) {
markersByType[info.Name] = info.Markers
docsByType[info.Name] = info.Doc
for _, field := range info.Fields {
fieldPath := fieldPath{typ: info.Name, field: field.Name}
markersByField[fieldPath] = field.Markers
docsByField[fieldPath] = field.Doc
}
})
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
})
Context("of package-level markers", func() {
It("should consider markers anywhere not obviously type- or field-level as package-level", func() {
By("grabbing all package-level markers")
pkgMarkers, err := PackageMarkers(col, fakePkg)
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
By("checking that it only contains package-level markers")
Expect(pkgMarkers).NotTo(HaveKey("testing:typelvl"))
By("checking that it contains the right package-level markers")
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here unattached")))
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here at end after last node")))
By("checking that it doesn't contain any markers it's not supposed to")
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement(ContainSubstring("not here")))))
})
})
Context("of type-level markers", func() {
It("should not have package-level markers associated", func() {
Expect(markersByType).To(HaveKeyWithValue("Foo", Not(HaveKey("testing:pkglvl"))))
})
It("should not contain markers it's not supposed to", func() {
Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:typelvl", ContainElement(ContainSubstring("not here")))))
Expect(markersByType).NotTo(ContainElement(HaveKeyWithValue("testing:eitherlvl", ContainElement(ContainSubstring("not here")))))
})
Context("with godoc", func() {
It("should associate markers in godoc", func() {
Expect(markersByType).To(HaveKeyWithValue("Foo",
HaveKeyWithValue("testing:typelvl", ContainElement("here on type"))))
})
It("should have docs without markers", func() {
Expect(docsByType).To(HaveKeyWithValue("Foo", "normal godoc normal godoc"))
})
It("should associate markers in the closest non-godoc block", func() {
Expect(markersByType).To(HaveKeyWithValue("Foo",
HaveKeyWithValue("testing:typelvl", ContainElement("here before type"))))
})
It("should re-associate package-level markers from the closest non-godoc", func() {
By("grabbing package markers")
pkgMarkers, err := PackageMarkers(col, fakePkg)
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
By("checking that the marker got reassociated")
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated")))
})
})
Context("types with /*…*/-style comments", func() {
It("should have doc without extraneous spaces", func() {
Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces",
"This type of doc has spaces preserved in go-ast, but we'd like to trim them."))
})
It("should have doc without extraneous spaces, even over multiple lines", func() {
Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces2",
"This type of doc has spaces preserved in go-ast, but we'd like to trim them, especially when formatted like this."))
})
})
Context("without godoc", func() {
It("should associate markers in the closest block", func() {
Expect(markersByType).To(HaveKeyWithValue("Quux",
HaveKeyWithValue("testing:typelvl", ContainElement("here without godoc"))))
})
It("should re-associate package-level markers from the closest block", func() {
By("grabbing package markers")
pkgMarkers, err := PackageMarkers(col, fakePkg)
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
By("checking that the marker got reassociated")
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", ContainElement("here reassociated no godoc")))
})
})
It("should not re-associate package-level markers in godoc", func() {
By("grabbing package markers")
pkgMarkers, err := PackageMarkers(col, fakePkg)
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
By("checking that the marker didn't get reassociated")
Expect(pkgMarkers).To(HaveKeyWithValue("testing:pkglvl", Not(ContainElement("not here godoc"))))
})
It("should not re-associate package-level markers if there's also a type-level marker", func() {
By("checking that the type has the marker")
Expect(markersByType).To(HaveKeyWithValue("Foo",
HaveKeyWithValue("testing:eitherlvl", ContainElement("here not reassociated"))))
By("grabbing package markers")
pkgMarkers, err := PackageMarkers(col, fakePkg)
Expect(err).NotTo(HaveOccurred())
Expect(fakePkg.Errors).To(HaveLen(0))
By("checking that the marker didn't get reassociated to the package")
Expect(pkgMarkers).To(SatisfyAny(
Not(HaveKey("testing:eitherlvl")),
HaveKeyWithValue("testing:eitherlvl", Not(ContainElement("here not reassociated")))))
})
It("should consider markers on the gendecl even if there are no more markers in the file", func() {
Expect(markersByType).To(HaveKeyWithValue("Cheese",
HaveKeyWithValue("testing:typelvl", ContainElement("here on typedecl with no more"))))
})
})
Context("of field-level markers", func() {
It("should not contain markers it's not supposed to", func() {
Expect(markersByField).NotTo(ContainElement(HaveKeyWithValue("testing:fieldlvl", ContainElement(ContainSubstring("not here")))))
})
Context("with godoc", func() {
It("should associate markers in godoc", func() {
Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
HaveKeyWithValue("testing:fieldlvl", ContainElement("here in godoc"))))
})
It("should associate markers in the closest non-godoc block", func() {
Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
HaveKeyWithValue("testing:fieldlvl", ContainElement("here before godoc"))))
})
})
Context("without godoc", func() {
It("should associate markers in the closest block", func() {
Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
HaveKeyWithValue("testing:fieldlvl", ContainElement("here without godoc"))))
})
})
It("should not consider markers at the end of a line", func() {
Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithGodoc"},
HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
Expect(markersByField).To(HaveKeyWithValue(fieldPath{typ: "Foo", field: "WithoutGodoc"},
HaveKeyWithValue("testing:fieldlvl", Not(ContainElement("not here after field")))))
})
})
})