Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.14/BUG FIXES-20251031-144915.yaml
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 5 additions & 1 deletion internal/command/providers_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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".
`
}

Expand Down
37 changes: 34 additions & 3 deletions internal/command/providers_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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{
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run "test" {
module {
source = "./testing"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}
Loading