Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify errors tests a bit so they don't all have to write a schema #196

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ func (g *generator) convertDefinition(
globalBinding, ok := g.Config.Bindings[def.Name]
if ok && options.Bind != "-" {
if options.TypeName != "" {
return nil, errorf(pos,
// The option position (in the query) is more useful here.
return nil, errorf(options.pos,
"typename option conflicts with global binding for %s; "+
"use `bind: \"-\"` to override it", def.Name)
}
Expand Down
31 changes: 22 additions & 9 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generate

import (
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -235,14 +236,15 @@ func TestGenerateWithConfig(t *testing.T) {
}
}

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

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

// Schema is either <base>.schema.graphql, or <dir>/schema.graphql if
// that doesn't exist.
schemaFilename := baseFilename + ".schema.graphql"
if _, err := os.Stat(filepath.Join(errorsDir, schemaFilename)); err != nil {
if errors.Is(err, os.ErrNotExist) {
schemaFilename = "schema.graphql"
} else {
t.Fatal(err)
}
}

t.Run(testFilename, func(t *testing.T) {
_, err := Generate(&Config{
Schema: []string{filepath.Join(errorsDir, schemaFilename)},
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions generate/testdata/errors/InvalidQuery.schema.graphql

This file was deleted.

1 change: 0 additions & 1 deletion generate/testdata/errors/NoQuery.schema.graphql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type Query {
f: String
user: User
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
testdata/errors/ConflictingTypeNameAndGlobalBind.schema.graphql:8: typename option conflicts with global binding for ValidScalar; use `bind: "-"` to override it
testdata/errors/ConflictingTypeNameAndGlobalBind.graphql:4: typename option conflicts with global binding for ValidScalar; use `bind: "-"` to override it