From cf9c491d9ff3fe91d6eb71c73563617615fde30d Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Mon, 8 Sep 2025 10:36:16 -0400 Subject: [PATCH] fix minor regression in tests (#37570) At one point, these tests were explicitly passing an empty cli config filepath so that terraform would ignore any existing cli config file (only relevant if you are running these tests locally), but the behavior changed over time such that it was no longer working for these tests (now if the env var file path is empty, we fall back to the default). The actual behavior is reasonably correct (I've depended on passing in blank config file paths before, so I don't love it, but I don't see a need to break this either) so I've added a small method that drops a blank file in place to fix the behavior and avoid file not found errors. --- internal/command/e2etest/init_test.go | 36 +++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/internal/command/e2etest/init_test.go b/internal/command/e2etest/init_test.go index d277a3b01e..96c52f783b 100644 --- a/internal/command/e2etest/init_test.go +++ b/internal/command/e2etest/init_test.go @@ -112,6 +112,12 @@ func TestInitProvidersVendored(t *testing.T) { t.Fatalf("unexpected error: %s", err) } + // "zero out" any existing cli config file by passing in an empty override + // file. + configFile := emptyConfigFileForTests(t, wantMachineDir) + tf.AddEnv(fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", configFile)) + defer os.Remove(configFile) + stdout, stderr, err := tf.Run("init") if err != nil { t.Errorf("unexpected error: %s", err) @@ -127,7 +133,6 @@ func TestInitProvidersVendored(t *testing.T) { if !strings.Contains(stdout, "- Installing hashicorp/null v1.0.0+local") { t.Errorf("provider download message is missing from output:\n%s", stdout) - t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)") } } @@ -144,12 +149,6 @@ func TestInitProvidersLocalOnly(t *testing.T) { fixturePath := filepath.Join("testdata", "local-only-provider") tf := e2e.NewBinary(t, terraformBin, fixturePath) - // If you run this test on a workstation with a plugin-cache directory - // configured, it will leave a bad directory behind and terraform init will - // not work until you remove it. - // - // To avoid this, we will "zero out" any existing cli config file. - tf.AddEnv("TF_CLI_CONFIG_FILE=") // Our fixture dir has a generic os_arch dir, which we need to customize // to the actual OS/arch where this test is running in order to get the @@ -161,6 +160,16 @@ func TestInitProvidersLocalOnly(t *testing.T) { t.Fatalf("unexpected error: %s", err) } + // If you run this test on a workstation with a plugin-cache directory + // configured, it will leave a bad directory behind and terraform init will + // not work until you remove it. + // + // To avoid this, we will "zero out" any existing cli config file by + // passing in an empty override file. + configFile := emptyConfigFileForTests(t, wantMachineDir) + defer os.Remove(configFile) + tf.AddEnv(fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", configFile)) + stdout, stderr, err := tf.Run("init") if err != nil { t.Errorf("unexpected error: %s", err) @@ -409,3 +418,16 @@ func TestInitProviderWarnings(t *testing.T) { } } + +// emptyConfigFileForTests creates a blank .terraformrc file in the requested +// path and returns the path to the new file. It is the caller's responsibility +// to cleanup the file after use. +func emptyConfigFileForTests(t testing.TB, path string) string { + // "zero out" any existing cli config file by passing in an empty override + // file + configFile, err := os.Create(filepath.Join(path, ".terraformrc")) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + return configFile.Name() +}