From cb72a8e1d2f9185b0319dea146e6a9fa244446fb Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 20 Mar 2026 18:07:13 +0100 Subject: [PATCH] command: test `providers schema` with const variables --- internal/command/providers_schema_test.go | 99 +++++++++++++++++++ .../.terraform/environment | 1 + 2 files changed, 100 insertions(+) create mode 100644 internal/command/testdata/dynamic-module-sources/command-with-const-var-cloud-backend/.terraform/environment diff --git a/internal/command/providers_schema_test.go b/internal/command/providers_schema_test.go index 4afbc1b2ab..7ac33d6112 100644 --- a/internal/command/providers_schema_test.go +++ b/internal/command/providers_schema_test.go @@ -18,6 +18,10 @@ import ( "github.com/zclconf/go-cty/cty" "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/backend" + backendInit "github.com/hashicorp/terraform/internal/backend/init" + backendCloud "github.com/hashicorp/terraform/internal/cloud" + "github.com/hashicorp/terraform/internal/configs/configschema" "github.com/hashicorp/terraform/internal/providers" testing_provider "github.com/hashicorp/terraform/internal/providers/testing" @@ -211,6 +215,101 @@ func TestProvidersSchema_output_withStateStore(t *testing.T) { } } +func TestProvidersSchema_constVariable(t *testing.T) { + t.Run("missing value", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &ProvidersSchemaCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{"-json"} + if code := c.Run(args); code == 0 { + t.Fatalf("expected error, got 0") + } + + errStr := ui.ErrorWriter.String() + if !strings.Contains(errStr, "No value for required variable") { + t.Fatalf("expected missing variable error, got: %s", errStr) + } + }) + + t.Run("value via cli", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &ProvidersSchemaCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{"-json", "-var", "module_name=child"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + wantOutput := []string{ + `"registry.terraform.io/hashicorp/test"`, + } + + for _, want := range wantOutput { + if !strings.Contains(output, want) { + t.Fatalf("output missing %s:\n%s", want, output) + } + } + }) + + t.Run("value via backend", func(t *testing.T) { + server := cloudTestServerWithVars(t) + defer server.Close() + d := testDisco(server) + + previousBackend := backendInit.Backend("cloud") + backendInit.Set("cloud", func() backend.Backend { return backendCloud.New(d) }) + defer backendInit.Set("cloud", previousBackend) + + wd := tempWorkingDirFixture(t, "dynamic-module-sources/command-with-const-var-cloud-backend") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &ProvidersSchemaCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + Services: d, + }, + } + + args := []string{"-json"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + wantOutput := []string{ + `"registry.terraform.io/hashicorp/test"`, + } + + for _, want := range wantOutput { + if !strings.Contains(output, want) { + t.Fatalf("output missing %s:\n%s", want, output) + } + } + }) +} + type providerSchemas struct { FormatVersion string `json:"format_version"` Schemas map[string]providerSchema `json:"provider_schemas"` diff --git a/internal/command/testdata/dynamic-module-sources/command-with-const-var-cloud-backend/.terraform/environment b/internal/command/testdata/dynamic-module-sources/command-with-const-var-cloud-backend/.terraform/environment new file mode 100644 index 0000000000..9daeafb986 --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/command-with-const-var-cloud-backend/.terraform/environment @@ -0,0 +1 @@ +test