Merge pull request #29571 from hashicorp/jbardin/tmp-dir-fixes

fix temp directory handling in some tests
This commit is contained in:
James Bardin 2021-09-13 13:55:47 -04:00 committed by GitHub
commit d33a423722
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 48 deletions

View File

@ -132,7 +132,7 @@ func TestLocalRun_stalePlan(t *testing.T) {
stateFile := statefile.New(plan.PriorState, "boop", 2)
// Roundtrip through serialization as expected by the operation
outDir := testTempDir(t)
outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan")
if err := planfile.Create(planPath, configload.NewEmptySnapshot(), prevStateFile, stateFile, plan); err != nil {

View File

@ -174,7 +174,7 @@ func TestLocal_planOutputsChanged(t *testing.T) {
// unknown" situation because that's already common for printing out
// resource changes and we already have many tests for that.
}))
outDir := testTempDir(t)
outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-outputs-changed")
@ -232,7 +232,7 @@ func TestLocal_planModuleOutputsChanged(t *testing.T) {
OutputValue: addrs.OutputValue{Name: "changed"},
}, cty.StringVal("before"), false)
}))
outDir := testTempDir(t)
outDir := t.TempDir()
defer os.RemoveAll(outDir)
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-module-outputs-changed")
@ -275,8 +275,7 @@ func TestLocal_planTainted(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_tainted())
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
defer configCleanup()
@ -356,8 +355,7 @@ func TestLocal_planDeposedOnly(t *testing.T) {
},
)
}))
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
defer configCleanup()
@ -448,8 +446,7 @@ func TestLocal_planTainted_createBeforeDestroy(t *testing.T) {
defer cleanup()
p := TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_tainted())
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan-cbd")
defer configCleanup()
@ -540,8 +537,7 @@ func TestLocal_planDestroy(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState())
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan")
@ -594,8 +590,7 @@ func TestLocal_planDestroy_withDataSources(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState_withDataSource())
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/destroy-with-ds")
@ -670,8 +665,7 @@ func TestLocal_planOutPathNoChange(t *testing.T) {
TestLocalProvider(t, b, "test", planFixtureSchema())
testStateFile(t, b.StatePath, testPlanState())
outDir := testTempDir(t)
defer os.RemoveAll(outDir)
outDir := t.TempDir()
planPath := filepath.Join(outDir, "plan.tfplan")
op, configCleanup, done := testOperationPlan(t, "./testdata/plan")

View File

@ -1,7 +1,6 @@
package local
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -24,7 +23,7 @@ import (
// public fields without any locks.
func TestLocal(t *testing.T) (*Local, func()) {
t.Helper()
tempDir := testTempDir(t)
tempDir := t.TempDir()
local := New()
local.StatePath = filepath.Join(tempDir, "state.tfstate")
@ -189,15 +188,6 @@ func (b *TestLocalNoDefaultState) StateMgr(name string) (statemgr.Full, error) {
return b.Local.StateMgr(name)
}
func testTempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
return d
}
func testStateFile(t *testing.T, path string, s *states.State) {
stateFile := statemgr.NewFilesystem(path)
stateFile.WriteState(s)

View File

@ -147,16 +147,10 @@ func tempWorkingDir(t *testing.T) (*workdir.Dir, func() error) {
// the testWorkingDir commentary for an example of how to use this function
// along with testChdir to meet the expectations of command.Meta legacy
// functionality.
func tempWorkingDirFixture(t *testing.T, fixtureName string) (*workdir.Dir, func() error) {
func tempWorkingDirFixture(t *testing.T, fixtureName string) *workdir.Dir {
t.Helper()
dirPath, err := os.MkdirTemp("", "tf-command-test-"+fixtureName)
if err != nil {
t.Fatal(err)
}
done := func() error {
return os.RemoveAll(dirPath)
}
dirPath := testTempDir(t)
t.Logf("temporary directory %s with fixture %q", dirPath, fixtureName)
fixturePath := testFixturePath(fixtureName)
@ -165,7 +159,7 @@ func tempWorkingDirFixture(t *testing.T, fixtureName string) (*workdir.Dir, func
// on failure, a failure to copy will prevent us from cleaning up the
// temporary directory. Oh well. :(
return workdir.NewDir(dirPath), done
return workdir.NewDir(dirPath)
}
func testFixturePath(name string) string {
@ -550,13 +544,8 @@ func testTempFile(t *testing.T) string {
func testTempDir(t *testing.T) string {
t.Helper()
d, err := ioutil.TempDir(testingDir, "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
d, err = filepath.EvalSymlinks(d)
d := t.TempDir()
d, err := filepath.EvalSymlinks(d)
if err != nil {
t.Fatal(err)
}

View File

@ -8,8 +8,7 @@ import (
)
func TestGet(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "get")
defer cleanup()
wd := tempWorkingDirFixture(t, "get")
defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi()
@ -56,8 +55,7 @@ func TestGet_multipleArgs(t *testing.T) {
}
func TestGet_update(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "get")
defer cleanup()
wd := tempWorkingDirFixture(t, "get")
defer testChdir(t, wd.RootModuleDir())()
ui := cli.NewMockUi()

View File

@ -1855,8 +1855,7 @@ func TestMetaBackend_configToExtra(t *testing.T) {
// no config; return inmem backend stored in state
func TestBackendFromState(t *testing.T) {
wd, cleanup := tempWorkingDirFixture(t, "backend-from-state")
defer cleanup()
wd := tempWorkingDirFixture(t, "backend-from-state")
defer testChdir(t, wd.RootModuleDir())()
// Setup the meta