@@ -45,14 +45,15 @@ var _ = Describe("Cfg", func() {
4545 )
4646
4747 var (
48- c Cfg
48+ c Cfg
49+ pluginChain []string
50+ otherPluginChain []string
51+ )
4952
53+ BeforeEach (func () {
5054 pluginChain = []string {"go.kubebuilder.io/v2" }
51-
5255 otherPluginChain = []string {"go.kubebuilder.io/v3" }
53- )
5456
55- BeforeEach (func () {
5657 c = Cfg {
5758 Version : Version ,
5859 Domain : domain ,
@@ -136,6 +137,12 @@ var _ = Describe("Cfg", func() {
136137
137138 Context ("Resources" , func () {
138139 var (
140+ res resource.Resource
141+ resWithoutPlural resource.Resource
142+ checkResource func (result , expected resource.Resource )
143+ )
144+
145+ BeforeEach (func () {
139146 res = resource.Resource {
140147 GVK : resource.GVK {
141148 Group : "group" ,
@@ -157,35 +164,35 @@ var _ = Describe("Cfg", func() {
157164 },
158165 }
159166 resWithoutPlural = res .Copy ()
160- )
161167
162- // As some of the tests insert directly into the slice without using the interface methods,
163- // regular plural forms should not be present in here. rsWithoutPlural is used for this purpose.
164- resWithoutPlural .Plural = ""
165-
166- // Auxiliary function for GetResource, AddResource and UpdateResource tests
167- checkResource := func (result , expected resource.Resource ) {
168- Expect (result .GVK .IsEqualTo (expected .GVK )).To (BeTrue ())
169- Expect (result .Plural ).To (Equal (expected .Plural ))
170- Expect (result .Path ).To (Equal (expected .Path ))
171- if expected .API == nil {
172- Expect (result .API ).To (BeNil ())
173- } else {
174- Expect (result .API ).NotTo (BeNil ())
175- Expect (result .API .CRDVersion ).To (Equal (expected .API .CRDVersion ))
176- Expect (result .API .Namespaced ).To (Equal (expected .API .Namespaced ))
177- }
178- Expect (result .Controller ).To (Equal (expected .Controller ))
179- if expected .Webhooks == nil {
180- Expect (result .Webhooks ).To (BeNil ())
181- } else {
182- Expect (result .Webhooks ).NotTo (BeNil ())
183- Expect (result .Webhooks .WebhookVersion ).To (Equal (expected .Webhooks .WebhookVersion ))
184- Expect (result .Webhooks .Defaulting ).To (Equal (expected .Webhooks .Defaulting ))
185- Expect (result .Webhooks .Validation ).To (Equal (expected .Webhooks .Validation ))
186- Expect (result .Webhooks .Conversion ).To (Equal (expected .Webhooks .Conversion ))
168+ // As some of the tests insert directly into the slice without using the interface methods,
169+ // regular plural forms should not be present in here. rsWithoutPlural is used for this purpose.
170+ resWithoutPlural .Plural = ""
171+
172+ // Auxiliary function for GetResource, AddResource and UpdateResource tests
173+ checkResource = func (result , expected resource.Resource ) {
174+ Expect (result .GVK .IsEqualTo (expected .GVK )).To (BeTrue ())
175+ Expect (result .Plural ).To (Equal (expected .Plural ))
176+ Expect (result .Path ).To (Equal (expected .Path ))
177+ if expected .API == nil {
178+ Expect (result .API ).To (BeNil ())
179+ } else {
180+ Expect (result .API ).NotTo (BeNil ())
181+ Expect (result .API .CRDVersion ).To (Equal (expected .API .CRDVersion ))
182+ Expect (result .API .Namespaced ).To (Equal (expected .API .Namespaced ))
183+ }
184+ Expect (result .Controller ).To (Equal (expected .Controller ))
185+ if expected .Webhooks == nil {
186+ Expect (result .Webhooks ).To (BeNil ())
187+ } else {
188+ Expect (result .Webhooks ).NotTo (BeNil ())
189+ Expect (result .Webhooks .WebhookVersion ).To (Equal (expected .Webhooks .WebhookVersion ))
190+ Expect (result .Webhooks .Defaulting ).To (Equal (expected .Webhooks .Defaulting ))
191+ Expect (result .Webhooks .Validation ).To (Equal (expected .Webhooks .Validation ))
192+ Expect (result .Webhooks .Conversion ).To (Equal (expected .Webhooks .Conversion ))
193+ }
187194 }
188- }
195+ })
189196
190197 DescribeTable ("ResourcesLength should return the number of resources" ,
191198 func (n int ) {
@@ -354,6 +361,11 @@ var _ = Describe("Cfg", func() {
354361 )
355362
356363 var (
364+ c0 , c1 , c2 Cfg
365+ pluginCfg PluginConfig
366+ )
367+
368+ BeforeEach (func () {
357369 c0 = Cfg {
358370 Version : Version ,
359371 Domain : domain ,
@@ -386,11 +398,11 @@ var _ = Describe("Cfg", func() {
386398 },
387399 },
388400 }
389- pluginConfig = PluginConfig {
401+ pluginCfg = PluginConfig {
390402 Data1 : "plugin value 1" ,
391403 Data2 : "plugin value 2" ,
392404 }
393- )
405+ } )
394406
395407 It ("DecodePluginConfig should fail for no plugin config object" , func () {
396408 var pluginCfg PluginConfig
@@ -407,32 +419,36 @@ var _ = Describe("Cfg", func() {
407419 })
408420
409421 DescribeTable ("DecodePluginConfig should retrieve the plugin data correctly" ,
410- func (inputConfig Cfg , expectedPluginConfig PluginConfig ) {
422+ func (getCfg func () Cfg , expected func () PluginConfig ) {
411423 var pluginCfg PluginConfig
412- Expect (inputConfig .DecodePluginConfig (key , & pluginCfg )).To (Succeed ())
413- Expect (pluginCfg ).To (Equal (expectedPluginConfig ))
424+ Expect (getCfg () .DecodePluginConfig (key , & pluginCfg )).To (Succeed ())
425+ Expect (pluginCfg ).To (Equal (expected () ))
414426 },
415- Entry ("for an empty plugin config object" , c1 , PluginConfig { }),
416- Entry ("for a full plugin config object" , c2 , pluginConfig ),
427+ Entry ("for an empty plugin config object" , func () Cfg { return c1 }, func () PluginConfig { return PluginConfig {} }),
428+ Entry ("for a full plugin config object" , func () Cfg { return c2 }, func () PluginConfig { return pluginCfg } ),
417429 // TODO (coverage): add cases where yaml.Marshal returns an error
418430 // TODO (coverage): add cases where yaml.Unmarshal returns an error
419431 )
420432
421433 DescribeTable ("EncodePluginConfig should encode the plugin data correctly" ,
422- func (pluginConfig PluginConfig , expectedConfig Cfg ) {
423- Expect (c .EncodePluginConfig (key , pluginConfig )).To (Succeed ())
424- Expect (c ).To (Equal (expectedConfig ))
434+ func (getPluginCfg func () PluginConfig , expectedCfg func () Cfg ) {
435+ Expect (c .EncodePluginConfig (key , getPluginCfg () )).To (Succeed ())
436+ Expect (c ).To (Equal (expectedCfg () ))
425437 },
426- Entry ("for an empty plugin config object" , PluginConfig {}, c1 ),
427- Entry ("for a full plugin config object" , pluginConfig , c2 ),
438+ Entry ("for an empty plugin config object" , func () PluginConfig { return PluginConfig {} }, func () Cfg { return c1 } ),
439+ Entry ("for a full plugin config object" , func () PluginConfig { return pluginCfg }, func () Cfg { return c2 } ),
428440 // TODO (coverage): add cases where yaml.Marshal returns an error
429441 // TODO (coverage): add cases where yaml.Unmarshal returns an error
430442 )
431443 })
432444
433445 Context ("Persistence" , func () {
434446 var (
435- // BeforeEach is called after the entries are evaluated, and therefore, c is not available
447+ c1 , c2 Cfg
448+ s1 , s1bis , s2 string
449+ )
450+
451+ BeforeEach (func () {
436452 c1 = Cfg {
437453 Version : Version ,
438454 Domain : domain ,
@@ -472,8 +488,8 @@ var _ = Describe("Cfg", func() {
472488 Kind : "Kind" ,
473489 },
474490 Plural : "kindes" ,
475- API : & resource. API {} ,
476- Webhooks : & resource. Webhooks {} ,
491+ API : nil ,
492+ Webhooks : nil ,
477493 },
478494 {
479495 GVK : resource.GVK {
@@ -564,22 +580,23 @@ resources:
564580 webhookVersion: v1
565581version: "3"
566582`
567- )
583+ } )
568584
569585 DescribeTable ("MarshalYAML should succeed" ,
570- func (c Cfg , content string ) {
571- b , err := c .MarshalYAML ()
586+ func (getCfg func () Cfg , getContent func () string ) {
587+ b , err := getCfg () .MarshalYAML ()
572588 Expect (err ).NotTo (HaveOccurred ())
573- Expect (string (b )).To (Equal (content ))
589+ Expect (string (b )).To (Equal (getContent () ))
574590 },
575- Entry ("for a basic configuration" , c1 , s1 ),
576- Entry ("for a full configuration" , c2 , s2 ),
591+ Entry ("for a basic configuration" , func () Cfg { return c1 }, func () string { return s1 } ),
592+ Entry ("for a full configuration" , func () Cfg { return c2 }, func () string { return s2 } ),
577593 )
578594
579595 DescribeTable ("UnmarshalYAML should succeed" ,
580- func (content string , c Cfg ) {
596+ func (getContent func () string , getCfg func () Cfg ) {
581597 var unmarshalled Cfg
582- Expect (unmarshalled .UnmarshalYAML ([]byte (content ))).To (Succeed ())
598+ Expect (unmarshalled .UnmarshalYAML ([]byte (getContent ()))).To (Succeed ())
599+ c := getCfg ()
583600 Expect (unmarshalled .Version .Compare (c .Version )).To (Equal (0 ))
584601 Expect (unmarshalled .Domain ).To (Equal (c .Domain ))
585602 Expect (unmarshalled .Repository ).To (Equal (c .Repository ))
@@ -590,9 +607,9 @@ version: "3"
590607 Expect (unmarshalled .Plugins ).To (HaveLen (len (c .Plugins )))
591608 // TODO: fully test Plugins field and not on its length
592609 },
593- Entry ("basic" , s1 , c1 ),
594- Entry ("full" , s2 , c2 ),
595- Entry ("string layout" , s1bis , c1 ),
610+ Entry ("basic" , func () string { return s1 }, func () Cfg { return c1 } ),
611+ Entry ("full" , func () string { return s2 }, func () Cfg { return c2 } ),
612+ Entry ("string layout" , func () string { return s1bis }, func () Cfg { return c1 } ),
596613 )
597614
598615 DescribeTable ("UnmarshalYAML should fail" ,
0 commit comments