Skip to content

Commit 7199fbd

Browse files
authored
Update use of testChdir to standard library's t.Chdir, remove testChdir function from codebase (#37334)
* Replace use of `testChdir` with `t.Chdir` * Update tests to use temporary directories with copied content, instead of using directories in the repo directly. * Remove stacks copy of testChdir function This has been replaced with t.Chdir from the standard library * Replace remaining simple usage of testChdir * Update guidance for using `tempWorkingDir` * Replace use of testChdir in a function reused in a single test * Update comments to no longer recommend using testChdir * Remove testChdir function!
1 parent d938350 commit 7199fbd

34 files changed

+370
-387
lines changed

internal/command/apply_destroy_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestApply_destroy(t *testing.T) {
2323
// Create a temporary working directory that is empty
2424
td := t.TempDir()
2525
testCopyDir(t, testFixturePath("apply"), td)
26-
t.Cleanup(testChdir(t, td))
26+
t.Chdir(td)
2727

2828
originalState := states.BuildState(func(s *states.SyncState) {
2929
s.SetResourceInstanceCurrent(
@@ -127,7 +127,7 @@ func TestApply_destroyApproveNo(t *testing.T) {
127127
// Create a temporary working directory that is empty
128128
td := t.TempDir()
129129
testCopyDir(t, testFixturePath("apply"), td)
130-
t.Cleanup(testChdir(t, td))
130+
t.Chdir(td)
131131

132132
// Create some existing state
133133
originalState := states.BuildState(func(s *states.SyncState) {
@@ -195,7 +195,7 @@ func TestApply_destroyApproveYes(t *testing.T) {
195195
// Create a temporary working directory that is empty
196196
td := t.TempDir()
197197
testCopyDir(t, testFixturePath("apply"), td)
198-
t.Cleanup(testChdir(t, td))
198+
t.Chdir(td)
199199

200200
// Create some existing state
201201
originalState := states.BuildState(func(s *states.SyncState) {
@@ -266,7 +266,7 @@ func TestApply_destroyLockedState(t *testing.T) {
266266
// Create a temporary working directory that is empty
267267
td := t.TempDir()
268268
testCopyDir(t, testFixturePath("apply"), td)
269-
t.Cleanup(testChdir(t, td))
269+
t.Chdir(td)
270270

271271
originalState := states.BuildState(func(s *states.SyncState) {
272272
s.SetResourceInstanceCurrent(
@@ -324,7 +324,7 @@ func TestApply_destroyPlan(t *testing.T) {
324324
// Create a temporary working directory that is empty
325325
td := t.TempDir()
326326
testCopyDir(t, testFixturePath("apply"), td)
327-
t.Cleanup(testChdir(t, td))
327+
t.Chdir(td)
328328

329329
planPath := testPlanFileNoop(t)
330330

@@ -356,7 +356,7 @@ func TestApply_destroyPath(t *testing.T) {
356356
// Create a temporary working directory that is empty
357357
td := t.TempDir()
358358
testCopyDir(t, testFixturePath("apply"), td)
359-
t.Cleanup(testChdir(t, td))
359+
t.Chdir(td)
360360

361361
p := applyFixtureProvider()
362362

@@ -390,7 +390,7 @@ func TestApply_destroyTargetedDependencies(t *testing.T) {
390390
// Create a temporary working directory that is empty
391391
td := t.TempDir()
392392
testCopyDir(t, testFixturePath("apply-destroy-targeted"), td)
393-
t.Cleanup(testChdir(t, td))
393+
t.Chdir(td)
394394

395395
originalState := states.BuildState(func(s *states.SyncState) {
396396
s.SetResourceInstanceCurrent(
@@ -524,7 +524,7 @@ func TestApply_destroyTargeted(t *testing.T) {
524524
// Create a temporary working directory that is empty
525525
td := t.TempDir()
526526
testCopyDir(t, testFixturePath("apply-destroy-targeted"), td)
527-
t.Cleanup(testChdir(t, td))
527+
t.Chdir(td)
528528

529529
originalState := states.BuildState(func(s *states.SyncState) {
530530
s.SetResourceInstanceCurrent(

internal/command/apply_test.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestApply(t *testing.T) {
3737
// Create a temporary working directory that is empty
3838
td := t.TempDir()
3939
testCopyDir(t, testFixturePath("apply"), td)
40-
t.Cleanup(testChdir(t, td))
40+
t.Chdir(td)
4141

4242
statePath := testTempFile(t)
4343

@@ -75,7 +75,7 @@ func TestApply_path(t *testing.T) {
7575
// Create a temporary working directory that is empty
7676
td := t.TempDir()
7777
testCopyDir(t, testFixturePath("apply"), td)
78-
t.Cleanup(testChdir(t, td))
78+
t.Chdir(td)
7979

8080
p := applyFixtureProvider()
8181

@@ -105,7 +105,7 @@ func TestApply_approveNo(t *testing.T) {
105105
// Create a temporary working directory that is empty
106106
td := t.TempDir()
107107
testCopyDir(t, testFixturePath("apply"), td)
108-
t.Cleanup(testChdir(t, td))
108+
t.Chdir(td)
109109

110110
statePath := testTempFile(t)
111111

@@ -148,7 +148,7 @@ func TestApply_approveYes(t *testing.T) {
148148
// Create a temporary working directory that is empty
149149
td := t.TempDir()
150150
testCopyDir(t, testFixturePath("apply"), td)
151-
t.Cleanup(testChdir(t, td))
151+
t.Chdir(td)
152152

153153
statePath := testTempFile(t)
154154

@@ -195,7 +195,7 @@ func TestApply_lockedState(t *testing.T) {
195195
// Create a temporary working directory that is empty
196196
td := t.TempDir()
197197
testCopyDir(t, testFixturePath("apply"), td)
198-
t.Cleanup(testChdir(t, td))
198+
t.Chdir(td)
199199

200200
statePath := testTempFile(t)
201201

@@ -234,7 +234,7 @@ func TestApply_lockedStateWait(t *testing.T) {
234234
// Create a temporary working directory that is empty
235235
td := t.TempDir()
236236
testCopyDir(t, testFixturePath("apply"), td)
237-
t.Cleanup(testChdir(t, td))
237+
t.Chdir(td)
238238

239239
statePath := testTempFile(t)
240240

@@ -278,7 +278,7 @@ func TestApply_parallelism(t *testing.T) {
278278
// Create a temporary working directory that is empty
279279
td := t.TempDir()
280280
testCopyDir(t, testFixturePath("parallelism"), td)
281-
t.Cleanup(testChdir(t, td))
281+
t.Chdir(td)
282282

283283
statePath := testTempFile(t)
284284

@@ -376,7 +376,7 @@ func TestApply_configInvalid(t *testing.T) {
376376
// Create a temporary working directory that is empty
377377
td := t.TempDir()
378378
testCopyDir(t, testFixturePath("apply-config-invalid"), td)
379-
t.Cleanup(testChdir(t, td))
379+
t.Chdir(td)
380380

381381
p := testProvider()
382382
view, done := testView(t)
@@ -402,7 +402,7 @@ func TestApply_defaultState(t *testing.T) {
402402
// Create a temporary working directory that is empty
403403
td := t.TempDir()
404404
testCopyDir(t, testFixturePath("apply"), td)
405-
t.Cleanup(testChdir(t, td))
405+
t.Chdir(td)
406406

407407
statePath := filepath.Join(td, DefaultStateFilename)
408408

@@ -454,7 +454,7 @@ func TestApply_error(t *testing.T) {
454454
// Create a temporary working directory that is empty
455455
td := t.TempDir()
456456
testCopyDir(t, testFixturePath("apply-error"), td)
457-
t.Cleanup(testChdir(t, td))
457+
t.Chdir(td)
458458

459459
statePath := testTempFile(t)
460460

@@ -531,7 +531,7 @@ func TestApply_input(t *testing.T) {
531531
// Create a temporary working directory that is empty
532532
td := t.TempDir()
533533
testCopyDir(t, testFixturePath("apply-input"), td)
534-
t.Cleanup(testChdir(t, td))
534+
t.Chdir(td)
535535

536536
// Disable test mode so input would be asked
537537
test = false
@@ -581,7 +581,7 @@ func TestApply_inputPartial(t *testing.T) {
581581
// Create a temporary working directory that is empty
582582
td := t.TempDir()
583583
testCopyDir(t, testFixturePath("apply-input-partial"), td)
584-
t.Cleanup(testChdir(t, td))
584+
t.Chdir(td)
585585

586586
// Disable test mode so input would be asked
587587
test = false
@@ -627,7 +627,7 @@ func TestApply_noArgs(t *testing.T) {
627627
// Create a temporary working directory that is empty
628628
td := t.TempDir()
629629
testCopyDir(t, testFixturePath("apply"), td)
630-
t.Cleanup(testChdir(t, td))
630+
t.Chdir(td)
631631

632632
statePath := testTempFile(t)
633633

@@ -1250,7 +1250,7 @@ foo = "bar"
12501250
t.Run(name, func(t *testing.T) {
12511251
td := t.TempDir()
12521252
testCopyDir(t, testFixturePath("apply-ephemeral-variable"), td)
1253-
t.Cleanup(testChdir(t, td))
1253+
t.Chdir(td)
12541254

12551255
_, snap := testModuleWithSnapshot(t, "apply-ephemeral-variable")
12561256
plannedVal := cty.ObjectVal(map[string]cty.Value{
@@ -1424,7 +1424,7 @@ func TestApply_changedVars_applyTime(t *testing.T) {
14241424
// var files.
14251425
td := t.TempDir()
14261426
testCopyDir(t, testFixturePath("apply-vars-auto"), td)
1427-
t.Cleanup(testChdir(t, td))
1427+
t.Chdir(td)
14281428

14291429
p := planVarsFixtureProvider()
14301430
view, done := testView(t)
@@ -1470,7 +1470,7 @@ func TestApply_planNoModuleFiles(t *testing.T) {
14701470
td := testTempDir(t)
14711471
defer os.RemoveAll(td)
14721472

1473-
t.Cleanup(testChdir(t, td))
1473+
t.Chdir(td)
14741474

14751475
p := applyFixtureProvider()
14761476
planPath := applyFixturePlanFile(t)
@@ -1493,7 +1493,7 @@ func TestApply_refresh(t *testing.T) {
14931493
// Create a temporary working directory that is empty
14941494
td := t.TempDir()
14951495
testCopyDir(t, testFixturePath("apply"), td)
1496-
t.Cleanup(testChdir(t, td))
1496+
t.Chdir(td)
14971497

14981498
originalState := states.BuildState(func(s *states.SyncState) {
14991499
s.SetResourceInstanceCurrent(
@@ -1560,7 +1560,7 @@ func TestApply_refreshFalse(t *testing.T) {
15601560
// Create a temporary working directory that is empty
15611561
td := t.TempDir()
15621562
testCopyDir(t, testFixturePath("apply"), td)
1563-
t.Cleanup(testChdir(t, td))
1563+
t.Chdir(td)
15641564

15651565
originalState := states.BuildState(func(s *states.SyncState) {
15661566
s.SetResourceInstanceCurrent(
@@ -1609,7 +1609,7 @@ func TestApply_shutdown(t *testing.T) {
16091609
// Create a temporary working directory that is empty
16101610
td := t.TempDir()
16111611
testCopyDir(t, testFixturePath("apply-shutdown"), td)
1612-
t.Cleanup(testChdir(t, td))
1612+
t.Chdir(td)
16131613

16141614
cancelled := make(chan struct{})
16151615
shutdownCh := make(chan struct{})
@@ -1697,7 +1697,7 @@ func TestApply_state(t *testing.T) {
16971697
// Create a temporary working directory that is empty
16981698
td := t.TempDir()
16991699
testCopyDir(t, testFixturePath("apply"), td)
1700-
t.Cleanup(testChdir(t, td))
1700+
t.Chdir(td)
17011701

17021702
originalState := states.BuildState(func(s *states.SyncState) {
17031703
s.SetResourceInstanceCurrent(
@@ -1791,7 +1791,7 @@ func TestApply_stateNoExist(t *testing.T) {
17911791
// Create a temporary working directory that is empty
17921792
td := t.TempDir()
17931793
testCopyDir(t, testFixturePath("apply"), td)
1794-
t.Cleanup(testChdir(t, td))
1794+
t.Chdir(td)
17951795

17961796
p := applyFixtureProvider()
17971797
view, done := testView(t)
@@ -1816,7 +1816,7 @@ func TestApply_sensitiveOutput(t *testing.T) {
18161816
// Create a temporary working directory that is empty
18171817
td := t.TempDir()
18181818
testCopyDir(t, testFixturePath("apply-sensitive-output"), td)
1819-
t.Cleanup(testChdir(t, td))
1819+
t.Chdir(td)
18201820

18211821
p := testProvider()
18221822
view, done := testView(t)
@@ -1853,7 +1853,7 @@ func TestApply_vars(t *testing.T) {
18531853
// Create a temporary working directory that is empty
18541854
td := t.TempDir()
18551855
testCopyDir(t, testFixturePath("apply-vars"), td)
1856-
t.Cleanup(testChdir(t, td))
1856+
t.Chdir(td)
18571857

18581858
statePath := testTempFile(t)
18591859

@@ -1910,7 +1910,7 @@ func TestApply_varFile(t *testing.T) {
19101910
// Create a temporary working directory that is empty
19111911
td := t.TempDir()
19121912
testCopyDir(t, testFixturePath("apply-vars"), td)
1913-
t.Cleanup(testChdir(t, td))
1913+
t.Chdir(td)
19141914

19151915
varFilePath := testTempFile(t)
19161916
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
@@ -1972,7 +1972,7 @@ func TestApply_varFileDefault(t *testing.T) {
19721972
// Create a temporary working directory that is empty
19731973
td := t.TempDir()
19741974
testCopyDir(t, testFixturePath("apply-vars"), td)
1975-
t.Cleanup(testChdir(t, td))
1975+
t.Chdir(td)
19761976

19771977
varFilePath := filepath.Join(td, "terraform.tfvars")
19781978
if err := os.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
@@ -2033,7 +2033,7 @@ func TestApply_varFileDefaultJSON(t *testing.T) {
20332033
// Create a temporary working directory that is empty
20342034
td := t.TempDir()
20352035
testCopyDir(t, testFixturePath("apply-vars"), td)
2036-
t.Cleanup(testChdir(t, td))
2036+
t.Chdir(td)
20372037

20382038
varFilePath := filepath.Join(td, "terraform.tfvars.json")
20392039
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFileJSON), 0644); err != nil {
@@ -2094,7 +2094,7 @@ func TestApply_backup(t *testing.T) {
20942094
// Create a temporary working directory that is empty
20952095
td := t.TempDir()
20962096
testCopyDir(t, testFixturePath("apply"), td)
2097-
t.Cleanup(testChdir(t, td))
2097+
t.Chdir(td)
20982098

20992099
originalState := states.BuildState(func(s *states.SyncState) {
21002100
s.SetResourceInstanceCurrent(
@@ -2171,7 +2171,7 @@ func TestApply_disableBackup(t *testing.T) {
21712171
// Create a temporary working directory that is empty
21722172
td := t.TempDir()
21732173
testCopyDir(t, testFixturePath("apply"), td)
2174-
t.Cleanup(testChdir(t, td))
2174+
t.Chdir(td)
21752175

21762176
originalState := testState()
21772177
statePath := testStateFile(t, originalState)
@@ -2250,7 +2250,7 @@ func TestApply_terraformEnv(t *testing.T) {
22502250
// Create a temporary working directory that is empty
22512251
td := t.TempDir()
22522252
testCopyDir(t, testFixturePath("apply-terraform-env"), td)
2253-
t.Cleanup(testChdir(t, td))
2253+
t.Chdir(td)
22542254

22552255
statePath := testTempFile(t)
22562256

@@ -2287,7 +2287,7 @@ func TestApply_terraformEnvNonDefault(t *testing.T) {
22872287
// Create a temporary working directory that is empty
22882288
td := t.TempDir()
22892289
testCopyDir(t, testFixturePath("apply-terraform-env"), td)
2290-
t.Cleanup(testChdir(t, td))
2290+
t.Chdir(td)
22912291

22922292
// Create new env
22932293
{
@@ -2348,7 +2348,7 @@ output = test
23482348
func TestApply_targeted(t *testing.T) {
23492349
td := t.TempDir()
23502350
testCopyDir(t, testFixturePath("apply-targeted"), td)
2351-
t.Cleanup(testChdir(t, td))
2351+
t.Chdir(td)
23522352

23532353
p := testProvider()
23542354
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
@@ -2403,7 +2403,7 @@ func TestApply_targetFlagsDiags(t *testing.T) {
24032403
t.Run(target, func(t *testing.T) {
24042404
td := testTempDir(t)
24052405
defer os.RemoveAll(td)
2406-
t.Cleanup(testChdir(t, td))
2406+
t.Chdir(td)
24072407

24082408
view, done := testView(t)
24092409
c := &ApplyCommand{
@@ -2436,7 +2436,7 @@ func TestApply_targetFlagsDiags(t *testing.T) {
24362436
func TestApply_replace(t *testing.T) {
24372437
td := t.TempDir()
24382438
testCopyDir(t, testFixturePath("apply-replace"), td)
2439-
t.Cleanup(testChdir(t, td))
2439+
t.Chdir(td)
24402440

24412441
originalState := states.BuildState(func(s *states.SyncState) {
24422442
s.SetResourceInstanceCurrent(
@@ -2523,7 +2523,7 @@ func TestApply_pluginPath(t *testing.T) {
25232523
// Create a temporary working directory that is empty
25242524
td := t.TempDir()
25252525
testCopyDir(t, testFixturePath("apply"), td)
2526-
t.Cleanup(testChdir(t, td))
2526+
t.Chdir(td)
25272527

25282528
statePath := testTempFile(t)
25292529

@@ -2563,7 +2563,7 @@ func TestApply_jsonGoldenReference(t *testing.T) {
25632563
// Create a temporary working directory that is empty
25642564
td := t.TempDir()
25652565
testCopyDir(t, testFixturePath("apply"), td)
2566-
t.Cleanup(testChdir(t, td))
2566+
t.Chdir(td)
25672567

25682568
statePath := testTempFile(t)
25692569

@@ -2604,7 +2604,7 @@ func TestApply_warnings(t *testing.T) {
26042604
// Create a temporary working directory that is empty
26052605
td := t.TempDir()
26062606
testCopyDir(t, testFixturePath("apply"), td)
2607-
t.Cleanup(testChdir(t, td))
2607+
t.Chdir(td)
26082608

26092609
p := testProvider()
26102610
p.GetProviderSchemaResponse = applyFixtureSchema()

0 commit comments

Comments
 (0)