Test for local variables in provider block

Signed-off-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
This commit is contained in:
Ilia Gogotchuri 2025-02-25 13:27:23 +04:00
parent 3d635da47f
commit c692d072b3
No known key found for this signature in database
5 changed files with 29 additions and 1 deletions

View File

@ -170,6 +170,10 @@ func TestTest(t *testing.T) {
expected: "1 passed, 0 failed.",
code: 0,
},
"local_variables_in_provider_block": {
expected: "1 passed, 0 failed.",
code: 0,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {

View File

@ -0,0 +1,8 @@
variable "input" {
type = string
default = "bar"
}
resource "test_resource" "foo" {
value = var.input
}

View File

@ -0,0 +1,13 @@
variables {
username = "u"
password = "p"
}
provider "test" {
username = var.username
password = var.password
}
run "validate" {}

View File

@ -28,6 +28,8 @@ var (
Attributes: map[string]*configschema.Attribute{
"data_prefix": {Type: cty.String, Optional: true},
"resource_prefix": {Type: cty.String, Optional: true},
"username": {Type: cty.String, Optional: true},
"password": {Type: cty.String, Optional: true},
},
},
},

View File

@ -1098,10 +1098,11 @@ func (c *Config) getTransformAddVariablesForTest(evalCtx *hcl.EvalContext) confi
newVars := make(map[string]*Variable, len(c.Module.Variables)+len(file.Variables))
for k, v := range c.Module.Variables {
oldVars[k] = v
newVars[k] = v
}
for variableName, variableExpr := range file.Variables {
// Skip if variable already exists in the config.
if v, ok := oldVars[variableName]; ok {
if v, ok := newVars[variableName]; ok {
newVars[variableName] = v
continue
}