123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package main
- import (
- "bytes"
- "context"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "github.com/google/go-cmp/cmp"
- "github.com/google/go-cmp/cmp/cmpopts"
- "github.com/kyleconroy/sqlc/internal/cmd"
- )
- func TestExamples(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
- examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
- if err != nil {
- t.Fatal(err)
- }
- files, err := os.ReadDir(examples)
- if err != nil {
- t.Fatal(err)
- }
- for _, replay := range files {
- if !replay.IsDir() {
- continue
- }
- tc := replay.Name()
- t.Run(tc, func(t *testing.T) {
- t.Parallel()
- path := filepath.Join(examples, tc)
- var stderr bytes.Buffer
- output, err := cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
- if err != nil {
- t.Fatalf("sqlc generate failed: %s", stderr.String())
- }
- cmpDirectory(t, path, output)
- })
- }
- }
- func BenchmarkExamples(b *testing.B) {
- ctx := context.Background()
- examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
- if err != nil {
- b.Fatal(err)
- }
- files, err := os.ReadDir(examples)
- if err != nil {
- b.Fatal(err)
- }
- for _, replay := range files {
- if !replay.IsDir() {
- continue
- }
- tc := replay.Name()
- b.Run(tc, func(b *testing.B) {
- path := filepath.Join(examples, tc)
- for i := 0; i < b.N; i++ {
- var stderr bytes.Buffer
- cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
- }
- })
- }
- }
- func TestReplay(t *testing.T) {
- t.Parallel()
- ctx := context.Background()
- var dirs []string
- err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
- dirs = append(dirs, filepath.Dir(path))
- return filepath.SkipDir
- }
- return nil
- })
- if err != nil {
- t.Fatal(err)
- }
- for _, replay := range dirs {
- tc := replay
- t.Run(tc, func(t *testing.T) {
- t.Parallel()
- path, _ := filepath.Abs(tc)
- var stderr bytes.Buffer
- expected := expectedStderr(t, path)
- output, err := cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
- if len(expected) == 0 && err != nil {
- t.Fatalf("sqlc generate failed: %s", stderr.String())
- }
- cmpDirectory(t, path, output)
- if diff := cmp.Diff(expected, stderr.String()); diff != "" {
- t.Errorf("stderr differed (-want +got):\n%s", diff)
- }
- })
- }
- }
- func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
- expected := map[string]string{}
- var ff = func(path string, file os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if file.IsDir() {
- return nil
- }
- if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") {
- return nil
- }
- if strings.Contains(path, "/kotlin/build") {
- return nil
- }
- if strings.HasSuffix(path, "_test.go") || strings.Contains(path, "src/test/") {
- return nil
- }
- if strings.Contains(path, "/python/.venv") || strings.Contains(path, "/python/src/tests/") ||
- strings.HasSuffix(path, "__init__.py") || strings.Contains(path, "/python/src/dbtest/") {
- return nil
- }
- blob, err := os.ReadFile(path)
- if err != nil {
- return err
- }
- expected[path] = string(blob)
- return nil
- }
- if err := filepath.Walk(dir, ff); err != nil {
- t.Fatal(err)
- }
- if !cmp.Equal(expected, actual, cmpopts.EquateEmpty()) {
- t.Errorf("%s contents differ", dir)
- for name, contents := range expected {
- name := name
- tn := strings.Replace(name, dir+"/", "", -1)
- t.Run(tn, func(t *testing.T) {
- if actual[name] == "" {
- t.Errorf("%s is empty", name)
- return
- }
- if diff := cmp.Diff(contents, actual[name]); diff != "" {
- t.Errorf("%s differed (-want +got):\n%s", name, diff)
- }
- })
- }
- }
- }
- func expectedStderr(t *testing.T, dir string) string {
- t.Helper()
- path := filepath.Join(dir, "stderr.txt")
- if _, err := os.Stat(path); !os.IsNotExist(err) {
- blob, err := os.ReadFile(path)
- if err != nil {
- t.Fatal(err)
- }
- return string(blob)
- }
- return ""
- }
- func BenchmarkReplay(b *testing.B) {
- ctx := context.Background()
- var dirs []string
- err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
- dirs = append(dirs, filepath.Dir(path))
- return filepath.SkipDir
- }
- return nil
- })
- if err != nil {
- b.Fatal(err)
- }
- for _, replay := range dirs {
- tc := replay
- b.Run(tc, func(b *testing.B) {
- path, _ := filepath.Abs(tc)
- for i := 0; i < b.N; i++ {
- var stderr bytes.Buffer
- cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
- }
- })
- }
- }
|