providers lock: include test files when loading configuration (#37904)

Co-authored-by: Liam Cervante <liam.cervante@hashicorp.com>
pull/37900/head
github-actions[bot] 3 months ago committed by GitHub
parent e2327fdc31
commit b0ebd07f11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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"

@ -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)
@ -415,7 +417,9 @@ Options:
-enable-plugin-cache Enable the usage of the globally configured plugin cache.
This will speed up the locking process, but the providers
wont be loaded from an authoritative source.
won't be loaded from an authoritative source.
-test-directory=path Set the Terraform test directory, defaults to "tests".
`
}

@ -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{

@ -0,0 +1,5 @@
run "test" {
module {
source = "./testing"
}
}

@ -0,0 +1,7 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}
Loading…
Cancel
Save