Skip to content

Commit

Permalink
Allow use of ${terraform.workspace} in tests (#99)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
richardTowers authored Jan 24, 2021
1 parent b11b475 commit 530c035
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"fmt"
"os"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -187,9 +188,16 @@ func (r *Runner) EvaluateExpr(expr hcl.Expression, ret interface{}, wantTy *cty.
for _, variable := range r.tfconfig.Module.Variables {
variables[variable.Name] = variable.Default
}
workspace, success := os.LookupEnv("TERRAFORM_WORKSPACE")
if !success {
workspace = "default"
}
rawVal, diags := expr.Value(&hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.ObjectVal(variables),
"terraform": cty.ObjectVal(map[string]cty.Value{
"workspace": cty.StringVal(workspace),
}),
},
})
if diags.HasErrors() {
Expand Down

0 comments on commit 530c035

Please sign in to comment.