From 0c97c5a3d94519c3c23736b47077e7ca34dd2225 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Feb 2017 09:11:48 -0800 Subject: [PATCH] command: tests should move to temporary cwd This fixes any issues where rogue tfstate files may appear in the cwd otherwise. --- command/get_test.go | 6 ++++++ command/graph_test.go | 6 ++++++ command/plan_test.go | 15 +++++++++++++++ command/state_test.go | 3 +++ 4 files changed, 30 insertions(+) diff --git a/command/get_test.go b/command/get_test.go index c3ea4e0ce..b93415192 100644 --- a/command/get_test.go +++ b/command/get_test.go @@ -9,6 +9,9 @@ import ( ) func TestGet(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + ui := new(cli.MockUi) c := &GetCommand{ Meta: Meta{ @@ -87,6 +90,9 @@ func TestGet_noArgs(t *testing.T) { } func TestGet_update(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + ui := new(cli.MockUi) c := &GetCommand{ Meta: Meta{ diff --git a/command/graph_test.go b/command/graph_test.go index 716218a7a..6eabc29b3 100644 --- a/command/graph_test.go +++ b/command/graph_test.go @@ -10,6 +10,9 @@ import ( ) func TestGraph(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + ui := new(cli.MockUi) c := &GraphCommand{ Meta: Meta{ @@ -79,6 +82,9 @@ func TestGraph_noArgs(t *testing.T) { } func TestGraph_plan(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + planPath := testPlanFile(t, &terraform.Plan{ Diff: &terraform.Diff{ Modules: []*terraform.ModuleDiff{ diff --git a/command/plan_test.go b/command/plan_test.go index b9e25bf22..786bae78e 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -191,6 +191,9 @@ func TestPlan_noState(t *testing.T) { } func TestPlan_outPath(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + tf, err := ioutil.TempFile("", "tf") if err != nil { t.Fatalf("err: %s", err) @@ -402,6 +405,9 @@ func TestPlan_outBackendLegacy(t *testing.T) { } func TestPlan_refresh(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + p := testProvider() ui := new(cli.MockUi) c := &PlanCommand{ @@ -619,6 +625,9 @@ func TestPlan_validate(t *testing.T) { } func TestPlan_vars(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + p := testProvider() ui := new(cli.MockUi) c := &PlanCommand{ @@ -654,6 +663,9 @@ func TestPlan_vars(t *testing.T) { } func TestPlan_varsUnset(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + // Disable test mode so input would be asked test = false defer func() { test = true }() @@ -678,6 +690,9 @@ func TestPlan_varsUnset(t *testing.T) { } func TestPlan_varFile(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + varFilePath := testTempFile(t) if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil { t.Fatalf("err: %s", err) diff --git a/command/state_test.go b/command/state_test.go index b02fd964d..28d64e359 100644 --- a/command/state_test.go +++ b/command/state_test.go @@ -25,6 +25,9 @@ func testStateBackups(t *testing.T, dir string) []string { } func TestStateDefaultBackupExtension(t *testing.T) { + tmp, cwd := testCwd(t) + defer testFixCwd(t, tmp, cwd) + s, err := (&StateMeta{}).State(&Meta{}) if err != nil { t.Fatal(err)