Skip to content

Commit ffc0aeb

Browse files
fix: unwrap LocalConfigField at UserGlobalKey in resolveMachine/resolveFolder (#570)
1 parent a9d4efc commit ffc0aeb

File tree

2 files changed

+118
-2
lines changed

2 files changed

+118
-2
lines changed

pkg/configuration/configresolver/resolver.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ func (r *Resolver) resolveMachine(name, _ string) (any, ConfigSource) {
137137

138138
ugk := UserGlobalKey(name)
139139
if r.isUserSet(ugk) {
140-
return r.conf.Get(ugk), ConfigSourceUserGlobal
140+
if lf := r.localField(ugk); lf != nil {
141+
if lf.Changed {
142+
return lf.Value, ConfigSourceUserGlobal
143+
}
144+
} else {
145+
return r.conf.Get(ugk), ConfigSourceUserGlobal
146+
}
141147
}
142148

143149
if remote != nil {
@@ -175,7 +181,13 @@ func (r *Resolver) resolveFolder(name, effectiveOrg, folderPath string) (any, Co
175181

176182
ugk := UserGlobalKey(name)
177183
if r.isUserSet(ugk) {
178-
return r.conf.Get(ugk), ConfigSourceUserGlobal
184+
if lf := r.localField(ugk); lf != nil {
185+
if lf.Changed {
186+
return lf.Value, ConfigSourceUserGlobal
187+
}
188+
} else {
189+
return r.conf.Get(ugk), ConfigSourceUserGlobal
190+
}
179191
}
180192

181193
if remoteOrg != nil {

pkg/configuration/configresolver/resolver_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,110 @@ func TestResolve_LocalFieldNilValue(t *testing.T) {
602602
assert.Nil(t, val)
603603
}
604604

605+
func TestResolve_UserGlobalAsLocalConfigField(t *testing.T) {
606+
tests := []struct {
607+
name string
608+
flagName string
609+
setup func(conf configuration.Configuration)
610+
org string
611+
folder string
612+
wantVal any
613+
wantSource configresolver.ConfigSource
614+
}{
615+
{
616+
name: "machine scope: LocalConfigField Changed=true returns Value",
617+
flagName: "api_endpoint",
618+
setup: func(conf configuration.Configuration) {
619+
conf.Set(configresolver.UserGlobalKey("api_endpoint"), &configresolver.LocalConfigField{Value: "user-ep", Changed: true})
620+
},
621+
org: "org1",
622+
folder: "",
623+
wantVal: "user-ep",
624+
wantSource: configresolver.ConfigSourceUserGlobal,
625+
},
626+
{
627+
name: "machine scope: LocalConfigField Changed=false falls through to default",
628+
flagName: "api_endpoint",
629+
setup: func(conf configuration.Configuration) {
630+
conf.Set(configresolver.UserGlobalKey("api_endpoint"), &configresolver.LocalConfigField{Value: "ignored", Changed: false})
631+
},
632+
org: "org1",
633+
folder: "",
634+
wantVal: nil,
635+
wantSource: configresolver.ConfigSourceDefault,
636+
},
637+
{
638+
name: "machine scope: LocalConfigField Changed=false falls through to remote",
639+
flagName: "api_endpoint",
640+
setup: func(conf configuration.Configuration) {
641+
conf.Set(configresolver.UserGlobalKey("api_endpoint"), &configresolver.LocalConfigField{Value: "ignored", Changed: false})
642+
conf.Set(configresolver.RemoteMachineKey("api_endpoint"), &configresolver.RemoteConfigField{Value: "remote-ep", IsLocked: false})
643+
},
644+
org: "org1",
645+
folder: "",
646+
wantVal: "remote-ep",
647+
wantSource: configresolver.ConfigSourceRemote,
648+
},
649+
{
650+
name: "folder scope: LocalConfigField Changed=true returns Value",
651+
flagName: "snyk_code_enabled",
652+
setup: func(conf configuration.Configuration) {
653+
conf.Set(configresolver.UserGlobalKey("snyk_code_enabled"), &configresolver.LocalConfigField{Value: true, Changed: true})
654+
},
655+
org: "org1",
656+
folder: "/proj",
657+
wantVal: true,
658+
wantSource: configresolver.ConfigSourceUserGlobal,
659+
},
660+
{
661+
name: "folder scope: LocalConfigField Changed=false falls through to default",
662+
flagName: "snyk_code_enabled",
663+
setup: func(conf configuration.Configuration) {
664+
conf.Set(configresolver.UserGlobalKey("snyk_code_enabled"), &configresolver.LocalConfigField{Value: true, Changed: false})
665+
},
666+
org: "org1",
667+
folder: "/proj",
668+
wantVal: nil,
669+
wantSource: configresolver.ConfigSourceDefault,
670+
},
671+
{
672+
name: "folder scope: LocalConfigField Changed=false falls through to remote org",
673+
flagName: "snyk_code_enabled",
674+
setup: func(conf configuration.Configuration) {
675+
conf.Set(configresolver.UserGlobalKey("snyk_code_enabled"), &configresolver.LocalConfigField{Value: true, Changed: false})
676+
conf.Set(configresolver.RemoteOrgKey("org1", "snyk_code_enabled"), &configresolver.RemoteConfigField{Value: false, IsLocked: false})
677+
},
678+
org: "org1",
679+
folder: "/proj",
680+
wantVal: false,
681+
wantSource: configresolver.ConfigSourceRemote,
682+
},
683+
}
684+
685+
for _, tc := range tests {
686+
t.Run(tc.name, func(t *testing.T) {
687+
fs := newTestFlagSet()
688+
conf := configuration.NewWithOpts()
689+
tc.setup(conf)
690+
r := newResolver(conf, fs)
691+
692+
val, src := r.Resolve(tc.flagName, tc.org, tc.folder)
693+
assert.Equal(t, tc.wantVal, val)
694+
assert.Equal(t, tc.wantSource, src)
695+
})
696+
}
697+
}
698+
699+
func TestResolveBool_UserGlobalAsLocalConfigField(t *testing.T) {
700+
fs := newTestFlagSet()
701+
conf := configuration.NewWithOpts()
702+
name := "snyk_code_enabled"
703+
conf.Set(configresolver.UserGlobalKey(name), &configresolver.LocalConfigField{Value: true, Changed: true})
704+
r := newResolver(conf, fs)
705+
got := r.ResolveBool(name, "org1", "/proj")
706+
assert.True(t, got, "ResolveBool must unwrap LocalConfigField.Value, not see the struct pointer")
707+
}
708+
605709
func TestResolve_UserGlobalKeyDeleted(t *testing.T) {
606710
fs := newTestFlagSet()
607711
conf := configuration.NewWithOpts()

0 commit comments

Comments
 (0)