diff --git a/cmd/use/use.go b/cmd/use/use.go index 49485b40..815e422a 100644 --- a/cmd/use/use.go +++ b/cmd/use/use.go @@ -64,6 +64,7 @@ func useContext(path string, omcConfigFile string, idFlag string) error { NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "*", Project: c.Project}) configId = c.Id found = true + vars.Namespace = c.Project } else { NewContexts = append(NewContexts, types.Context{Id: c.Id, Path: c.Path, Current: "", Project: c.Project}) } @@ -80,8 +81,10 @@ func useContext(path string, omcConfigFile string, idFlag string) error { } if len(namespaces) == 1 { NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: namespaces[0]}) + vars.Namespace = namespaces[0] } else { NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: defaultProject}) + vars.Namespace = defaultProject } } diff --git a/cmd/use/use_test.go b/cmd/use/use_test.go index d36c9750..2ebd354a 100644 --- a/cmd/use/use_test.go +++ b/cmd/use/use_test.go @@ -2,12 +2,14 @@ package use import ( "encoding/json" + "errors" "io/ioutil" "os" "path/filepath" "testing" "github.com/gmeghnag/omc/types" + "github.com/gmeghnag/omc/vars" ) func TestUseContext(t *testing.T) { @@ -152,6 +154,9 @@ func TestUseContext(t *testing.T) { if !compareConfig(actualConfig, expectedConfig, tc.ignoreConfigID) { t.Errorf("Expected file content %+v, got %+v", expectedConfig, actualConfig) } + + // Ensure state matches the current path and project + checkContextConsistency(t, actualConfig) }) } } @@ -195,3 +200,36 @@ func compareConfig(actualConfig types.Config, expectedConfig types.Config, ignor // This line executes after all the condition checks. return compareContexts(actualConfig.Contexts, expectedConfig.Contexts, ignoreConfigID) } + +// check that what's stored in memory matches the currently selected context +func checkContextConsistency(t *testing.T, config types.Config) { + t.Helper() + c, err := currentContext(config.Contexts) + if err != nil { + t.Error(err) + } + + if c.Path != vars.MustGatherRootPath { + t.Errorf("in-memory path does not match current context. Want %q, got %q", + c.Path, + vars.MustGatherRootPath, + ) + } + + if c.Project != vars.Namespace { + t.Errorf("in-memory project does not match current context. Want %q, got %q", + c.Project, + vars.Namespace, + ) + } +} + +func currentContext(contexts []types.Context) (types.Context, error) { + for _, c := range contexts { + if c.Current == "*" { + return c, nil + } + } + + return types.Context{}, errors.New("could not find current context") +}