snapshots for errors
This commit is contained in:
+88
-48
@@ -11,19 +11,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dataDir = "testdata/queries"
|
const (
|
||||||
|
dataDir = "testdata/queries"
|
||||||
func readFile(t *testing.T, filename string, allowNotExist bool) string {
|
errorsDir = "testdata/errors"
|
||||||
t.Helper()
|
)
|
||||||
data, err := ioutil.ReadFile(filepath.Join(dataDir, filename))
|
|
||||||
if err != nil {
|
|
||||||
if allowNotExist && errors.Is(err, os.ErrNotExist) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return string(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func gofmt(filename, src string) (string, error) {
|
func gofmt(filename, src string) (string, error) {
|
||||||
src = strings.TrimSpace(src)
|
src = strings.TrimSpace(src)
|
||||||
@@ -34,6 +25,43 @@ func gofmt(filename, src string) (string, error) {
|
|||||||
return string(formatted), nil
|
return string(formatted), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkSnapshot(t *testing.T, filename, content string) {
|
||||||
|
t.Helper()
|
||||||
|
update := (os.Getenv("UPDATE_SNAPSHOTS") == "1")
|
||||||
|
|
||||||
|
expectedBytes, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil && !(update && errors.Is(err, os.ErrNotExist)) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
expectedContent := string(expectedBytes)
|
||||||
|
|
||||||
|
if strings.HasSuffix(filename, ".go") {
|
||||||
|
fmted, err := gofmt(filename, expectedContent)
|
||||||
|
if err != nil {
|
||||||
|
// Ignore gofmt errors if we are updating
|
||||||
|
if !update {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
expectedContent = fmted
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if content != expectedContent {
|
||||||
|
t.Errorf("mismatch in %v", filename)
|
||||||
|
if testing.Verbose() {
|
||||||
|
t.Errorf("got:\n%v\nwant:\n%v\n", content, expectedContent)
|
||||||
|
}
|
||||||
|
if update {
|
||||||
|
t.Log("Updating testdata dir to match")
|
||||||
|
err = ioutil.WriteFile(filename, []byte(content), 0o644)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unable to update testdata dir: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestGenerate is a snapshot-based test of code-generation.
|
// TestGenerate is a snapshot-based test of code-generation.
|
||||||
//
|
//
|
||||||
// This file just has the test runner; the actual data is all in
|
// This file just has the test runner; the actual data is all in
|
||||||
@@ -47,7 +75,6 @@ func gofmt(filename, src string) (string, error) {
|
|||||||
// update the snapshots. Make sure to check that the output is sensible; the
|
// update the snapshots. Make sure to check that the output is sensible; the
|
||||||
// snapshots don't even get compiled!
|
// snapshots don't even get compiled!
|
||||||
func TestGenerate(t *testing.T) {
|
func TestGenerate(t *testing.T) {
|
||||||
update := (os.Getenv("UPDATE_SNAPSHOTS") == "1")
|
|
||||||
// we can test parts of features even if they're not done yet!
|
// we can test parts of features even if they're not done yet!
|
||||||
allowBrokenFeatures = true
|
allowBrokenFeatures = true
|
||||||
|
|
||||||
@@ -57,17 +84,17 @@ func TestGenerate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
graphqlFilename := file.Name()
|
sourceFilename := file.Name()
|
||||||
if graphqlFilename == "schema.graphql" || !strings.HasSuffix(graphqlFilename, ".graphql") {
|
if sourceFilename == "schema.graphql" || !strings.HasSuffix(sourceFilename, ".graphql") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
goFilename := graphqlFilename + ".go"
|
goFilename := sourceFilename + ".go"
|
||||||
queriesFilename := graphqlFilename + ".json"
|
queriesFilename := sourceFilename + ".json"
|
||||||
|
|
||||||
t.Run(graphqlFilename, func(t *testing.T) {
|
t.Run(sourceFilename, func(t *testing.T) {
|
||||||
generated, err := Generate(&Config{
|
generated, err := Generate(&Config{
|
||||||
Schema: filepath.Join(dataDir, "schema.graphql"),
|
Schema: filepath.Join(dataDir, "schema.graphql"),
|
||||||
Operations: []string{filepath.Join(dataDir, graphqlFilename)},
|
Operations: []string{filepath.Join(dataDir, sourceFilename)},
|
||||||
Package: "test",
|
Package: "test",
|
||||||
Generated: goFilename,
|
Generated: goFilename,
|
||||||
ExportOperations: queriesFilename,
|
ExportOperations: queriesFilename,
|
||||||
@@ -82,36 +109,49 @@ func TestGenerate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for filename, content := range generated {
|
for filename, content := range generated {
|
||||||
expectedContent := readFile(t, filename, update)
|
checkSnapshot(t, filepath.Join(dataDir, filename), string(content))
|
||||||
if strings.HasSuffix(filename, ".go") {
|
|
||||||
fmted, err := gofmt(filename, expectedContent)
|
|
||||||
if err != nil {
|
|
||||||
// Ignore gofmt errors if we are updating
|
|
||||||
if !update {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
expectedContent = fmted
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(content) != expectedContent {
|
|
||||||
t.Errorf("mismatch in %v", filename)
|
|
||||||
if testing.Verbose() {
|
|
||||||
t.Errorf("got:\n%v\nwant:\n%v\n",
|
|
||||||
string(content), expectedContent)
|
|
||||||
}
|
|
||||||
if update {
|
|
||||||
t.Log("Updating testdata dir to match")
|
|
||||||
err = ioutil.WriteFile(filepath.Join(dataDir, filename), content, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unable to update testdata dir: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(benkraft): Also check that the code at least builds!
|
// TODO(benkraft): Also check that the code at least builds!
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateErrors(t *testing.T) {
|
||||||
|
// we can test parts of features even if they're not done yet!
|
||||||
|
allowBrokenFeatures = true
|
||||||
|
|
||||||
|
files, err := ioutil.ReadDir(errorsDir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
sourceFilename := file.Name()
|
||||||
|
if !strings.HasSuffix(sourceFilename, ".graphql") &&
|
||||||
|
!strings.HasSuffix(sourceFilename, ".go") ||
|
||||||
|
strings.HasSuffix(sourceFilename, ".schema.graphql") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
schemaFilename := strings.TrimSuffix(sourceFilename, filepath.Ext(sourceFilename)) + ".schema.graphql"
|
||||||
|
errorsFilename := sourceFilename + ".error"
|
||||||
|
|
||||||
|
t.Run(sourceFilename, func(t *testing.T) {
|
||||||
|
_, err := Generate(&Config{
|
||||||
|
Schema: filepath.Join(errorsDir, schemaFilename),
|
||||||
|
Operations: []string{filepath.Join(errorsDir, sourceFilename)},
|
||||||
|
Package: "test",
|
||||||
|
Generated: os.DevNull,
|
||||||
|
Scalars: map[string]string{
|
||||||
|
"ValidScalar": "string",
|
||||||
|
"InvalidScalar": "bogus",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected an error")
|
||||||
|
}
|
||||||
|
|
||||||
|
checkSnapshot(t, filepath.Join(errorsDir, errorsFilename), err.Error())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
const _ = `# @genqlient
|
||||||
|
query MyQuery { g }
|
||||||
|
`
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query-spec does not match schema: testdata/errors/InvalidQuery.go:2: Cannot query field "g" on type "Query". Did you mean "f"?
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query MyQuery { g }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query-spec does not match schema: testdata/errors/InvalidQuery.graphql:1: Cannot query field "g" on type "Query". Did you mean "f"?
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
type Query {
|
||||||
|
f: String
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
const _ = `# @genqlient
|
||||||
|
query InvalidScalar { f }
|
||||||
|
`
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
unknown name "bogus"; expected a builtin or path/to/package.Name
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query InvalidScalar { f }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
unknown name "bogus"; expected a builtin or path/to/package.Name
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
scalar InvalidScalar
|
||||||
|
|
||||||
|
type Query { f: InvalidScalar }
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
const _ = `# @genqlient
|
||||||
|
query InvalidSchema { f }
|
||||||
|
`
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
invalid schema file testdata/errors/InvalidSchema.schema.graphql: testdata/errors/InvalidSchema.schema.graphql:4: Expected :, found }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query InvalidSchema { f }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
invalid schema file testdata/errors/InvalidSchema.schema.graphql: testdata/errors/InvalidSchema.schema.graphql:4: Expected :, found }
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
type Query {
|
||||||
|
f: String!
|
||||||
|
bogus
|
||||||
|
}
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
package errors
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
no queries found in [testdata/errors/NoQuery.go]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
no queries found in [testdata/errors/NoQuery.graphql]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
type Query { f: String }
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
const _ = `# @genqlient
|
||||||
|
query UnknownScalar { f }
|
||||||
|
`
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
unknown scalar UnknownScalar: please add it to genqlient.yaml
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
query UnknownScalar { f }
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
unknown scalar UnknownScalar: please add it to genqlient.yaml
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
scalar UnknownScalar
|
||||||
|
|
||||||
|
type Query { f: UnknownScalar }
|
||||||
Reference in New Issue
Block a user