Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update in-memory namespace on use #172

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand All @@ -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
}
}

Expand Down
38 changes: 38 additions & 0 deletions cmd/use/use_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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")
}
Loading