Skip to content

Commit 77ef450

Browse files
authored
Add support for JSON syntax in TestRunner (#177)
1 parent e7ddb76 commit 77ef450

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

helper/runner_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,39 @@ terraform {
247247
}
248248
}
249249

250+
func Test_GetModuleContent_json(t *testing.T) {
251+
files := map[string]string{
252+
"main.tf.json": `{"variable": {"foo": {"type": "string"}}}`,
253+
}
254+
255+
runner := TestRunner(t, files)
256+
257+
schema := &hclext.BodySchema{
258+
Blocks: []hclext.BlockSchema{
259+
{
260+
Type: "variable",
261+
Body: &hclext.BodySchema{
262+
Blocks: []hclext.BlockSchema{
263+
{
264+
Type: "type",
265+
LabelNames: []string{"name"},
266+
Body: &hclext.BodySchema{},
267+
},
268+
},
269+
},
270+
},
271+
},
272+
}
273+
got, err := runner.GetModuleContent(schema, nil)
274+
if err != nil {
275+
t.Error(err)
276+
} else {
277+
if len(got.Blocks) != 1 {
278+
t.Errorf("got %d blocks, but 1 block is expected", len(got.Blocks))
279+
}
280+
}
281+
}
282+
250283
func Test_EvaluateExpr(t *testing.T) {
251284
tests := []struct {
252285
Name string

helper/testing.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package helper
33
import (
44
"fmt"
55
"reflect"
6+
"strings"
67
"testing"
78

89
"github.com/google/go-cmp/cmp"
@@ -20,7 +21,13 @@ func TestRunner(t *testing.T, files map[string]string) *Runner {
2021
parser := hclparse.NewParser()
2122

2223
for name, src := range files {
23-
file, diags := parser.ParseHCL([]byte(src), name)
24+
var file *hcl.File
25+
var diags hcl.Diagnostics
26+
if strings.HasSuffix(name, ".json") {
27+
file, diags = parser.ParseJSON([]byte(src), name)
28+
} else {
29+
file, diags = parser.ParseHCL([]byte(src), name)
30+
}
2431
if diags.HasErrors() {
2532
t.Fatal(diags)
2633
}

0 commit comments

Comments
 (0)