Local.StatePaths doesn't need to reutrn an error

add a test to ensure we have consistent output
This commit is contained in:
James Bardin 2017-02-28 19:18:16 -05:00
parent dc675540de
commit 4dac986a91
4 changed files with 48 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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
}