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.
This commit is contained in:
Alisdair McDiarmid 2020-06-18 17:09:20 -04:00
parent dc8fd14c1e
commit 0ed04d05aa
2 changed files with 26 additions and 52 deletions

View File

@ -540,7 +540,7 @@ func testChdir(t *testing.T, new string) func() {
} }
// testCwd is used to change the current working directory // 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) { func testCwd(t *testing.T) (string, string) {
t.Helper() t.Helper()

View File

@ -25,14 +25,10 @@ import (
) )
func TestPlan(t *testing.T) { func TestPlan(t *testing.T) {
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := planFixtureProvider() p := planFixtureProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -50,23 +46,17 @@ func TestPlan(t *testing.T) {
} }
func TestPlan_lockedState(t *testing.T) { func TestPlan_lockedState(t *testing.T) {
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
testPath := testFixturePath("plan") unlock, err := testLockState(testDataDir, filepath.Join(td, DefaultStateFilename))
unlock, err := testLockState(testDataDir, filepath.Join(testPath, DefaultStateFilename))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer unlock() defer unlock()
if err := os.Chdir(testPath); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := planFixtureProvider() p := planFixtureProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &PlanCommand{ c := &PlanCommand{
@ -489,14 +479,10 @@ func TestPlan_validate(t *testing.T) {
test = false test = false
defer func() { test = true }() defer func() { test = true }()
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan-invalid"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
if err := os.Chdir(testFixturePath("plan-invalid")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider() p := testProvider()
p.GetSchemaReturn = &terraform.ProviderSchema{ p.GetSchemaReturn = &terraform.ProviderSchema{
@ -726,14 +712,10 @@ func TestPlan_varFileWithDecls(t *testing.T) {
} }
func TestPlan_detailedExitcode(t *testing.T) { func TestPlan_detailedExitcode(t *testing.T) {
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := planFixtureProvider() p := planFixtureProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -751,14 +733,10 @@ func TestPlan_detailedExitcode(t *testing.T) {
} }
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) { func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan-emptydiff"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
if err := os.Chdir(testFixturePath("plan-emptydiff")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider() p := testProvider()
ui := new(cli.MockUi) ui := new(cli.MockUi)
@ -855,14 +833,10 @@ func TestPlan_shutdown(t *testing.T) {
} }
func TestPlan_init_required(t *testing.T) { func TestPlan_init_required(t *testing.T) {
cwd, err := os.Getwd() td := tempDir(t)
if err != nil { copy.CopyDir(testFixturePath("plan"), td)
t.Fatalf("err: %s", err) defer os.RemoveAll(td)
} defer testChdir(t, td)()
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &PlanCommand{ c := &PlanCommand{