Skip to content

Commit fa620ad

Browse files
authored
test: Add and update some init command tests, linting and code comment changes (#38056)
1 parent 44e5f86 commit fa620ad

File tree

3 files changed

+94
-33
lines changed

3 files changed

+94
-33
lines changed

internal/command/init_test.go

Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ func TestInit_two_step_provider_download(t *testing.T) {
240240
}
241241
}
242242

243+
// Test that an error is returned if users provide the removed directory argument, which was replaced with -chdir
244+
// See: https://github.com/hashicorp/terraform/commit/ca23a096d8c48544b9bfc6dbf13c66488f9b6964
243245
func TestInit_multipleArgs(t *testing.T) {
244246
// Create a temporary working directory that is empty
245247
td := t.TempDir()
@@ -263,6 +265,13 @@ func TestInit_multipleArgs(t *testing.T) {
263265
if code := c.Run(args); code != 1 {
264266
t.Fatalf("bad: \n%s", done(t).All())
265267
}
268+
269+
expectedMsg := "Did you mean to use -chdir?"
270+
if !strings.Contains(done(t).All(), expectedMsg) {
271+
t.Fatalf("expected the error message to include %q as part of protecting against deprecated additional arguments.",
272+
expectedMsg,
273+
)
274+
}
266275
}
267276

268277
func TestInit_migrateStateAndJSON(t *testing.T) {
@@ -401,7 +410,7 @@ func TestInit_fromModule_dstInSrc(t *testing.T) {
401410
}
402411

403412
func TestInit_get(t *testing.T) {
404-
// Create a temporary working directory that is empty
413+
// Create a temporary working directory and copy in test fixtures
405414
td := t.TempDir()
406415
testCopyDir(t, testFixturePath("init-get"), td)
407416
t.Chdir(td)
@@ -429,7 +438,7 @@ func TestInit_get(t *testing.T) {
429438
}
430439

431440
func TestInit_json(t *testing.T) {
432-
// Create a temporary working directory that is empty
441+
// Create a temporary working directory and copy in test fixtures
433442
td := t.TempDir()
434443
testCopyDir(t, testFixturePath("init-get"), td)
435444
t.Chdir(td)
@@ -455,7 +464,7 @@ func TestInit_json(t *testing.T) {
455464
}
456465

457466
func TestInit_getUpgradeModules(t *testing.T) {
458-
// Create a temporary working directory that is empty
467+
// Create a temporary working directory and copy in test fixtures
459468
td := t.TempDir()
460469
testCopyDir(t, testFixturePath("init-get"), td)
461470
t.Chdir(td)
@@ -486,8 +495,9 @@ func TestInit_getUpgradeModules(t *testing.T) {
486495
}
487496
}
488497

489-
func TestInit_backend(t *testing.T) {
490-
// Create a temporary working directory that is empty
498+
// Test initializing a backend from config (new working directory with no pre-existing backend state file).
499+
func TestInit_backend_initFromConfig(t *testing.T) {
500+
// Create a temporary working directory and copy in test fixtures
491501
td := t.TempDir()
492502
testCopyDir(t, testFixturePath("init-backend"), td)
493503
t.Chdir(td)
@@ -512,6 +522,40 @@ func TestInit_backend(t *testing.T) {
512522
}
513523
}
514524

525+
// Test init when the -backend=false flag is present (backend state file is used instead of the config).
526+
func TestInit_backend_initFromState(t *testing.T) {
527+
td := t.TempDir()
528+
testCopyDir(t, testFixturePath("init-backend-config-file-change-to-s3"), td)
529+
t.Chdir(td)
530+
531+
ui := new(cli.MockUi)
532+
view, done := testView(t)
533+
c := &InitCommand{
534+
Meta: Meta{
535+
testingOverrides: metaOverridesForProvider(testProvider()),
536+
Ui: ui,
537+
View: view,
538+
},
539+
}
540+
541+
args := []string{
542+
"-backend=false",
543+
}
544+
if code := c.Run(args); code != 0 {
545+
t.Fatalf("bad: \n%s", done(t).All())
546+
}
547+
548+
// Double check that the successful init above was due to ignoring the config.
549+
// When we don't provide -backend=false there should be an error due to a config change being detected;
550+
// the config specifies an s3 backend instead of local.
551+
args = []string{}
552+
view, done = testView(t)
553+
c.View = view
554+
if code := c.Run(args); code != 1 {
555+
t.Fatalf("bad, expected a 'Backend configuration changed' error but command succeeded : \n%s", done(t).All())
556+
}
557+
}
558+
515559
// regression test for https://github.com/hashicorp/terraform/issues/38027
516560
func TestInit_backend_migration_stateMgr_error(t *testing.T) {
517561
// Create a temporary working directory that is empty
@@ -591,7 +635,7 @@ func TestInit_backend_migration_stateMgr_error(t *testing.T) {
591635
}
592636

593637
func TestInit_backendUnset(t *testing.T) {
594-
// Create a temporary working directory that is empty
638+
// Create a temporary working directory and copy in test fixtures
595639
td := t.TempDir()
596640
testCopyDir(t, testFixturePath("init-backend"), td)
597641
t.Chdir(td)
@@ -661,7 +705,7 @@ func TestInit_backendUnset(t *testing.T) {
661705
}
662706

663707
func TestInit_backendConfigFile(t *testing.T) {
664-
// Create a temporary working directory that is empty
708+
// Create a temporary working directory and copy in test fixtures
665709
td := t.TempDir()
666710
testCopyDir(t, testFixturePath("init-backend-config-file"), td)
667711
t.Chdir(td)
@@ -797,7 +841,7 @@ func TestInit_backendConfigFile(t *testing.T) {
797841
}
798842

799843
func TestInit_backendConfigFilePowershellConfusion(t *testing.T) {
800-
// Create a temporary working directory that is empty
844+
// Create a temporary working directory and copy in test fixtures
801845
td := t.TempDir()
802846
testCopyDir(t, testFixturePath("init-backend-config-file"), td)
803847
t.Chdir(td)
@@ -833,7 +877,7 @@ func TestInit_backendConfigFilePowershellConfusion(t *testing.T) {
833877
}
834878

835879
func TestInit_backendReconfigure(t *testing.T) {
836-
// Create a temporary working directory that is empty
880+
// Create a temporary working directory and copy in test fixtures
837881
td := t.TempDir()
838882
testCopyDir(t, testFixturePath("init-backend"), td)
839883
t.Chdir(td)
@@ -880,7 +924,7 @@ func TestInit_backendReconfigure(t *testing.T) {
880924
}
881925

882926
func TestInit_backendConfigFileChange(t *testing.T) {
883-
// Create a temporary working directory that is empty
927+
// Create a temporary working directory and copy in test fixtures
884928
td := t.TempDir()
885929
testCopyDir(t, testFixturePath("init-backend-config-file-change"), td)
886930
t.Chdir(td)
@@ -908,7 +952,7 @@ func TestInit_backendConfigFileChange(t *testing.T) {
908952
}
909953

910954
func TestInit_backendMigrateWhileLocked(t *testing.T) {
911-
// Create a temporary working directory that is empty
955+
// Create a temporary working directory and copy in test fixtures
912956
td := t.TempDir()
913957
testCopyDir(t, testFixturePath("init-backend-migrate-while-locked"), td)
914958
t.Chdir(td)
@@ -961,7 +1005,7 @@ func TestInit_backendMigrateWhileLocked(t *testing.T) {
9611005
}
9621006

9631007
func TestInit_backendConfigFileChangeWithExistingState(t *testing.T) {
964-
// Create a temporary working directory that is empty
1008+
// Create a temporary working directory and copy in test fixtures
9651009
td := t.TempDir()
9661010
testCopyDir(t, testFixturePath("init-backend-config-file-change-migrate-existing"), td)
9671011
t.Chdir(td)
@@ -998,7 +1042,7 @@ func TestInit_backendConfigFileChangeWithExistingState(t *testing.T) {
9981042
}
9991043

10001044
func TestInit_backendConfigKV(t *testing.T) {
1001-
// Create a temporary working directory that is empty
1045+
// Create a temporary working directory and copy in test fixtures
10021046
td := t.TempDir()
10031047
testCopyDir(t, testFixturePath("init-backend-config-kv"), td)
10041048
t.Chdir(td)
@@ -1026,7 +1070,7 @@ func TestInit_backendConfigKV(t *testing.T) {
10261070
}
10271071

10281072
func TestInit_backendConfigKVReInit(t *testing.T) {
1029-
// Create a temporary working directory that is empty
1073+
// Create a temporary working directory and copy in test fixtures
10301074
td := t.TempDir()
10311075
testCopyDir(t, testFixturePath("init-backend-config-kv"), td)
10321076
t.Chdir(td)
@@ -1089,7 +1133,7 @@ func TestInit_backendConfigKVReInit(t *testing.T) {
10891133
}
10901134

10911135
func TestInit_backendConfigKVReInitWithConfigDiff(t *testing.T) {
1092-
// Create a temporary working directory that is empty
1136+
// Create a temporary working directory and copy in test fixtures
10931137
td := t.TempDir()
10941138
testCopyDir(t, testFixturePath("init-backend"), td)
10951139
t.Chdir(td)
@@ -1137,7 +1181,7 @@ func TestInit_backendConfigKVReInitWithConfigDiff(t *testing.T) {
11371181
}
11381182

11391183
func TestInit_backendCli_no_config_block(t *testing.T) {
1140-
// Create a temporary working directory that is empty
1184+
// Create a temporary working directory and copy in test fixtures
11411185
td := t.TempDir()
11421186
testCopyDir(t, testFixturePath("init"), td)
11431187
t.Chdir(td)
@@ -1658,7 +1702,7 @@ func TestInit_inputFalse(t *testing.T) {
16581702
}
16591703

16601704
func TestInit_getProvider(t *testing.T) {
1661-
// Create a temporary working directory that is empty
1705+
// Create a temporary working directory and copy in test fixtures
16621706
td := t.TempDir()
16631707
testCopyDir(t, testFixturePath("init-get-providers"), td)
16641708
t.Chdir(td)
@@ -1765,7 +1809,7 @@ func TestInit_getProvider(t *testing.T) {
17651809
}
17661810

17671811
func TestInit_getProviderSource(t *testing.T) {
1768-
// Create a temporary working directory that is empty
1812+
// Create a temporary working directory and copy in test fixtures
17691813
td := t.TempDir()
17701814
testCopyDir(t, testFixturePath("init-get-provider-source"), td)
17711815
t.Chdir(td)
@@ -1815,7 +1859,7 @@ func TestInit_getProviderSource(t *testing.T) {
18151859
}
18161860

18171861
func TestInit_getProviderLegacyFromState(t *testing.T) {
1818-
// Create a temporary working directory that is empty
1862+
// Create a temporary working directory and copy in test fixtures
18191863
td := t.TempDir()
18201864
testCopyDir(t, testFixturePath("init-get-provider-legacy-from-state"), td)
18211865
t.Chdir(td)
@@ -1857,7 +1901,7 @@ func TestInit_getProviderLegacyFromState(t *testing.T) {
18571901
}
18581902

18591903
func TestInit_getProviderInvalidPackage(t *testing.T) {
1860-
// Create a temporary working directory that is empty
1904+
// Create a temporary working directory and copy in test fixtures
18611905
td := t.TempDir()
18621906
testCopyDir(t, testFixturePath("init-get-provider-invalid-package"), td)
18631907
t.Chdir(td)
@@ -1921,7 +1965,7 @@ func TestInit_getProviderInvalidPackage(t *testing.T) {
19211965
}
19221966

19231967
func TestInit_getProviderDetectedLegacy(t *testing.T) {
1924-
// Create a temporary working directory that is empty
1968+
// Create a temporary working directory and copy in test fixtures
19251969
td := t.TempDir()
19261970
testCopyDir(t, testFixturePath("init-get-provider-detected-legacy"), td)
19271971
t.Chdir(td)
@@ -1990,7 +2034,7 @@ func TestInit_getProviderDetectedLegacy(t *testing.T) {
19902034
}
19912035

19922036
func TestInit_providerSource(t *testing.T) {
1993-
// Create a temporary working directory that is empty
2037+
// Create a temporary working directory and copy in test fixtures
19942038
td := t.TempDir()
19952039
testCopyDir(t, testFixturePath("init-required-providers"), td)
19962040
t.Chdir(td)
@@ -2184,7 +2228,7 @@ func TestInit_cancelProviders(t *testing.T) {
21842228
}
21852229

21862230
func TestInit_getUpgradePlugins(t *testing.T) {
2187-
// Create a temporary working directory that is empty
2231+
// Create a temporary working directory and copy in test fixtures
21882232
td := t.TempDir()
21892233
testCopyDir(t, testFixturePath("init-get-providers"), td)
21902234
t.Chdir(td)
@@ -2309,7 +2353,7 @@ func TestInit_getUpgradePlugins(t *testing.T) {
23092353
}
23102354

23112355
func TestInit_getProviderMissing(t *testing.T) {
2312-
// Create a temporary working directory that is empty
2356+
// Create a temporary working directory and copy in test fixtures
23132357
td := t.TempDir()
23142358
testCopyDir(t, testFixturePath("init-get-providers"), td)
23152359
t.Chdir(td)
@@ -2350,7 +2394,7 @@ func TestInit_getProviderMissing(t *testing.T) {
23502394
}
23512395

23522396
func TestInit_checkRequiredVersion(t *testing.T) {
2353-
// Create a temporary working directory that is empty
2397+
// Create a temporary working directory and copy in test fixtures
23542398
td := t.TempDir()
23552399
testCopyDir(t, testFixturePath("init-check-required-version"), td)
23562400
t.Chdir(td)
@@ -2432,7 +2476,7 @@ func TestInit_checkRequiredVersionFirst(t *testing.T) {
24322476
}
24332477

24342478
func TestInit_providerLockFile(t *testing.T) {
2435-
// Create a temporary working directory that is empty
2479+
// Create a temporary working directory and copy in test fixtures
24362480
td := t.TempDir()
24372481
testCopyDir(t, testFixturePath("init-provider-lock-file"), td)
24382482
// The temporary directory does not have write permission (dr-xr-xr-x) after the copy
@@ -2621,7 +2665,7 @@ provider "registry.terraform.io/hashicorp/test" {
26212665

26222666
for _, tc := range cases {
26232667
t.Run(tc.desc, func(t *testing.T) {
2624-
// Create a temporary working directory that is empty
2668+
// Create a temporary working directory and copy in test fixtures
26252669
td := t.TempDir()
26262670
testCopyDir(t, testFixturePath(tc.fixture), td)
26272671
t.Chdir(td)
@@ -3160,7 +3204,7 @@ func TestInit_testsWithExternalProviders(t *testing.T) {
31603204
}
31613205

31623206
func TestInit_tests(t *testing.T) {
3163-
// Create a temporary working directory that is empty
3207+
// Create a temporary working directory and copy in test fixtures
31643208
td := t.TempDir()
31653209
testCopyDir(t, testFixturePath("init-with-tests"), td)
31663210
t.Chdir(td)
@@ -3190,7 +3234,7 @@ func TestInit_tests(t *testing.T) {
31903234
}
31913235

31923236
func TestInit_testsWithProvider(t *testing.T) {
3193-
// Create a temporary working directory that is empty
3237+
// Create a temporary working directory and copy in test fixtures
31943238
td := t.TempDir()
31953239
testCopyDir(t, testFixturePath("init-with-tests-with-provider"), td)
31963240
t.Chdir(td)
@@ -3298,7 +3342,7 @@ func TestInit_testsWithInvalidRequiredProviders(t *testing.T) {
32983342
}
32993343

33003344
func TestInit_testsWithModule(t *testing.T) {
3301-
// Create a temporary working directory that is empty
3345+
// Create a temporary working directory and copy in test fixtures
33023346
td := t.TempDir()
33033347
testCopyDir(t, testFixturePath("init-with-tests-with-module"), td)
33043348
t.Chdir(td)
@@ -4183,7 +4227,7 @@ func TestInit_stateStore_providerUpgrade(t *testing.T) {
41834227
}
41844228

41854229
func TestInit_stateStore_unset(t *testing.T) {
4186-
// Create a temporary working directory that is empty
4230+
// Create a temporary working directory and copy in test fixtures
41874231
td := t.TempDir()
41884232
testCopyDir(t, testFixturePath("init-state-store"), td)
41894233
t.Chdir(td)
@@ -4284,7 +4328,7 @@ func TestInit_stateStore_unset(t *testing.T) {
42844328
}
42854329

42864330
func TestInit_stateStore_unset_withoutProviderRequirements(t *testing.T) {
4287-
// Create a temporary working directory that is empty
4331+
// Create a temporary working directory and copy in test fixtures
42884332
td := t.TempDir()
42894333
testCopyDir(t, testFixturePath("init-state-store"), td)
42904334
t.Chdir(td)
@@ -4386,7 +4430,7 @@ func TestInit_stateStore_unset_withoutProviderRequirements(t *testing.T) {
43864430
}
43874431

43884432
func TestInit_stateStore_to_backend(t *testing.T) {
4389-
// Create a temporary working directory that is empty
4433+
// Create a temporary working directory and copy in test fixtures
43904434
td := t.TempDir()
43914435
testCopyDir(t, testFixturePath("init-state-store"), td)
43924436
t.Chdir(td)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 3,
3+
"terraform_version": "1.15.0",
4+
"backend": {
5+
"type": "local",
6+
"config": {
7+
"path": "local-state.tfstate"
8+
},
9+
"hash": 4282859327
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
terraform {
2+
backend "s3" {
3+
// No config, as this fixture is intended for tests using -backend=false,
4+
// so the fact this is completely unconfigured should not cause an error.
5+
}
6+
}

0 commit comments

Comments
 (0)