Unverified Commit f78b9cb5 authored by bryanl's avatar bryanl
Browse files

require env to exist before making it current


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent c15cffba
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"sync" "sync"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
stringutils "github.com/ksonnet/ksonnet/pkg/util/strings"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/afero" "github.com/spf13/afero"
...@@ -62,6 +63,20 @@ func (ba *baseApp) CurrentEnvironment() string { ...@@ -62,6 +63,20 @@ func (ba *baseApp) CurrentEnvironment() string {
} }
func (ba *baseApp) SetCurrentEnvironment(name string) error { func (ba *baseApp) SetCurrentEnvironment(name string) error {
envs, err := ba.Environments()
if err != nil {
return errors.Wrap(err, "loading environments")
}
var envNames []string
for _, env := range envs {
envNames = append(envNames, env.Name)
}
if !stringutils.InSlice(name, envNames) {
return errors.Errorf("environment %q does not exist", name)
}
currentPath := filepath.Join(ba.root, currentEnvName) currentPath := filepath.Join(ba.root, currentEnvName)
return afero.WriteFile(ba.fs, currentPath, []byte(name), DefaultFilePermissions) return afero.WriteFile(ba.fs, currentPath, []byte(name), DefaultFilePermissions)
} }
......
...@@ -60,15 +60,41 @@ func Test_baseapp_CurrentEnvironment(t *testing.T) { ...@@ -60,15 +60,41 @@ func Test_baseapp_CurrentEnvironment(t *testing.T) {
} }
func Test_baseapp_SetCurrentEnvironment(t *testing.T) { func Test_baseapp_SetCurrentEnvironment(t *testing.T) {
cases := []struct {
name string
envName string
isErr bool
}{
{
name: "environment exists",
envName: "default",
},
{
name: "environment does not exist",
envName: "invalid",
isErr: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
fs := afero.NewMemMapFs() fs := afero.NewMemMapFs()
stageFile(t, fs, "app010_app.yaml", "/app.yaml") stageFile(t, fs, "app010_app.yaml", "/app.yaml")
ba := newBaseApp(fs, "/") ba := newBaseApp(fs, "/")
err := ba.SetCurrentEnvironment("default") err := ba.SetCurrentEnvironment(tc.envName)
if tc.isErr {
require.Error(t, err)
return
}
require.NoError(t, err) require.NoError(t, err)
current := ba.CurrentEnvironment() current := ba.CurrentEnvironment()
assert.Equal(t, "default", current) assert.Equal(t, tc.envName, current)
})
}
} }
func Test_baseApp_AddRegistry(t *testing.T) { func Test_baseApp_AddRegistry(t *testing.T) {
...@@ -164,8 +190,6 @@ func Test_baseApp_UpdateRegistry(t *testing.T) { ...@@ -164,8 +190,6 @@ func Test_baseApp_UpdateRegistry(t *testing.T) {
} }
func Test_baseApp_UpdateLibrary(t *testing.T) { func Test_baseApp_UpdateLibrary(t *testing.T) {
//func (ba *baseApp) UpdateLib(name string, env string, libSpec *LibraryConfig) error
tests := []struct { tests := []struct {
name string name string
libCfg LibraryConfig libCfg LibraryConfig
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment