Switch to cupaloy for snapshots

Slightly uglier filenames, but less code and free diffing!  Approved in
ADR-466 for Khan use.  Fixes #23.
This commit is contained in:
Ben Kraft
2021-06-03 12:08:09 -07:00
parent 3c11f07e62
commit 1f442da041
63 changed files with 78 additions and 85 deletions
+8 -52
View File
@@ -1,13 +1,13 @@
package generate
import (
"errors"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Khan/genqlient/internal/testutil"
)
const (
@@ -15,52 +15,6 @@ const (
errorsDir = "testdata/errors"
)
func gofmt(filename, src string) (string, error) {
src = strings.TrimSpace(src)
formatted, err := format.Source([]byte(src))
if err != nil {
return src, errorf(nil, "go parse error in %v: %v", filename, err)
}
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.
//
// This file just has the test runner; the actual data is all in
@@ -109,7 +63,9 @@ func TestGenerate(t *testing.T) {
}
for filename, content := range generated {
checkSnapshot(t, filepath.Join(dataDir, filename), string(content))
t.Run(filename, func(t *testing.T) {
testutil.Cupaloy.SnapshotT(t, string(content))
})
// TODO(benkraft): Also check that the code at least builds!
}
})
@@ -133,8 +89,8 @@ func TestGenerateErrors(t *testing.T) {
continue
}
schemaFilename := strings.TrimSuffix(sourceFilename, filepath.Ext(sourceFilename)) + ".schema.graphql"
errorsFilename := sourceFilename + ".error"
baseFilename := strings.TrimSuffix(sourceFilename, filepath.Ext(sourceFilename))
schemaFilename := baseFilename + ".schema.graphql"
t.Run(sourceFilename, func(t *testing.T) {
_, err := Generate(&Config{
@@ -151,7 +107,7 @@ func TestGenerateErrors(t *testing.T) {
t.Fatal("expected an error")
}
checkSnapshot(t, filepath.Join(errorsDir, errorsFilename), err.Error())
testutil.Cupaloy.SnapshotT(t, err.Error())
})
}
}
+1
View File
@@ -40,6 +40,7 @@ func getTestQueries(t *testing.T, ext string) *ast.QueryDocument {
// produces equivalent results. We do not test the results it produces (that's
// covered by TestGenerate), just that they are equivalent in different
// languages (since TestGenerate only uses .graphql as input).
// TODO: redo this as more standard snapshot tests?
func TestParse(t *testing.T) {
extensions := []string{"go"}
-1
View File
@@ -1 +0,0 @@
no queries found, looked in: testdata/errors/NoQuery.go
-1
View File
@@ -1 +0,0 @@
no queries found, looked in: testdata/errors/NoQuery.graphql
@@ -37,3 +37,4 @@ query convertTimezone ($dt: DateTime!, $tz: String) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/DateTime.graphql"
}
]
}
}
@@ -30,3 +30,4 @@ query EmptyInterface {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/EmptyInterface.graphql"
}
]
}
}
@@ -62,3 +62,4 @@ query InputEnumQuery ($role: Role!) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InputEnum.graphql"
}
]
}
}
@@ -79,3 +79,4 @@ query InputObjectQuery ($query: UserQueryInput) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InputObject.graphql"
}
]
}
}
@@ -401,3 +401,4 @@ query InterfaceNesting {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InterfaceNesting.graphql"
}
]
}
}
@@ -131,3 +131,4 @@ query InterfaceNoFragmentsQuery {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InterfaceNoFragments.graphql"
}
]
}
}
@@ -51,3 +51,4 @@ query ListInputQuery ($names: [String]) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/ListInput.graphql"
}
]
}
}
@@ -28,3 +28,4 @@ query ListOfListsOfLists {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/ListOfListsOfLists.graphql"
}
]
}
}
@@ -123,3 +123,4 @@ query OmitEmptyQuery ($query: UserQueryInput, $queries: [UserQueryInput], $dt: D
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/Omitempty.graphql"
}
]
}
}
@@ -114,3 +114,4 @@ query PointersQuery ($query: UserQueryInput, $dt: DateTime, $tz: String) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/Pointers.graphql"
}
]
}
}
@@ -114,3 +114,4 @@ query PointersQuery ($query: UserQueryInput, $dt: DateTime, $tz: String) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/PointersInline.graphql"
}
]
}
}
@@ -51,3 +51,4 @@ query QueryWithAlias {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithAlias.graphql"
}
]
}
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithDoubleAlias.graphql"
}
]
}
}
@@ -72,3 +72,4 @@ query QueryWithEnums {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithEnums.graphql"
}
]
}
}
@@ -48,3 +48,4 @@ query QueryWithSlices {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithSlices.graphql"
}
]
}
}
@@ -51,3 +51,4 @@ query QueryWithStructs {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithStructs.graphql"
}
]
}
}
@@ -51,3 +51,4 @@ query SimpleInputQuery ($name: String!) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleInput.graphql"
}
]
}
}
@@ -53,3 +53,4 @@ mutation SimpleMutation ($name: String!) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleMutation.graphql"
}
]
}
}
@@ -46,3 +46,4 @@ query SimpleQuery {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleQuery.graphql"
}
]
}
}
@@ -48,3 +48,4 @@ query TypeNameQuery {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/TypeName.graphql"
}
]
}
}
@@ -97,3 +97,4 @@ query UnionNoFragmentsQuery {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/UnionNoFragments.graphql"
}
]
}
}
@@ -72,3 +72,4 @@ query UsesEnumTwiceQuery {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/UsesEnumTwice.graphql"
}
]
}
}
@@ -79,3 +79,4 @@ query unexported ($query: UserQueryInput) {
)
return &retval, err
}
@@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/unexported.graphql"
}
]
}
}
@@ -1 +1 @@
testdata/errors/InvalidQuery.go:4: query-spec does not match schema: Cannot query field "g" on type "Query". Did you mean "f"?
testdata/errors/InvalidQuery.go:4: query-spec does not match schema: Cannot query field "g" on type "Query". Did you mean "f"?
@@ -1 +1 @@
testdata/errors/InvalidQuery.graphql:1: query-spec does not match schema: Cannot query field "g" on type "Query". Did you mean "f"?
testdata/errors/InvalidQuery.graphql:1: query-spec does not match schema: Cannot query field "g" on type "Query". Did you mean "f"?
@@ -1 +1 @@
invalid type-name "bogus" (unknown type-name "bogus"); expected a builtin, path/to/package.Name, interface{}, or a slice, map, or pointer of those
invalid type-name "bogus" (unknown type-name "bogus"); expected a builtin, path/to/package.Name, interface{}, or a slice, map, or pointer of those
@@ -1 +1 @@
invalid type-name "bogus" (unknown type-name "bogus"); expected a builtin, path/to/package.Name, interface{}, or a slice, map, or pointer of those
invalid type-name "bogus" (unknown type-name "bogus"); expected a builtin, path/to/package.Name, interface{}, or a slice, map, or pointer of those
@@ -1 +1 @@
testdata/errors/InvalidSchema.schema.graphql:4: invalid schema file testdata/errors/InvalidSchema.schema.graphql: Expected :, found }
testdata/errors/InvalidSchema.schema.graphql:4: invalid schema file testdata/errors/InvalidSchema.schema.graphql: Expected :, found }
@@ -1 +1 @@
testdata/errors/InvalidSchema.schema.graphql:4: invalid schema file testdata/errors/InvalidSchema.schema.graphql: Expected :, found }
testdata/errors/InvalidSchema.schema.graphql:4: invalid schema file testdata/errors/InvalidSchema.schema.graphql: Expected :, found }
@@ -0,0 +1 @@
no queries found, looked in: testdata/errors/NoQuery.go
@@ -0,0 +1 @@
no queries found, looked in: testdata/errors/NoQuery.graphql
@@ -1 +1 @@
testdata/errors/UnknownScalar.schema.graphql:3: unknown scalar UnknownScalar: please add it to genqlient.yaml
testdata/errors/UnknownScalar.schema.graphql:3: unknown scalar UnknownScalar: please add it to genqlient.yaml
@@ -1 +1 @@
testdata/errors/UnknownScalar.schema.graphql:3: unknown scalar UnknownScalar: please add it to genqlient.yaml
testdata/errors/UnknownScalar.schema.graphql:3: unknown scalar UnknownScalar: please add it to genqlient.yaml
+1
View File
@@ -3,6 +3,7 @@ module github.com/Khan/genqlient
go 1.13
require (
github.com/bradleyjkemp/cupaloy/v2 v2.6.0
github.com/vektah/gqlparser/v2 v2.1.0
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6
gopkg.in/yaml.v2 v2.2.4
+7
View File
@@ -2,6 +2,10 @@ github.com/agnivade/levenshtein v1.0.1 h1:3oJU7J3FGFmyhn8KHjmVaZCN5hxTr7GxgRue+s
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/bradleyjkemp/cupaloy v1.3.0 h1:UJ0YJuhkMXEQcaoQNSCmK8og6GEVW/eDhAZuizvcpOY=
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0 h1:knToPYa2xtfg42U3I6punFEjaGFKWQRXJwj0JTv4mTs=
github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -16,8 +20,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vektah/gqlparser/v2 v2.1.0 h1:uiKJ+T5HMGGQM2kRKQ8Pxw8+Zq9qhhZhz/lieYvCMns=
github.com/vektah/gqlparser/v2 v2.1.0/go.mod h1:SyUiHgLATUR8BiYURfTirrTcGpcE+4XkV2se04Px1Ms=
golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6 h1:iZgcI2DDp6zW5v9Z/5+f0NuqoxNdmzg4hivjk2WLXpY=
@@ -29,3 +35,4 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+5
View File
@@ -0,0 +1,5 @@
package testutil
import "github.com/bradleyjkemp/cupaloy/v2"
var Cupaloy = cupaloy.New(cupaloy.SnapshotSubdirectory("testdata/snapshots"))