Unverified Commit af245809 authored by Bryan Liles's avatar Bryan Liles Committed by GitHub
Browse files

Merge pull request #818 from bryanl/746-ks-env-current-requires-valid-env

require env to exist before making it current
parents c15cffba f78b9cb5
......@@ -20,6 +20,7 @@ import (
"sync"
"github.com/ghodss/yaml"
stringutils "github.com/ksonnet/ksonnet/pkg/util/strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
......@@ -62,6 +63,20 @@ func (ba *baseApp) CurrentEnvironment() string {
}
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)
return afero.WriteFile(ba.fs, currentPath, []byte(name), DefaultFilePermissions)
}
......
......@@ -60,15 +60,41 @@ func Test_baseapp_CurrentEnvironment(t *testing.T) {
}
func Test_baseapp_SetCurrentEnvironment(t *testing.T) {
fs := afero.NewMemMapFs()
stageFile(t, fs, "app010_app.yaml", "/app.yaml")
ba := newBaseApp(fs, "/")
cases := []struct {
name string
envName string
isErr bool
}{
{
name: "environment exists",
envName: "default",
},
{
name: "environment does not exist",
envName: "invalid",
isErr: true,
},
}
err := ba.SetCurrentEnvironment("default")
require.NoError(t, err)
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
fs := afero.NewMemMapFs()
stageFile(t, fs, "app010_app.yaml", "/app.yaml")
ba := newBaseApp(fs, "/")
err := ba.SetCurrentEnvironment(tc.envName)
if tc.isErr {
require.Error(t, err)
return
}
require.NoError(t, err)
current := ba.CurrentEnvironment()
assert.Equal(t, tc.envName, current)
})
}
current := ba.CurrentEnvironment()
assert.Equal(t, "default", current)
}
func Test_baseApp_AddRegistry(t *testing.T) {
......@@ -164,8 +190,6 @@ func Test_baseApp_UpdateRegistry(t *testing.T) {
}
func Test_baseApp_UpdateLibrary(t *testing.T) {
//func (ba *baseApp) UpdateLib(name string, env string, libSpec *LibraryConfig) error
tests := []struct {
name string
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