@@ -33,17 +33,16 @@ var _ = Describe("Loader parsing root module", func() {
33
33
testmodPkg = loaderPkg + "/testmod"
34
34
)
35
35
36
- var indexOfPackage = func (pkgID string , pkgs []* loader.Package ) int {
37
- for i := range pkgs {
38
- if pkgs [i ].ID == pkgID {
39
- return i
40
- }
41
- }
42
- return - 1
36
+ var assertPkgExists = func (pkgID string , pkgs map [string ]struct {}) {
37
+ Expect (pkgs ).Should (HaveKey (pkgID ))
43
38
}
44
39
45
- var assertPkgExists = func (pkgID string , pkgs []* loader.Package ) {
46
- ExpectWithOffset (1 , indexOfPackage (pkgID , pkgs )).Should (BeNumerically (">" , - 1 ))
40
+ var dedupPkgs = func (pkgs []* loader.Package ) map [string ]struct {} {
41
+ uniquePkgs := make (map [string ]struct {})
42
+ for _ , p := range pkgs {
43
+ uniquePkgs [p .ID ] = struct {}{}
44
+ }
45
+ return uniquePkgs
47
46
}
48
47
49
48
Context ("with named packages/modules" , func () {
@@ -67,37 +66,40 @@ var _ = Describe("Loader parsing root module", func() {
67
66
It ("should load one package" , func () {
68
67
pkgs , err := loader .LoadRoots ("sigs.k8s.io/controller-tools/pkg/loader/testmod/submod1" )
69
68
Expect (err ).ToNot (HaveOccurred ())
70
- Expect (pkgs ).To (HaveLen (1 ))
71
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
69
+ uniquePkgs := dedupPkgs (pkgs )
70
+ Expect (uniquePkgs ).To (HaveLen (1 ))
71
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
72
72
})
73
73
})
74
74
75
75
Context ("with roots=[sigs.k8s.io/controller-tools/pkg/loader/testmod/...]" , func () {
76
76
It ("should load six packages" , func () {
77
77
pkgs , err := loader .LoadRoots ("sigs.k8s.io/controller-tools/pkg/loader/testmod/..." )
78
78
Expect (err ).ToNot (HaveOccurred ())
79
- Expect (pkgs ).To (HaveLen (6 ))
80
- assertPkgExists (testmodPkg , pkgs )
81
- assertPkgExists (testmodPkg + "/subdir1" , pkgs )
82
- assertPkgExists (testmodPkg + "/subdir1/subdir1" , pkgs )
83
- assertPkgExists (testmodPkg + "/subdir1/subdir2" , pkgs )
84
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
85
- assertPkgExists (testmodPkg + "/submod1/subdir1" , pkgs )
79
+ uniquePkgs := dedupPkgs (pkgs )
80
+ Expect (uniquePkgs ).To (HaveLen (6 ))
81
+ assertPkgExists (testmodPkg , uniquePkgs )
82
+ assertPkgExists (testmodPkg + "/subdir1" , uniquePkgs )
83
+ assertPkgExists (testmodPkg + "/subdir1/subdir1" , uniquePkgs )
84
+ assertPkgExists (testmodPkg + "/subdir1/subdir2" , uniquePkgs )
85
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
86
+ assertPkgExists (testmodPkg + "/submod1/subdir1" , uniquePkgs )
86
87
})
87
88
})
88
89
89
90
Context ("with roots=[sigs.k8s.io/controller-tools/pkg/loader/testmod/..., ./...]" , func () {
90
91
It ("should load seven packages" , func () {
91
92
pkgs , err := loader .LoadRoots ("sigs.k8s.io/controller-tools/pkg/loader/testmod/..." , "./..." )
92
93
Expect (err ).ToNot (HaveOccurred ())
93
- Expect (pkgs ).To (HaveLen (7 ))
94
- assertPkgExists (testmodPkg , pkgs )
95
- assertPkgExists (testmodPkg + "/subdir1" , pkgs )
96
- assertPkgExists (testmodPkg + "/subdir1/subdir1" , pkgs )
97
- assertPkgExists (testmodPkg + "/subdir1/subdir2" , pkgs )
98
- assertPkgExists (testmodPkg + "/subdir1/submod1" , pkgs )
99
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
100
- assertPkgExists (testmodPkg + "/submod1/subdir1" , pkgs )
94
+ uniquePkgs := dedupPkgs (pkgs )
95
+ Expect (uniquePkgs ).To (HaveLen (7 ))
96
+ assertPkgExists (testmodPkg , uniquePkgs )
97
+ assertPkgExists (testmodPkg + "/subdir1" , uniquePkgs )
98
+ assertPkgExists (testmodPkg + "/subdir1/subdir1" , uniquePkgs )
99
+ assertPkgExists (testmodPkg + "/subdir1/subdir2" , uniquePkgs )
100
+ assertPkgExists (testmodPkg + "/subdir1/submod1" , uniquePkgs )
101
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
102
+ assertPkgExists (testmodPkg + "/submod1/subdir1" , uniquePkgs )
101
103
})
102
104
})
103
105
})
@@ -106,26 +108,29 @@ var _ = Describe("Loader parsing root module", func() {
106
108
It ("should load one package" , func () {
107
109
pkgs , err := loader .LoadRoots ("../crd/." )
108
110
Expect (err ).ToNot (HaveOccurred ())
109
- Expect (pkgs ).To (HaveLen (1 ))
110
- assertPkgExists (pkgPkg + "/crd" , pkgs )
111
+ uniquePkgs := dedupPkgs (pkgs )
112
+ Expect (uniquePkgs ).To (HaveLen (1 ))
113
+ assertPkgExists (pkgPkg + "/crd" , uniquePkgs )
111
114
})
112
115
})
113
116
114
117
Context ("with roots=[./]" , func () {
115
118
It ("should load one package" , func () {
116
119
pkgs , err := loader .LoadRoots ("./" )
117
120
Expect (err ).ToNot (HaveOccurred ())
118
- Expect (pkgs ).To (HaveLen (1 ))
119
- assertPkgExists (loaderPkg , pkgs )
121
+ uniquePkgs := dedupPkgs (pkgs )
122
+ Expect (uniquePkgs ).To (HaveLen (1 ))
123
+ assertPkgExists (loaderPkg , uniquePkgs )
120
124
})
121
125
})
122
126
123
127
Context ("with roots=[../../pkg/loader]" , func () {
124
128
It ("should load one package" , func () {
125
129
pkgs , err := loader .LoadRoots ("../../pkg/loader" )
126
130
Expect (err ).ToNot (HaveOccurred ())
127
- Expect (pkgs ).To (HaveLen (1 ))
128
- assertPkgExists (loaderPkg , pkgs )
131
+ uniquePkgs := dedupPkgs (pkgs )
132
+ Expect (uniquePkgs ).To (HaveLen (1 ))
133
+ assertPkgExists (loaderPkg , uniquePkgs )
129
134
})
130
135
})
131
136
@@ -135,57 +140,62 @@ var _ = Describe("Loader parsing root module", func() {
135
140
"../../pkg/loader/../loader/testmod/..." ,
136
141
"./testmod/./../testmod//." )
137
142
Expect (err ).ToNot (HaveOccurred ())
138
- Expect (pkgs ).To (HaveLen (7 ))
139
- assertPkgExists (testmodPkg , pkgs )
140
- assertPkgExists (testmodPkg + "/subdir1" , pkgs )
141
- assertPkgExists (testmodPkg + "/subdir1/subdir1" , pkgs )
142
- assertPkgExists (testmodPkg + "/subdir1/subdir2" , pkgs )
143
- assertPkgExists (testmodPkg + "/subdir1/submod1" , pkgs )
144
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
145
- assertPkgExists (testmodPkg + "/submod1/subdir1" , pkgs )
143
+ uniquePkgs := dedupPkgs (pkgs )
144
+ Expect (uniquePkgs ).To (HaveLen (7 ))
145
+ assertPkgExists (testmodPkg , uniquePkgs )
146
+ assertPkgExists (testmodPkg + "/subdir1" , uniquePkgs )
147
+ assertPkgExists (testmodPkg + "/subdir1/subdir1" , uniquePkgs )
148
+ assertPkgExists (testmodPkg + "/subdir1/subdir2" , uniquePkgs )
149
+ assertPkgExists (testmodPkg + "/subdir1/submod1" , uniquePkgs )
150
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
151
+ assertPkgExists (testmodPkg + "/submod1/subdir1" , uniquePkgs )
146
152
})
147
153
})
148
154
149
155
Context ("with roots=[./testmod/...]" , func () {
150
156
It ("should load seven packages" , func () {
151
157
pkgs , err := loader .LoadRoots ("./testmod/..." )
152
158
Expect (err ).ToNot (HaveOccurred ())
153
- Expect (pkgs ).To (HaveLen (7 ))
154
- assertPkgExists (testmodPkg , pkgs )
155
- assertPkgExists (testmodPkg + "/subdir1" , pkgs )
156
- assertPkgExists (testmodPkg + "/subdir1/subdir1" , pkgs )
157
- assertPkgExists (testmodPkg + "/subdir1/subdir2" , pkgs )
158
- assertPkgExists (testmodPkg + "/subdir1/submod1" , pkgs )
159
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
160
- assertPkgExists (testmodPkg + "/submod1/subdir1" , pkgs )
159
+ uniquePkgs := dedupPkgs (pkgs )
160
+ Expect (uniquePkgs ).To (HaveLen (7 ))
161
+ assertPkgExists (testmodPkg , uniquePkgs )
162
+ assertPkgExists (testmodPkg + "/subdir1" , uniquePkgs )
163
+ assertPkgExists (testmodPkg + "/subdir1/subdir1" , uniquePkgs )
164
+ assertPkgExists (testmodPkg + "/subdir1/subdir2" , uniquePkgs )
165
+ assertPkgExists (testmodPkg + "/subdir1/submod1" , uniquePkgs )
166
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
167
+ assertPkgExists (testmodPkg + "/submod1/subdir1" , uniquePkgs )
161
168
})
162
169
})
163
170
164
171
Context ("with roots=[./testmod/subdir1/submod1/...]" , func () {
165
172
It ("should load one package" , func () {
166
173
pkgs , err := loader .LoadRoots ("./testmod/subdir1/submod1/..." )
167
174
Expect (err ).ToNot (HaveOccurred ())
168
- Expect (pkgs ).To (HaveLen (1 ))
169
- assertPkgExists (testmodPkg + "/subdir1/submod1" , pkgs )
175
+ uniquePkgs := dedupPkgs (pkgs )
176
+ Expect (uniquePkgs ).To (HaveLen (1 ))
177
+ assertPkgExists (testmodPkg + "/subdir1/submod1" , uniquePkgs )
170
178
})
171
179
})
172
180
173
181
Context ("with roots=[./testmod, ./testmod/submod1]" , func () {
174
182
It ("should load two packages" , func () {
175
183
pkgs , err := loader .LoadRoots ("./testmod" , "./testmod/submod1" )
176
184
Expect (err ).ToNot (HaveOccurred ())
177
- Expect (pkgs ).To (HaveLen (2 ))
178
- assertPkgExists (testmodPkg , pkgs )
179
- assertPkgExists (testmodPkg + "/submod1" , pkgs )
185
+ uniquePkgs := dedupPkgs (pkgs )
186
+ Expect (uniquePkgs ).To (HaveLen (2 ))
187
+ assertPkgExists (testmodPkg , uniquePkgs )
188
+ assertPkgExists (testmodPkg + "/submod1" , uniquePkgs )
180
189
})
181
190
})
182
191
183
192
Context ("with roots=[./testmod/submod1/subdir1/]" , func () {
184
193
It ("should load one package" , func () {
185
194
pkgs , err := loader .LoadRoots ("./testmod/submod1/subdir1/" )
186
195
Expect (err ).ToNot (HaveOccurred ())
187
- Expect (pkgs ).To (HaveLen (1 ))
188
- assertPkgExists (testmodPkg + "/submod1/subdir1" , pkgs )
196
+ uniquePkgs := dedupPkgs (pkgs )
197
+ Expect (uniquePkgs ).To (HaveLen (1 ))
198
+ assertPkgExists (testmodPkg + "/submod1/subdir1" , uniquePkgs )
189
199
})
190
200
})
191
201
})
0 commit comments