From 0ed04d05aafd9b335fb80ef9adc78c40f73898c5 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Thu, 18 Jun 2020 17:09:20 -0400 Subject: [PATCH] command: Fix command fixture modify-in-place bugs Some of the tests in the command package were running directly on the fixture directories, and modifying or locking files within them. This could cause state to leak between tests. This commit cleans up all such cases that I could find. --- command/command_test.go | 2 +- command/plan_test.go | 76 ++++++++++++++--------------------------- 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index 2a528765a..2a08d6afc 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -540,7 +540,7 @@ func testChdir(t *testing.T, new string) func() { } // testCwd is used to change the current working directory -// into a test directory that should be remoted after +// into a test directory that should be removed after func testCwd(t *testing.T) (string, string) { t.Helper() diff --git a/command/plan_test.go b/command/plan_test.go index 3116dd754..fcac3a7c0 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -25,14 +25,10 @@ import ( ) func TestPlan(t *testing.T) { - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Chdir(testFixturePath("plan")); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) + td := tempDir(t) + copy.CopyDir(testFixturePath("plan"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() p := planFixtureProvider() ui := new(cli.MockUi) @@ -50,23 +46,17 @@ func TestPlan(t *testing.T) { } func TestPlan_lockedState(t *testing.T) { - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } + td := tempDir(t) + copy.CopyDir(testFixturePath("plan"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() - testPath := testFixturePath("plan") - unlock, err := testLockState(testDataDir, filepath.Join(testPath, DefaultStateFilename)) + unlock, err := testLockState(testDataDir, filepath.Join(td, DefaultStateFilename)) if err != nil { t.Fatal(err) } defer unlock() - if err := os.Chdir(testPath); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) - p := planFixtureProvider() ui := new(cli.MockUi) c := &PlanCommand{ @@ -489,14 +479,10 @@ func TestPlan_validate(t *testing.T) { test = false defer func() { test = true }() - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Chdir(testFixturePath("plan-invalid")); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) + td := tempDir(t) + copy.CopyDir(testFixturePath("plan-invalid"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() p := testProvider() p.GetSchemaReturn = &terraform.ProviderSchema{ @@ -726,14 +712,10 @@ func TestPlan_varFileWithDecls(t *testing.T) { } func TestPlan_detailedExitcode(t *testing.T) { - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Chdir(testFixturePath("plan")); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) + td := tempDir(t) + copy.CopyDir(testFixturePath("plan"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() p := planFixtureProvider() ui := new(cli.MockUi) @@ -751,14 +733,10 @@ func TestPlan_detailedExitcode(t *testing.T) { } func TestPlan_detailedExitcode_emptyDiff(t *testing.T) { - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Chdir(testFixturePath("plan-emptydiff")); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) + td := tempDir(t) + copy.CopyDir(testFixturePath("plan-emptydiff"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() p := testProvider() ui := new(cli.MockUi) @@ -855,14 +833,10 @@ func TestPlan_shutdown(t *testing.T) { } func TestPlan_init_required(t *testing.T) { - cwd, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } - if err := os.Chdir(testFixturePath("plan")); err != nil { - t.Fatalf("err: %s", err) - } - defer os.Chdir(cwd) + td := tempDir(t) + copy.CopyDir(testFixturePath("plan"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() ui := new(cli.MockUi) c := &PlanCommand{