diff --git a/command/command_test.go b/command/command_test.go index 5d150bcb1..fcc8dfb25 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -224,21 +224,7 @@ func testProvider() *terraform.MockResourceProvider { } func testTempFile(t *testing.T) string { - tf, err := ioutil.TempFile("", "tf") - if err != nil { - t.Fatalf("err: %s", err) - } - - result := tf.Name() - - if err := tf.Close(); err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Remove(result); err != nil { - t.Fatalf("err: %s", err) - } - - return result + return filepath.Join(testTempDir(t), "state.tfstate") } func testTempDir(t *testing.T) string { diff --git a/command/plan_test.go b/command/plan_test.go index 935793174..8736b8cc9 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -108,6 +108,9 @@ func TestPlan_destroy(t *testing.T) { } func TestPlan_noState(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + p := testProvider() ui := new(cli.MockUi) c := &PlanCommand{ diff --git a/command/state_meta.go b/command/state_meta.go index f576004e3..c1af8b82e 100644 --- a/command/state_meta.go +++ b/command/state_meta.go @@ -3,12 +3,18 @@ package command import ( "errors" + "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" ) // StateMeta is the meta struct that should be embedded in state subcommands. type StateMeta struct{} +// State returns the state for this meta. +func (m *StateMeta) State(cm *Meta) (state.State, error) { + return cm.State() +} + // filterInstance filters a single instance out of filter results. func (c *StateMeta) filterInstance(rs []*terraform.StateFilterResult) (*terraform.StateFilterResult, error) { var result *terraform.StateFilterResult diff --git a/command/state_show.go b/command/state_show.go index 55bc30910..d9a4f1d08 100644 --- a/command/state_show.go +++ b/command/state_show.go @@ -26,7 +26,7 @@ func (c *StateShowCommand) Run(args []string) int { } args = cmdFlags.Args() - state, err := c.State() + state, err := c.Meta.State() if err != nil { c.Ui.Error(fmt.Sprintf(errStateLoadingState, err)) return cli.RunResultHelp diff --git a/command/state_test.go b/command/state_test.go new file mode 100644 index 000000000..7fe83e904 --- /dev/null +++ b/command/state_test.go @@ -0,0 +1,22 @@ +package command + +import ( + "path/filepath" + "sort" + "testing" +) + +// testStateBackups returns the list of backups in order of creation +// (oldest first) in the given directory. +func testStateBackups(t *testing.T, dir string) []string { + // Find all the backups + list, err := filepath.Glob(filepath.Join(dir, "*"+DefaultBackupExtension)) + if err != nil { + t.Fatalf("err: %s", err) + } + + // Sort them which will put them naturally in the right order + sort.Strings(list) + + return list +}