Skip to content

Commit 919fa27

Browse files
authored
helper: Fix panic when helper runner uses variables (#149)
1 parent 1d4544f commit 919fa27

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

helper/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (r *Runner) EnsureNoError(err error, proc func() error) error {
194194
// NewLocalRunner initialises a new test runner.
195195
// Internal use only.
196196
func NewLocalRunner(files map[string]*hcl.File, issues Issues) *Runner {
197-
return &Runner{files: map[string]*hcl.File{}, Issues: issues}
197+
return &Runner{files: map[string]*hcl.File{}, variables: map[string]*Variable{}, Issues: issues}
198198
}
199199

200200
// AddLocalFile adds a new file to the current mapped files.

helper/runner_test.go

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,29 +248,55 @@ terraform {
248248
}
249249

250250
func Test_EvaluateExpr(t *testing.T) {
251-
src := `
251+
tests := []struct {
252+
Name string
253+
Src string
254+
Want string
255+
}{
256+
{
257+
Name: "string literal",
258+
Src: `
252259
resource "aws_instance" "foo" {
253260
instance_type = "t2.micro"
254-
}`
255-
256-
runner := TestRunner(t, map[string]string{"main.tf": src})
261+
}`,
262+
Want: "t2.micro",
263+
},
264+
{
265+
Name: "string interpolation",
266+
Src: `
267+
variable "instance_type" {
268+
default = "t2.micro"
269+
}
257270
258-
resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
259-
Attributes: []hclext.AttributeSchema{{Name: "instance_type"}},
260-
}, nil)
261-
if err != nil {
262-
t.Fatal(err)
271+
resource "aws_instance" "foo" {
272+
instance_type = var.instance_type
273+
}`,
274+
Want: "t2.micro",
275+
},
263276
}
264277

265-
for _, resource := range resources.Blocks {
266-
var instanceType string
267-
if err := runner.EvaluateExpr(resource.Body.Attributes["instance_type"].Expr, &instanceType, nil); err != nil {
268-
t.Fatal(err)
269-
}
278+
for _, test := range tests {
279+
t.Run(test.Name, func(t *testing.T) {
280+
runner := TestRunner(t, map[string]string{"main.tf": test.Src})
270281

271-
if instanceType != "t2.micro" {
272-
t.Fatalf(`expected value is "t2.micro", but got "%s"`, instanceType)
273-
}
282+
resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
283+
Attributes: []hclext.AttributeSchema{{Name: "instance_type"}},
284+
}, nil)
285+
if err != nil {
286+
t.Fatal(err)
287+
}
288+
289+
for _, resource := range resources.Blocks {
290+
var instanceType string
291+
if err := runner.EvaluateExpr(resource.Body.Attributes["instance_type"].Expr, &instanceType, nil); err != nil {
292+
t.Fatal(err)
293+
}
294+
295+
if instanceType != test.Want {
296+
t.Fatalf(`"%s" is expected, but got "%s"`, test.Want, instanceType)
297+
}
298+
}
299+
})
274300
}
275301
}
276302

0 commit comments

Comments
 (0)