-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
config_test.go
250 lines (206 loc) · 8.38 KB
/
config_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package config
import (
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/99designs/gqlgen/internal/code"
)
func TestLoadConfig(t *testing.T) {
t.Run("config does not exist", func(t *testing.T) {
_, err := LoadConfig("doesnotexist.yml")
require.Error(t, err)
})
}
func TestReadConfig(t *testing.T) {
t.Run("empty config", func(t *testing.T) {
_, err := ReadConfig(strings.NewReader(""))
require.EqualError(t, err, "unable to parse config: EOF")
})
t.Run("malformed config", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/malformedconfig.yml")
_, err := ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `asdf` into config.Config")
})
t.Run("unknown keys", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/unknownkeys.yml")
_, err := ReadConfig(cfgFile)
require.EqualError(t, err, "unable to parse config: yaml: unmarshal errors:\n line 2: field unknown not found in type config.Config")
})
t.Run("globbed filenames", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/glob.yml")
c, err := ReadConfig(cfgFile)
require.NoError(t, err)
if runtime.GOOS == "windows" {
require.Equal(t, `testdata\cfg\glob\bar\bar with spaces.graphql`, c.SchemaFilename[0])
require.Equal(t, `testdata\cfg\glob\foo\foo.graphql`, c.SchemaFilename[1])
} else {
require.Equal(t, "testdata/cfg/glob/bar/bar with spaces.graphql", c.SchemaFilename[0])
require.Equal(t, "testdata/cfg/glob/foo/foo.graphql", c.SchemaFilename[1])
}
})
t.Run("unwalkable path", func(t *testing.T) {
cfgFile, _ := os.Open("testdata/cfg/unwalkable.yml")
_, err := ReadConfig(cfgFile)
if runtime.GOOS == "windows" {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: CreateFile not_walkable/: The system cannot find the file specified.")
} else {
require.EqualError(t, err, "failed to walk schema at root not_walkable/: lstat not_walkable/: no such file or directory")
}
})
}
func TestLoadConfigFromDefaultLocation(t *testing.T) {
testDir, err := os.Getwd()
require.NoError(t, err)
var cfg *Config
t.Run("will find closest match", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "subdir"))
require.NoError(t, err)
cfg, err = LoadConfigFromDefaultLocations()
require.NoError(t, err)
require.Equal(t, StringList{"inner"}, cfg.SchemaFilename)
})
t.Run("will find config in parent dirs", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "cfg", "otherdir"))
require.NoError(t, err)
cfg, err = LoadConfigFromDefaultLocations()
require.NoError(t, err)
require.Equal(t, StringList{"outer"}, cfg.SchemaFilename)
})
t.Run("will return error if config doesn't exist", func(t *testing.T) {
err = os.Chdir(testDir)
require.NoError(t, err)
cfg, err = LoadConfigFromDefaultLocations()
require.True(t, errors.Is(err, fs.ErrNotExist))
})
}
func TestLoadDefaultConfig(t *testing.T) {
testDir, err := os.Getwd()
require.NoError(t, err)
var cfg *Config
t.Run("will find the schema", func(t *testing.T) {
err = os.Chdir(filepath.Join(testDir, "testdata", "defaultconfig"))
require.NoError(t, err)
cfg, err = LoadDefaultConfig()
require.NoError(t, err)
require.NotEmpty(t, cfg.Sources)
})
t.Run("will return error if schema doesn't exist", func(t *testing.T) {
err = os.Chdir(testDir)
require.NoError(t, err)
cfg, err = LoadDefaultConfig()
require.True(t, errors.Is(err, fs.ErrNotExist))
})
}
func TestReferencedPackages(t *testing.T) {
t.Run("valid", func(t *testing.T) {
tm := TypeMap{
"Foo": {Model: StringList{"github.com/test.Foo"}},
"Bar": {Model: StringList{"github.com/test.Bar"}},
"Baz": {Model: StringList{"github.com/otherpkg.Baz"}},
"Map": {Model: StringList{"map[string]interface{}"}},
"SkipResolver": {
Fields: map[string]TypeMapField{
"field": {Resolver: false},
},
},
}
pkgs := tm.ReferencedPackages()
assert.Equal(t, []string{"github.com/test", "github.com/otherpkg"}, pkgs)
})
}
func TestConfigCheck(t *testing.T) {
for _, execLayout := range []ExecLayout{ExecLayoutSingleFile, ExecLayoutFollowSchema} {
t.Run(string(execLayout), func(t *testing.T) {
t.Run("invalid config format due to conflicting package names", func(t *testing.T) {
config := Config{
Exec: ExecConfig{Layout: execLayout, Filename: "generated/exec.go", DirName: "generated", Package: "graphql"},
Model: PackageConfig{Filename: "generated/models.go"},
}
require.EqualError(t, config.check(), "exec and model define the same import path (github.com/99designs/gqlgen/codegen/config/generated) with different package names (graphql vs generated)")
})
t.Run("federation must be in exec package", func(t *testing.T) {
config := Config{
Exec: ExecConfig{Layout: execLayout, Filename: "generated/exec.go", DirName: "generated"},
Federation: PackageConfig{Filename: "anotherpkg/federation.go"},
}
require.EqualError(t, config.check(), "federation and exec must be in the same package")
})
t.Run("federation must have same package name as exec", func(t *testing.T) {
config := Config{
Exec: ExecConfig{Layout: execLayout, Filename: "generated/exec.go", DirName: "generated"},
Federation: PackageConfig{Filename: "generated/federation.go", Package: "federation"},
}
require.EqualError(t, config.check(), "exec and federation define the same import path (github.com/99designs/gqlgen/codegen/config/generated) with different package names (generated vs federation)")
})
t.Run("deprecated federated flag raises an error", func(t *testing.T) {
config := Config{
Exec: ExecConfig{Layout: execLayout, Filename: "generated/exec.go", DirName: "generated"},
Federated: true,
}
require.EqualError(t, config.check(), "federated has been removed, instead use\nfederation:\n filename: path/to/federated.go")
})
})
}
}
func TestAutobinding(t *testing.T) {
t.Run("valid paths", func(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat",
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model",
},
Packages: code.NewPackages(),
}
cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
`})
require.NoError(t, cfg.autobind())
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model.Banned", cfg.Models["Banned"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message", cfg.Models["Message"].Model[0])
})
t.Run("normalized type names", func(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat",
"github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model",
},
Packages: code.NewPackages(),
}
cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
enum ProductSKU { ProductSkuTrial }
type ChatAPI { id: ID }
`})
require.NoError(t, cfg.autobind())
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/scalars/model.Banned", cfg.Models["Banned"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.Message", cfg.Models["Message"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.ProductSku", cfg.Models["ProductSKU"].Model[0])
require.Equal(t, "github.com/99designs/gqlgen/codegen/config/testdata/autobinding/chat.ChatAPI", cfg.Models["ChatAPI"].Model[0])
})
t.Run("with file path", func(t *testing.T) {
cfg := Config{
Models: TypeMap{},
AutoBind: []string{
"../chat",
},
Packages: code.NewPackages(),
}
cfg.Schema = gqlparser.MustLoadSchema(&ast.Source{Name: "TestAutobinding.schema", Input: `
scalar Banned
type Message { id: ID }
`})
require.EqualError(t, cfg.autobind(), "unable to load ../chat - make sure you're using an import path to a package that exists")
})
}