Skip to content

Commit 530c035

Browse files
Allow use of ${terraform.workspace} in tests (#99)
I'm working on a plugin which checks that resource names include `${terraform.workspace}` (if it's set) - https://github.com/richardTowers/tflint-ruleset-workspaces/ Currently it's not possible to test this functionality, because (unlike tflint) tflint-plugin-sdk doesn't support the `${terraform.workspace}` expression. Fixing #64 would be nicer, but as this is quite self contained I think it's okay to add this feature to the TestRunner.
1 parent b11b475 commit 530c035

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

helper/runner.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package helper
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/hashicorp/go-version"
78
"github.com/hashicorp/hcl/v2"
@@ -187,9 +188,16 @@ func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}, wantTy *cty.
187188
for _, variable := range r.tfconfig.Module.Variables {
188189
variables[variable.Name] = variable.Default
189190
}
191+
workspace, success := os.LookupEnv("TERRAFORM_WORKSPACE")
192+
if !success {
193+
workspace = "default"
194+
}
190195
rawVal, diags := expr.Value(&hcl.EvalContext{
191196
Variables: map[string]cty.Value{
192197
"var": cty.ObjectVal(variables),
198+
"terraform": cty.ObjectVal(map[string]cty.Value{
199+
"workspace": cty.StringVal(workspace),
200+
}),
193201
},
194202
})
195203
if diags.HasErrors() {

0 commit comments

Comments
 (0)