Skip to content

Commit 4bfb3ab

Browse files
liamcervantedsa0x
authored andcommitted
providers lock: include test files when loading configuration
1 parent bf169b8 commit 4bfb3ab

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'providers lock: include providers required by terraform test'
3+
time: 2025-10-31T14:49:15.121756+01:00
4+
custom:
5+
Issue: "37851"

internal/command/providers_lock.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ func (c *ProvidersLockCommand) Run(args []string) int {
4444
var optPlatforms arguments.FlagStringSlice
4545
var fsMirrorDir string
4646
var netMirrorURL string
47+
var testDirectory string
4748

4849
cmdFlags.Var(&optPlatforms, "platform", "target platform")
4950
cmdFlags.StringVar(&fsMirrorDir, "fs-mirror", "", "filesystem mirror directory")
5051
cmdFlags.StringVar(&netMirrorURL, "net-mirror", "", "network mirror base URL")
52+
cmdFlags.StringVar(&testDirectory, "test-directory", "tests", "test-directory")
5153
pluginCache := cmdFlags.Bool("enable-plugin-cache", false, "")
5254
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
5355
if err := cmdFlags.Parse(args); err != nil {
@@ -123,7 +125,7 @@ func (c *ProvidersLockCommand) Run(args []string) int {
123125
source = getproviders.NewRegistrySource(c.Services)
124126
}
125127

126-
config, confDiags := c.loadConfig(".")
128+
config, confDiags := c.loadConfigWithTests(".", testDirectory)
127129
diags = diags.Append(confDiags)
128130
reqs, hclDiags := config.ProviderRequirements()
129131
diags = diags.Append(hclDiags)
@@ -415,7 +417,9 @@ Options:
415417
416418
-enable-plugin-cache Enable the usage of the globally configured plugin cache.
417419
This will speed up the locking process, but the providers
418-
wont be loaded from an authoritative source.
420+
won't be loaded from an authoritative source.
421+
422+
-test-directory=path Set the Terraform test directory, defaults to "tests".
419423
`
420424
}
421425

internal/command/providers_lock_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"github.com/hashicorp/cli"
15+
1516
"github.com/hashicorp/terraform/internal/addrs"
1617
"github.com/hashicorp/terraform/internal/depsfile"
1718
"github.com/hashicorp/terraform/internal/getproviders"
@@ -50,7 +51,7 @@ provider "registry.terraform.io/hashicorp/test" {
5051
]
5152
}
5253
`
53-
runProviderLockGenericTest(t, testDirectory, expected)
54+
runProviderLockGenericTest(t, testDirectory, expected, false)
5455
})
5556

5657
// 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" {
6768
]
6869
}
6970
`
70-
runProviderLockGenericTest(t, testDirectory, expected)
71+
runProviderLockGenericTest(t, testDirectory, expected, false)
72+
})
73+
74+
// This test depends on the -fs-mirror argument, so we always know what results to expect
75+
t.Run("tests", func(t *testing.T) {
76+
testDirectory := "providers-lock/with-tests"
77+
expected := `# This file is maintained automatically by "terraform init".
78+
# Manual edits may be lost in future updates.
79+
80+
provider "registry.terraform.io/hashicorp/test" {
81+
version = "1.0.0"
82+
hashes = [
83+
"h1:7MjN4eFisdTv4tlhXH5hL4QQd39Jy4baPhFxwAd/EFE=",
84+
]
85+
}
86+
`
87+
runProviderLockGenericTest(t, testDirectory, expected, true)
7188
})
7289
}
7390

74-
func runProviderLockGenericTest(t *testing.T, testDirectory, expected string) {
91+
func runProviderLockGenericTest(t *testing.T, testDirectory, expected string, init bool) {
7592
td := t.TempDir()
7693
testCopyDir(t, testFixturePath(testDirectory), td)
7794
t.Chdir(td)
@@ -86,6 +103,20 @@ func runProviderLockGenericTest(t *testing.T, testDirectory, expected string) {
86103
t.Fatalf("unexpected error: %s", err)
87104
}
88105

106+
if init {
107+
// optionally execute the get command to fetch local modules if the
108+
// test case needs them
109+
c := &GetCommand{
110+
Meta: Meta{
111+
Ui: new(cli.MockUi),
112+
},
113+
}
114+
code := c.Run(nil)
115+
if code != 0 {
116+
t.Fatal("failed get command")
117+
}
118+
}
119+
89120
p := testProvider()
90121
ui := new(cli.MockUi)
91122
c := &ProvidersLockCommand{

internal/command/testdata/providers-lock/with-tests/fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/os_arch/terraform-provider-test

Whitespace-only changes.

internal/command/testdata/providers-lock/with-tests/main.tf

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
run "test" {
2+
module {
3+
source = "./testing"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "hashicorp/test"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)