1 package formatter_test
2
3 import (
4 "bytes"
5 "flag"
6 "io/ioutil"
7 "os"
8 "path"
9 "testing"
10 "unicode/utf8"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/vektah/gqlparser"
14 "github.com/vektah/gqlparser/ast"
15 "github.com/vektah/gqlparser/formatter"
16 "github.com/vektah/gqlparser/parser"
17 )
18
19 var update = flag.Bool("u", false, "update golden files")
20
21 func TestFormatter_FormatSchema(t *testing.T) {
22 const testSourceDir = "./testdata/source/schema"
23 const testBaselineDir = "./testdata/baseline/FormatSchema"
24
25 executeGoldenTesting(t, &goldenConfig{
26 SourceDir: testSourceDir,
27 BaselineFileName: func(cfg *goldenConfig, f os.FileInfo) string {
28 return path.Join(testBaselineDir, f.Name())
29 },
30 Run: func(t *testing.T, cfg *goldenConfig, f os.FileInfo) []byte {
31
32 schema, gqlErr := gqlparser.LoadSchema(&ast.Source{
33 Name: f.Name(),
34 Input: mustReadFile(path.Join(testSourceDir, f.Name())),
35 })
36 if gqlErr != nil {
37 t.Fatal(gqlErr)
38 }
39
40
41 var buf bytes.Buffer
42 formatter.NewFormatter(&buf).FormatSchema(schema)
43
44
45 _, gqlErr = gqlparser.LoadSchema(&ast.Source{
46 Name: f.Name(),
47 Input: buf.String(),
48 })
49 if gqlErr != nil {
50 t.Log(buf.String())
51 t.Fatal(gqlErr)
52 }
53
54 return buf.Bytes()
55 },
56 })
57 }
58
59 func TestFormatter_FormatSchemaDocument(t *testing.T) {
60 const testSourceDir = "./testdata/source/schema"
61 const testBaselineDir = "./testdata/baseline/FormatSchemaDocument"
62
63 executeGoldenTesting(t, &goldenConfig{
64 SourceDir: testSourceDir,
65 BaselineFileName: func(cfg *goldenConfig, f os.FileInfo) string {
66 return path.Join(testBaselineDir, f.Name())
67 },
68 Run: func(t *testing.T, cfg *goldenConfig, f os.FileInfo) []byte {
69
70 doc, gqlErr := parser.ParseSchema(&ast.Source{
71 Name: f.Name(),
72 Input: mustReadFile(path.Join(testSourceDir, f.Name())),
73 })
74 if gqlErr != nil {
75 t.Fatal(gqlErr)
76 }
77
78
79 var buf bytes.Buffer
80 formatter.NewFormatter(&buf).FormatSchemaDocument(doc)
81
82
83 _, gqlErr = parser.ParseSchema(&ast.Source{
84 Name: f.Name(),
85 Input: buf.String(),
86 })
87 if gqlErr != nil {
88 t.Log(buf.String())
89 t.Fatal(gqlErr)
90 }
91
92 return buf.Bytes()
93 },
94 })
95 }
96
97 func TestFormatter_FormatQueryDocument(t *testing.T) {
98 const testSourceDir = "./testdata/source/query"
99 const testBaselineDir = "./testdata/baseline/FormatQueryDocument"
100
101 executeGoldenTesting(t, &goldenConfig{
102 SourceDir: testSourceDir,
103 BaselineFileName: func(cfg *goldenConfig, f os.FileInfo) string {
104 return path.Join(testBaselineDir, f.Name())
105 },
106 Run: func(t *testing.T, cfg *goldenConfig, f os.FileInfo) []byte {
107
108 doc, gqlErr := parser.ParseQuery(&ast.Source{
109 Name: f.Name(),
110 Input: mustReadFile(path.Join(testSourceDir, f.Name())),
111 })
112 if gqlErr != nil {
113 t.Fatal(gqlErr)
114 }
115
116
117 var buf bytes.Buffer
118 formatter.NewFormatter(&buf).FormatQueryDocument(doc)
119
120
121 _, gqlErr = parser.ParseQuery(&ast.Source{
122 Name: f.Name(),
123 Input: buf.String(),
124 })
125 if gqlErr != nil {
126 t.Log(buf.String())
127 t.Fatal(gqlErr)
128 }
129
130 return buf.Bytes()
131 },
132 })
133 }
134
135 type goldenConfig struct {
136 SourceDir string
137 IsTarget func(f os.FileInfo) bool
138 BaselineFileName func(cfg *goldenConfig, f os.FileInfo) string
139 Run func(t *testing.T, cfg *goldenConfig, f os.FileInfo) []byte
140 }
141
142 func executeGoldenTesting(t *testing.T, cfg *goldenConfig) {
143 t.Helper()
144
145 if cfg.IsTarget == nil {
146 cfg.IsTarget = func(f os.FileInfo) bool {
147 return !f.IsDir()
148 }
149 }
150 if cfg.BaselineFileName == nil {
151 t.Fatal("BaselineFileName function is required")
152 }
153 if cfg.Run == nil {
154 t.Fatal("Run function is required")
155 }
156
157 fs, err := ioutil.ReadDir(cfg.SourceDir)
158 if err != nil {
159 t.Fatal(fs)
160 }
161
162 for _, f := range fs {
163 if f.IsDir() {
164 continue
165 }
166 f := f
167
168 t.Run(f.Name(), func(t *testing.T) {
169 result := cfg.Run(t, cfg, f)
170
171 expectedFilePath := cfg.BaselineFileName(cfg, f)
172
173 if *update {
174 err := os.Remove(expectedFilePath)
175 if err != nil && !os.IsNotExist(err) {
176 t.Fatal(err)
177 }
178 }
179
180 expected, err := ioutil.ReadFile(expectedFilePath)
181 if os.IsNotExist(err) {
182 err = os.MkdirAll(path.Dir(expectedFilePath), 0755)
183 if err != nil {
184 t.Fatal(err)
185 }
186 err = ioutil.WriteFile(expectedFilePath, result, 0444)
187 if err != nil {
188 t.Fatal(err)
189 }
190 return
191
192 } else if err != nil {
193 t.Fatal(err)
194 }
195
196 if bytes.Equal(expected, result) {
197 return
198 }
199
200 if utf8.Valid(expected) {
201 assert.Equalf(t, string(expected), string(result), "if you want to accept new result. use -u option")
202 }
203 })
204 }
205 }
206
207 func mustReadFile(name string) string {
208 src, err := ioutil.ReadFile(name)
209 if err != nil {
210 panic(err)
211 }
212
213 return string(src)
214 }
215
View as plain text