-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdecoder_test.go
72 lines (59 loc) · 1.57 KB
/
decoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package earlydecoder
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
tftest "github.com/hashicorp/terraform-schema/test"
"github.com/zclconf/go-cty-debug/ctydebug"
)
type testCase struct {
name string
cfg string
expectedMeta *tftest.Meta
expectedError hcl.Diagnostics
}
var customComparer = []cmp.Option{
cmp.Comparer(compareVersionConstraint),
ctydebug.CmpOptions,
}
func TestLoadTest(t *testing.T) {
path := t.TempDir()
testCases := []testCase{
{
"empty config",
``,
&tftest.Meta{
Path: path,
},
nil,
},
// TODO: add more test once we do more early decoding
}
runTestCases(testCases, t, path)
}
func runTestCases(testCases []testCase, t *testing.T, path string) {
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
file, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tftest.hcl", hcl.InitialPos)
if len(diags) > 0 {
t.Fatal(diags)
}
filename := "test.tftest.hcl"
meta, diags := LoadTest(path, filename, file)
if diff := cmp.Diff(tc.expectedError, diags, customComparer...); diff != "" {
t.Fatalf("expected errors doesn't match: %s", diff)
}
if diff := cmp.Diff(tc.expectedMeta, meta, customComparer...); diff != "" {
t.Fatalf("test meta doesn't match: %s", diff)
}
})
}
}
func compareVersionConstraint(x, y *version.Constraint) bool {
return x.Equals(y)
}