From 0d751be30b5bf6fb68564b1829505c045d3602a8 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Thu, 19 Mar 2026 11:00:36 +0100 Subject: [PATCH] command: test `get` with const variables --- internal/command/get_test.go | 90 +++++++++++++++++++ .../.terraform/terraform.tfstate | 18 ++++ .../get-const-var-backend/main.tf | 17 ++++ .../modules/example/empty.tf | 1 + .../get-const-var/main.tf | 8 ++ .../get-const-var/modules/example/empty.tf | 1 + 6 files changed, 135 insertions(+) create mode 100644 internal/command/testdata/dynamic-module-sources/get-const-var-backend/.terraform/terraform.tfstate create mode 100644 internal/command/testdata/dynamic-module-sources/get-const-var-backend/main.tf create mode 100644 internal/command/testdata/dynamic-module-sources/get-const-var-backend/modules/example/empty.tf create mode 100644 internal/command/testdata/dynamic-module-sources/get-const-var/main.tf create mode 100644 internal/command/testdata/dynamic-module-sources/get-const-var/modules/example/empty.tf diff --git a/internal/command/get_test.go b/internal/command/get_test.go index 81555ae843..71de3cf3f7 100644 --- a/internal/command/get_test.go +++ b/internal/command/get_test.go @@ -8,6 +8,10 @@ import ( "testing" "github.com/hashicorp/cli" + + "github.com/hashicorp/terraform/internal/backend" + backendInit "github.com/hashicorp/terraform/internal/backend/init" + backendCloud "github.com/hashicorp/terraform/internal/cloud" ) func TestGet(t *testing.T) { @@ -113,3 +117,89 @@ func TestGet_cancel(t *testing.T) { t.Fatalf("wrong error message\nshould contain: %s\ngot:\n%s", want, got) } } + +func TestGet_constVariable(t *testing.T) { + // Scenario 1: no value for variable -> diagnostic + t.Run("missing value", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/get-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &GetCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{} + 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) + } + }) + + // Scenario 2: value via cli -> works + t.Run("value via cli", func(t *testing.T) { + wd := tempWorkingDirFixture(t, "dynamic-module-sources/get-const-var") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &GetCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + }, + } + + args := []string{"-var", "module_name=example"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + if !strings.Contains(output, "- example in") { + t.Fatalf("doesn't look like get: %s", output) + } + }) + + // Scenario 3: value via backend + 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/get-const-var-backend") + t.Chdir(wd.RootModuleDir()) + + ui := cli.NewMockUi() + c := &GetCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + WorkingDir: wd, + Services: d, + }, + } + + args := []string{} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + output := ui.OutputWriter.String() + if !strings.Contains(output, "- example in") { + t.Fatalf("doesn't look like get: %s", output) + } + }) +} diff --git a/internal/command/testdata/dynamic-module-sources/get-const-var-backend/.terraform/terraform.tfstate b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/.terraform/terraform.tfstate new file mode 100644 index 0000000000..bc5581ba3b --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/.terraform/terraform.tfstate @@ -0,0 +1,18 @@ +{ + "version": 3, + "terraform_version": "1.15.0", + "backend": { + "type": "cloud", + "config": { + "hostname": null, + "organization": "hashicorp", + "token": null, + "workspaces": { + "name": "test", + "project": null, + "tags": null + } + }, + "hash": 1816475682 + } +} diff --git a/internal/command/testdata/dynamic-module-sources/get-const-var-backend/main.tf b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/main.tf new file mode 100644 index 0000000000..b8fa6fcd70 --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/main.tf @@ -0,0 +1,17 @@ +terraform { + cloud { + organization = "hashicorp" + workspaces { + name = "test" + } + } +} + +variable "module_name" { + type = string + const = true +} + +module "example" { + source = "./modules/${var.module_name}" +} diff --git a/internal/command/testdata/dynamic-module-sources/get-const-var-backend/modules/example/empty.tf b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/modules/example/empty.tf new file mode 100644 index 0000000000..d15abba597 --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/get-const-var-backend/modules/example/empty.tf @@ -0,0 +1 @@ +// Empty diff --git a/internal/command/testdata/dynamic-module-sources/get-const-var/main.tf b/internal/command/testdata/dynamic-module-sources/get-const-var/main.tf new file mode 100644 index 0000000000..9b60651f81 --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/get-const-var/main.tf @@ -0,0 +1,8 @@ +variable "module_name" { + type = string + const = true +} + +module "example" { + source = "./modules/${var.module_name}" +} diff --git a/internal/command/testdata/dynamic-module-sources/get-const-var/modules/example/empty.tf b/internal/command/testdata/dynamic-module-sources/get-const-var/modules/example/empty.tf new file mode 100644 index 0000000000..d15abba597 --- /dev/null +++ b/internal/command/testdata/dynamic-module-sources/get-const-var/modules/example/empty.tf @@ -0,0 +1 @@ +// Empty