From 4dac986a9188fa26887ef57d23ed7a11886922e3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 28 Feb 2017 19:18:16 -0500 Subject: [PATCH] Local.StatePaths doesn't need to reutrn an error add a test to ensure we have consistent output --- backend/local/backend.go | 9 +++---- backend/local/backend_test.go | 44 ++++++++++++++++++++++++++++++++++- command/meta.go | 2 +- command/state_meta.go | 2 +- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/backend/local/backend.go b/backend/local/backend.go index 84965850b5..0b592e33d7 100644 --- a/backend/local/backend.go +++ b/backend/local/backend.go @@ -179,10 +179,7 @@ func (b *Local) State(name string) (state.State, error) { return nil, err } - statePath, stateOutPath, backupPath, err := b.StatePaths(name) - if err != nil { - return nil, err - } + statePath, stateOutPath, backupPath := b.StatePaths(name) // Otherwise, we need to load the state. var s state.State = &state.LocalState{ @@ -296,7 +293,7 @@ func (b *Local) schemaConfigure(ctx context.Context) error { // StatePaths returns the StatePath, StateOutPath, and StateBackupPath as // configured from the CLI. -func (b *Local) StatePaths(name string) (string, string, string, error) { +func (b *Local) StatePaths(name string) (string, string, string) { statePath := b.StatePath stateOutPath := b.StateOutPath backupPath := b.StateBackupPath @@ -324,7 +321,7 @@ func (b *Local) StatePaths(name string) (string, string, string, error) { backupPath = stateOutPath + DefaultBackupExtension } - return statePath, stateOutPath, backupPath, nil + return statePath, stateOutPath, backupPath } // this only ensures that the named directory exists diff --git a/backend/local/backend_test.go b/backend/local/backend_test.go index 1d9459e64a..f929e74413 100644 --- a/backend/local/backend_test.go +++ b/backend/local/backend_test.go @@ -4,6 +4,7 @@ import ( "errors" "io/ioutil" "os" + "path/filepath" "reflect" "strings" "testing" @@ -39,6 +40,47 @@ func checkState(t *testing.T, path, expected string) { } } +func TestLocal_StatePaths(t *testing.T) { + b := &Local{} + + // Test the defaults + path, out, back := b.StatePaths("") + + if path != DefaultStateFilename { + t.Fatalf("expected %q, got %q", DefaultStateFilename, path) + } + + if out != DefaultStateFilename { + t.Fatalf("expected %q, got %q", DefaultStateFilename, out) + } + + dfltBackup := DefaultStateFilename + DefaultBackupExtension + if back != dfltBackup { + t.Fatalf("expected %q, got %q", dfltBackup, back) + } + + // check with env + testEnv := "test_env" + path, out, back = b.StatePaths(testEnv) + + expectedPath := filepath.Join(DefaultEnvDir, testEnv, DefaultStateFilename) + expectedOut := expectedPath + expectedBackup := expectedPath + DefaultBackupExtension + + if path != expectedPath { + t.Fatalf("expected %q, got %q", expectedPath, path) + } + + if out != expectedOut { + t.Fatalf("expected %q, got %q", expectedOut, out) + } + + if back != expectedBackup { + t.Fatalf("expected %q, got %q", expectedBackup, back) + } + +} + func TestLocal_addAndRemoveStates(t *testing.T) { defer testTmpDir(t)() dflt := backend.DefaultStateName @@ -117,7 +159,7 @@ func TestLocal_addAndRemoveStates(t *testing.T) { } } -// a local backend which return sentinel errors for NamedState methods to +// a local backend which returns sentinel errors for NamedState methods to // verify it's being called. type testDelegateBackend struct { *Local diff --git a/command/meta.go b/command/meta.go index 12f767d2a4..c18c77fc74 100644 --- a/command/meta.go +++ b/command/meta.go @@ -424,8 +424,8 @@ func (m *Meta) Env() string { current = backend.DefaultStateName } - // return default if the file simply doesn't exist if err != nil && !os.IsNotExist(err) { + // always return the default if we can't get an environment name log.Printf("[ERROR] failed to read current environment: %s", err) } diff --git a/command/state_meta.go b/command/state_meta.go index 0e31b874b2..9381c6a455 100644 --- a/command/state_meta.go +++ b/command/state_meta.go @@ -39,7 +39,7 @@ func (c *StateMeta) State(m *Meta) (state.State, error) { panic(err) } localB := localRaw.(*backendlocal.Local) - _, stateOutPath, _, err := localB.StatePaths(env) + _, stateOutPath, _ := localB.StatePaths(env) if err != nil { return nil, err }