Skip to content

Commit

Permalink
Add support for JSON syntax in TestRunner (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Aug 6, 2022
1 parent e7ddb76 commit 77ef450
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions helper/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,39 @@ terraform {
}
}

func Test_GetModuleContent_json(t *testing.T) {
files := map[string]string{
"main.tf.json": `{"variable": {"foo": {"type": "string"}}}`,
}

runner := TestRunner(t, files)

schema := &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "variable",
Body: &hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{
Type: "type",
LabelNames: []string{"name"},
Body: &hclext.BodySchema{},
},
},
},
},
},
}
got, err := runner.GetModuleContent(schema, nil)
if err != nil {
t.Error(err)
} else {
if len(got.Blocks) != 1 {
t.Errorf("got %d blocks, but 1 block is expected", len(got.Blocks))
}
}
}

func Test_EvaluateExpr(t *testing.T) {
tests := []struct {
Name string
Expand Down
9 changes: 8 additions & 1 deletion helper/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helper
import (
"fmt"
"reflect"
"strings"
"testing"

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

for name, src := range files {
file, diags := parser.ParseHCL([]byte(src), name)
var file *hcl.File
var diags hcl.Diagnostics
if strings.HasSuffix(name, ".json") {
file, diags = parser.ParseJSON([]byte(src), name)
} else {
file, diags = parser.ParseHCL([]byte(src), name)
}
if diags.HasErrors() {
t.Fatal(diags)
}
Expand Down

0 comments on commit 77ef450

Please sign in to comment.