diff --git a/.changes/v1.14/BUG FIXES-20251031-144915.yaml b/.changes/v1.14/BUG FIXES-20251031-144915.yaml new file mode 100644 index 000000000000..8bc6c766e30e --- /dev/null +++ b/.changes/v1.14/BUG FIXES-20251031-144915.yaml @@ -0,0 +1,5 @@ +kind: BUG FIXES +body: 'providers lock: include providers required by terraform test' +time: 2025-10-31T14:49:15.121756+01:00 +custom: + Issue: "37851" diff --git a/internal/command/providers_lock.go b/internal/command/providers_lock.go index 970a92881b38..aba227122c87 100644 --- a/internal/command/providers_lock.go +++ b/internal/command/providers_lock.go @@ -44,10 +44,12 @@ func (c *ProvidersLockCommand) Run(args []string) int { var optPlatforms arguments.FlagStringSlice var fsMirrorDir string var netMirrorURL string + var testDirectory string cmdFlags.Var(&optPlatforms, "platform", "target platform") cmdFlags.StringVar(&fsMirrorDir, "fs-mirror", "", "filesystem mirror directory") cmdFlags.StringVar(&netMirrorURL, "net-mirror", "", "network mirror base URL") + cmdFlags.StringVar(&testDirectory, "test-directory", "tests", "test-directory") pluginCache := cmdFlags.Bool("enable-plugin-cache", false, "") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { @@ -123,7 +125,7 @@ func (c *ProvidersLockCommand) Run(args []string) int { source = getproviders.NewRegistrySource(c.Services) } - config, confDiags := c.loadConfig(".") + config, confDiags := c.loadConfigWithTests(".", testDirectory) diags = diags.Append(confDiags) reqs, hclDiags := config.ProviderRequirements() diags = diags.Append(hclDiags) @@ -416,6 +418,8 @@ Options: -enable-plugin-cache Enable the usage of the globally configured plugin cache. This will speed up the locking process, but the providers won't be loaded from an authoritative source. + + -test-directory=path Set the Terraform test directory, defaults to "tests". ` } diff --git a/internal/command/providers_lock_test.go b/internal/command/providers_lock_test.go index 6e2ace31db74..5125c92af8db 100644 --- a/internal/command/providers_lock_test.go +++ b/internal/command/providers_lock_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/hashicorp/cli" + "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/depsfile" "github.com/hashicorp/terraform/internal/getproviders" @@ -50,7 +51,7 @@ provider "registry.terraform.io/hashicorp/test" { ] } ` - runProviderLockGenericTest(t, testDirectory, expected) + runProviderLockGenericTest(t, testDirectory, expected, false) }) // This test depends on the -fs-mirror argument, so we always know what results to expect @@ -67,11 +68,27 @@ provider "registry.terraform.io/hashicorp/test" { ] } ` - runProviderLockGenericTest(t, testDirectory, expected) + runProviderLockGenericTest(t, testDirectory, expected, false) + }) + + // This test depends on the -fs-mirror argument, so we always know what results to expect + t.Run("tests", func(t *testing.T) { + testDirectory := "providers-lock/with-tests" + expected := `# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/test" { + version = "1.0.0" + hashes = [ + "h1:7MjN4eFisdTv4tlhXH5hL4QQd39Jy4baPhFxwAd/EFE=", + ] +} +` + runProviderLockGenericTest(t, testDirectory, expected, true) }) } -func runProviderLockGenericTest(t *testing.T, testDirectory, expected string) { +func runProviderLockGenericTest(t *testing.T, testDirectory, expected string, init bool) { td := t.TempDir() testCopyDir(t, testFixturePath(testDirectory), td) t.Chdir(td) @@ -86,6 +103,20 @@ func runProviderLockGenericTest(t *testing.T, testDirectory, expected string) { t.Fatalf("unexpected error: %s", err) } + if init { + // optionally execute the get command to fetch local modules if the + // test case needs them + c := &GetCommand{ + Meta: Meta{ + Ui: new(cli.MockUi), + }, + } + code := c.Run(nil) + if code != 0 { + t.Fatal("failed get command") + } + } + p := testProvider() ui := new(cli.MockUi) c := &ProvidersLockCommand{ diff --git a/internal/command/testdata/providers-lock/with-tests/fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/os_arch/terraform-provider-test b/internal/command/testdata/providers-lock/with-tests/fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/os_arch/terraform-provider-test new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/internal/command/testdata/providers-lock/with-tests/main.tf b/internal/command/testdata/providers-lock/with-tests/main.tf new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/internal/command/testdata/providers-lock/with-tests/main.tftest.hcl b/internal/command/testdata/providers-lock/with-tests/main.tftest.hcl new file mode 100644 index 000000000000..a7dab355fddc --- /dev/null +++ b/internal/command/testdata/providers-lock/with-tests/main.tftest.hcl @@ -0,0 +1,5 @@ +run "test" { + module { + source = "./testing" + } +} diff --git a/internal/command/testdata/providers-lock/with-tests/testing/main.tf b/internal/command/testdata/providers-lock/with-tests/testing/main.tf new file mode 100644 index 000000000000..e75a64c0ca0a --- /dev/null +++ b/internal/command/testdata/providers-lock/with-tests/testing/main.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + test = { + source = "hashicorp/test" + } + } +} \ No newline at end of file