@@ -107,6 +107,13 @@ func TestRunSecretSet_GlobalScopeWithoutComponentPreservesType(t *testing.T) {
107107 svc := newFakeSecretService ()
108108 svc .scopes = map [string ]secrets.Scope {"SHARED_TOKEN" : secrets .ScopeGlobal }
109109 installService (t , svc , nil )
110+ originalLoadService := loadServiceFn
111+ var loadedScope secretScope
112+ loadServiceFn = func (scope secretScope ) (secretService , error ) {
113+ loadedScope = scope
114+ return originalLoadService (scope )
115+ }
116+ t .Cleanup (func () { loadServiceFn = originalLoadService })
110117 overrideEnumerateScopes (t , []scopeEntry {
111118 {
112119 Stack : "dev" ,
@@ -122,75 +129,96 @@ func TestRunSecretSet_GlobalScopeWithoutComponentPreservesType(t *testing.T) {
122129 err := runSecretSubcommand (t , "set" , "SHARED_TOKEN=v1" , "--stack" , "dev" , "--type" , "helm" )
123130 require .NoError (t , err )
124131 require .Len (t , svc .setCalls , 1 )
132+ assert .Equal (t , "helm" , loadedScope .ComponentType )
125133}
126134
127135func TestFindGlobalSetContext (t * testing.T ) {
128- t .Run ("enumeration error" , func (t * testing.T ) {
129- sentinel := errors .New ("stack enumeration failed" )
130- overrideEnumerateScopes (t , nil , sentinel )
131-
132- _ , _ , err := findGlobalSetContext (secretScope {Stack : "dev" }, "SHARED_TOKEN" )
133- require .ErrorIs (t , err , errUtils .ErrRequiredFlagNotProvided )
134- })
135-
136- t .Run ("no matching declaration" , func (t * testing.T ) {
137- overrideEnumerateScopes (t , []scopeEntry {
138- {
139- Stack : "prod" ,
140- Component : "other-stack-service" ,
141- ComponentType : "helm" ,
142- Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
143- },
144- {
145- Stack : "dev" ,
146- Component : "other-type-service" ,
147- ComponentType : "terraform" ,
148- Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
149- },
150- {
151- Stack : "dev" ,
152- Component : "example-service" ,
153- ComponentType : "helm" ,
154- Section : secretDeclarationSection ("OTHER_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
136+ sharedSection := secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" })
137+ tests := []struct {
138+ name string
139+ entries []scopeEntry
140+ enumerationErr error
141+ scope secretScope
142+ expectedComponent string
143+ expectedType string
144+ expectedErr error
145+ }{
146+ {
147+ name : "enumeration error" ,
148+ enumerationErr : errors .New ("stack enumeration failed" ),
149+ scope : secretScope {Stack : "dev" },
150+ expectedErr : errUtils .ErrRequiredFlagNotProvided ,
151+ },
152+ {
153+ name : "no matching declaration" ,
154+ entries : []scopeEntry {
155+ {
156+ Stack : "prod" ,
157+ Component : "other-stack-service" ,
158+ ComponentType : "helm" ,
159+ Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
160+ },
161+ {
162+ Stack : "dev" ,
163+ Component : "other-type-service" ,
164+ ComponentType : "terraform" ,
165+ Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
166+ },
167+ {
168+ Stack : "dev" ,
169+ Component : "example-service" ,
170+ ComponentType : "helm" ,
171+ Section : secretDeclarationSection ("OTHER_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" }),
172+ },
155173 },
156- }, nil )
157-
158- _ , _ , err := findGlobalSetContext (secretScope {Stack : "dev" , ComponentType : "helm" }, "SHARED_TOKEN" )
159- require .ErrorIs (t , err , errUtils .ErrRequiredFlagNotProvided )
160- })
161-
162- t .Run ("inconsistent declarations" , func (t * testing.T ) {
163- overrideEnumerateScopes (t , []scopeEntry {
164- {
165- Stack : "dev" ,
166- Component : "example-service-a" ,
167- ComponentType : "helm" ,
168- Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets-a" , "scope" : "global" }),
174+ scope : secretScope {Stack : "dev" , ComponentType : "helm" },
175+ expectedErr : errUtils .ErrRequiredFlagNotProvided ,
176+ },
177+ {
178+ name : "inconsistent declarations" ,
179+ entries : []scopeEntry {
180+ {
181+ Stack : "dev" ,
182+ Component : "example-service-a" ,
183+ ComponentType : "helm" ,
184+ Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets-a" , "scope" : "global" }),
185+ },
186+ {
187+ Stack : "dev" ,
188+ Component : "example-service-b" ,
189+ ComponentType : "helm" ,
190+ Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets-b" , "scope" : "global" }),
191+ },
169192 },
170- {
171- Stack : "dev" ,
172- Component : "example-service-b" ,
173- ComponentType : "helm" ,
174- Section : secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets-b" , "scope" : "global" }),
193+ scope : secretScope {Stack : "dev" },
194+ expectedErr : errUtils .ErrRequiredFlagNotProvided ,
195+ },
196+ {
197+ name : "identical declarations select first component" ,
198+ entries : []scopeEntry {
199+ {Stack : "dev" , Component : "example-service-a" , ComponentType : "helm" , Section : sharedSection },
200+ {Stack : "dev" , Component : "example-service-b" , ComponentType : "helm" , Section : sharedSection },
175201 },
176- }, nil )
177-
178- _ , _ , err := findGlobalSetContext ( secretScope { Stack : "dev" }, "SHARED_TOKEN" )
179- require . ErrorIs ( t , err , errUtils . ErrRequiredFlagNotProvided )
180- })
202+ scope : secretScope { Stack : "dev" },
203+ expectedComponent : "example-service-a" ,
204+ expectedType : "helm" ,
205+ },
206+ }
181207
182- t .Run ("identical declarations select first component" , func (t * testing.T ) {
183- section := secretDeclarationSection ("SHARED_TOKEN" , map [string ]any {"store" : "example-secrets" , "scope" : "global" })
184- overrideEnumerateScopes (t , []scopeEntry {
185- {Stack : "dev" , Component : "example-service-a" , ComponentType : "helm" , Section : section },
186- {Stack : "dev" , Component : "example-service-b" , ComponentType : "helm" , Section : section },
187- }, nil )
188-
189- component , componentType , err := findGlobalSetContext (secretScope {Stack : "dev" }, "SHARED_TOKEN" )
190- require .NoError (t , err )
191- assert .Equal (t , "example-service-a" , component )
192- assert .Equal (t , "helm" , componentType )
193- })
208+ for _ , tt := range tests {
209+ t .Run (tt .name , func (t * testing.T ) {
210+ overrideEnumerateScopes (t , tt .entries , tt .enumerationErr )
211+
212+ component , componentType , err := findGlobalSetContext (tt .scope , "SHARED_TOKEN" )
213+ if tt .expectedErr != nil {
214+ require .ErrorIs (t , err , tt .expectedErr )
215+ return
216+ }
217+ require .NoError (t , err )
218+ assert .Equal (t , tt .expectedComponent , component )
219+ assert .Equal (t , tt .expectedType , componentType )
220+ })
221+ }
194222}
195223
196224func TestRunSecretSet_NonGlobalScopeStillRequiresComponent (t * testing.T ) {
0 commit comments