Skip to content

Commit d4ec64f

Browse files
Simplify errors tests a bit so they don't all have to write a schema (#196)
Some of the errors tests need to have their own schema, so the schema can do something weird (or even be entirely invalid!). But most can still share a schema. In this commit I have those indeed share a schema, to avoid having to have a bunch of copies of mostly the same schema. While doing so I noticed one error whose location wasn't very useful, and fixed it. Test plan: make check
1 parent 39cd158 commit d4ec64f

10 files changed

+26
-37
lines changed

generate/convert.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ func (g *generator) convertDefinition(
294294
globalBinding, ok := g.Config.Bindings[def.Name]
295295
if ok && options.Bind != "-" {
296296
if options.TypeName != "" {
297-
return nil, errorf(pos,
297+
// The option position (in the query) is more useful here.
298+
return nil, errorf(options.pos,
298299
"typename option conflicts with global binding for %s; "+
299300
"use `bind: \"-\"` to override it", def.Name)
300301
}

generate/generate_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generate
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"os/exec"
@@ -235,14 +236,15 @@ func TestGenerateWithConfig(t *testing.T) {
235236
}
236237
}
237238

238-
// TestGenerate is a snapshot-based test of error text.
239+
// TestGenerateErrors is a snapshot-based test of error text.
239240
//
240-
// For each .go or .graphql file in testdata/errors, and corresponding
241-
// .schema.graphql file, it asserts that the given query returns an error, and
242-
// that that error's string-text matches the snapshot. The snapshotting is
243-
// useful to ensure we don't accidentally make the text less readable, drop the
244-
// line numbers, etc. We include both .go and .graphql tests, to make sure the
245-
// line numbers work in both cases.
241+
// For each .go or .graphql file in testdata/errors, it asserts that the given
242+
// query returns an error, and that that error's string-text matches the
243+
// snapshot. The snapshotting is useful to ensure we don't accidentally make
244+
// the text less readable, drop the line numbers, etc. We include both .go and
245+
// .graphql tests for some of the test cases, to make sure the line numbers
246+
// work in both cases. Tests may include a .schema.graphql file of their own,
247+
// or use the shared schema.graphql in the same directory for convenience.
246248
func TestGenerateErrors(t *testing.T) {
247249
files, err := os.ReadDir(errorsDir)
248250
if err != nil {
@@ -253,14 +255,25 @@ func TestGenerateErrors(t *testing.T) {
253255
sourceFilename := file.Name()
254256
if !strings.HasSuffix(sourceFilename, ".graphql") &&
255257
!strings.HasSuffix(sourceFilename, ".go") ||
256-
strings.HasSuffix(sourceFilename, ".schema.graphql") {
258+
strings.HasSuffix(sourceFilename, ".schema.graphql") ||
259+
sourceFilename == "schema.graphql" {
257260
continue
258261
}
259262

260263
baseFilename := strings.TrimSuffix(sourceFilename, filepath.Ext(sourceFilename))
261-
schemaFilename := baseFilename + ".schema.graphql"
262264
testFilename := strings.ReplaceAll(sourceFilename, ".", "/")
263265

266+
// Schema is either <base>.schema.graphql, or <dir>/schema.graphql if
267+
// that doesn't exist.
268+
schemaFilename := baseFilename + ".schema.graphql"
269+
if _, err := os.Stat(filepath.Join(errorsDir, schemaFilename)); err != nil {
270+
if errors.Is(err, os.ErrNotExist) {
271+
schemaFilename = "schema.graphql"
272+
} else {
273+
t.Fatal(err)
274+
}
275+
}
276+
264277
t.Run(testFilename, func(t *testing.T) {
265278
_, err := Generate(&Config{
266279
Schema: []string{filepath.Join(errorsDir, schemaFilename)},

generate/testdata/errors/ConflictingDirectiveArguments.schema.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

generate/testdata/errors/ConflictingDirectives.schema.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

generate/testdata/errors/ConflictingTypeNameAndForFieldBind.schema.graphql

Lines changed: 0 additions & 8 deletions
This file was deleted.

generate/testdata/errors/ConflictingTypeNameAndLocalBind.schema.graphql

Lines changed: 0 additions & 8 deletions
This file was deleted.

generate/testdata/errors/InvalidQuery.schema.graphql

Lines changed: 0 additions & 3 deletions
This file was deleted.

generate/testdata/errors/NoQuery.schema.graphql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type Query {
2+
f: String
23
user: User
34
}
45

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
testdata/errors/ConflictingTypeNameAndGlobalBind.schema.graphql:8: typename option conflicts with global binding for ValidScalar; use `bind: "-"` to override it
1+
testdata/errors/ConflictingTypeNameAndGlobalBind.graphql:4: typename option conflicts with global binding for ValidScalar; use `bind: "-"` to override it

0 commit comments

Comments
 (0)