Skip to content

Commit 0c97c5a

Browse files
committed
command: tests should move to temporary cwd
This fixes any issues where rogue tfstate files may appear in the cwd otherwise.
1 parent 1ab17bf commit 0c97c5a

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎command/get_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
)
1010

1111
func TestGet(t *testing.T) {
12+
tmp, cwd := testCwd(t)
13+
defer testFixCwd(t, tmp, cwd)
14+
1215
ui := new(cli.MockUi)
1316
c := &GetCommand{
1417
Meta: Meta{
@@ -87,6 +90,9 @@ func TestGet_noArgs(t *testing.T) {
8790
}
8891

8992
func TestGet_update(t *testing.T) {
93+
tmp, cwd := testCwd(t)
94+
defer testFixCwd(t, tmp, cwd)
95+
9096
ui := new(cli.MockUi)
9197
c := &GetCommand{
9298
Meta: Meta{

‎command/graph_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
)
1111

1212
func TestGraph(t *testing.T) {
13+
tmp, cwd := testCwd(t)
14+
defer testFixCwd(t, tmp, cwd)
15+
1316
ui := new(cli.MockUi)
1417
c := &GraphCommand{
1518
Meta: Meta{
@@ -79,6 +82,9 @@ func TestGraph_noArgs(t *testing.T) {
7982
}
8083

8184
func TestGraph_plan(t *testing.T) {
85+
tmp, cwd := testCwd(t)
86+
defer testFixCwd(t, tmp, cwd)
87+
8288
planPath := testPlanFile(t, &terraform.Plan{
8389
Diff: &terraform.Diff{
8490
Modules: []*terraform.ModuleDiff{

‎command/plan_test.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func TestPlan_noState(t *testing.T) {
191191
}
192192

193193
func TestPlan_outPath(t *testing.T) {
194+
tmp, cwd := testCwd(t)
195+
defer testFixCwd(t, tmp, cwd)
196+
194197
tf, err := ioutil.TempFile("", "tf")
195198
if err != nil {
196199
t.Fatalf("err: %s", err)
@@ -402,6 +405,9 @@ func TestPlan_outBackendLegacy(t *testing.T) {
402405
}
403406

404407
func TestPlan_refresh(t *testing.T) {
408+
tmp, cwd := testCwd(t)
409+
defer testFixCwd(t, tmp, cwd)
410+
405411
p := testProvider()
406412
ui := new(cli.MockUi)
407413
c := &PlanCommand{
@@ -619,6 +625,9 @@ func TestPlan_validate(t *testing.T) {
619625
}
620626

621627
func TestPlan_vars(t *testing.T) {
628+
tmp, cwd := testCwd(t)
629+
defer testFixCwd(t, tmp, cwd)
630+
622631
p := testProvider()
623632
ui := new(cli.MockUi)
624633
c := &PlanCommand{
@@ -654,6 +663,9 @@ func TestPlan_vars(t *testing.T) {
654663
}
655664

656665
func TestPlan_varsUnset(t *testing.T) {
666+
tmp, cwd := testCwd(t)
667+
defer testFixCwd(t, tmp, cwd)
668+
657669
// Disable test mode so input would be asked
658670
test = false
659671
defer func() { test = true }()
@@ -678,6 +690,9 @@ func TestPlan_varsUnset(t *testing.T) {
678690
}
679691

680692
func TestPlan_varFile(t *testing.T) {
693+
tmp, cwd := testCwd(t)
694+
defer testFixCwd(t, tmp, cwd)
695+
681696
varFilePath := testTempFile(t)
682697
if err := ioutil.WriteFile(varFilePath, []byte(planVarFile), 0644); err != nil {
683698
t.Fatalf("err: %s", err)

‎command/state_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func testStateBackups(t *testing.T, dir string) []string {
2525
}
2626

2727
func TestStateDefaultBackupExtension(t *testing.T) {
28+
tmp, cwd := testCwd(t)
29+
defer testFixCwd(t, tmp, cwd)
30+
2831
s, err := (&StateMeta{}).State(&Meta{})
2932
if err != nil {
3033
t.Fatal(err)

0 commit comments

Comments
 (0)