-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathparse_test.go
86 lines (70 loc) · 1.82 KB
/
parse_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfjson
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
const testFixtureDir = "testdata"
const testGoldenPlanFileName = "plan.json"
const testGoldenStateFileName = "state.json"
const testGoldenSchemasFileName = "schemas.json"
func testParse(t *testing.T, filename string, typ reflect.Type) {
entries, err := ioutil.ReadDir(testFixtureDir)
if err != nil {
t.Fatalf("err: %s", err)
}
for _, e := range entries {
if !e.IsDir() {
continue
}
t.Run(e.Name(), func(t *testing.T) {
expected, err := ioutil.ReadFile(filepath.Join(testFixtureDir, e.Name(), filename))
if err != nil {
if os.IsNotExist(err) {
t.Skip(err.Error())
}
t.Fatal(err)
}
parsed := reflect.New(typ).Interface()
dec := json.NewDecoder(bytes.NewBuffer(expected))
dec.DisallowUnknownFields()
if err = dec.Decode(parsed); err != nil {
t.Fatal(err)
}
actual, err := json.Marshal(parsed)
if err != nil {
t.Fatal(err)
}
// Add a newline at the end
actual = append(actual, byte('\n'))
// TODO: Compare the actual struct instead of byte slice
// because JSON does not guarantee consistent key ordering
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("unexpected: %s", diff)
}
})
}
}
func TestParsePlan(t *testing.T) {
testParse(t, testGoldenPlanFileName, reflect.TypeOf(Plan{}))
}
func TestParseSchemas(t *testing.T) {
testParse(t, testGoldenSchemasFileName, reflect.TypeOf(ProviderSchemas{}))
}
func TestParseState(t *testing.T) {
testParse(t, testGoldenStateFileName, reflect.TypeOf(State{}))
}
func lineAt(text []byte, offs int) []byte {
i := offs
for i < len(text) && text[i] != '\n' {
i++
}
return text[offs:i]
}