Skip to content

Commit

Permalink
Switch to cupaloy for snapshots
Browse files Browse the repository at this point in the history
Slightly uglier filenames, but less code and free diffing!  Approved in
ADR-466 for Khan use.  Fixes Khan#23.
  • Loading branch information
benjaminjkraft committed Jun 3, 2021
1 parent 3c11f07 commit 1f442da
Show file tree
Hide file tree
Showing 63 changed files with 78 additions and 85 deletions.
60 changes: 8 additions & 52 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,20 @@
package generate

import (
"errors"
"go/format"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/Khan/genqlient/internal/testutil"
)

const (
dataDir = "testdata/queries"
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
Expand Down Expand Up @@ -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!
}
})
Expand All @@ -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{
Expand All @@ -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 change: 1 addition & 0 deletions generate/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down
1 change: 0 additions & 1 deletion generate/testdata/errors/NoQuery.go.error

This file was deleted.

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

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/DateTime.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/EmptyInterface.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InputEnum.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InputObject.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InterfaceNesting.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/InterfaceNoFragments.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/ListInput.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/ListOfListsOfLists.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/Omitempty.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/Pointers.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/PointersInline.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithAlias.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithDoubleAlias.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithEnums.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithSlices.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/QueryWithStructs.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleInput.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleMutation.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/SimpleQuery.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/TypeName.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/UnionNoFragments.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/UsesEnumTwice.graphql"
}
]
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"sourceLocation": "testdata/queries/unexported.graphql"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -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"?
Original file line number Diff line number Diff line change
@@ -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"?
Loading

0 comments on commit 1f442da

Please sign in to comment.