1
0

endtoend_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/google/go-cmp/cmp"
  10. "github.com/google/go-cmp/cmp/cmpopts"
  11. "github.com/kyleconroy/sqlc/internal/cmd"
  12. )
  13. func TestExamples(t *testing.T) {
  14. t.Parallel()
  15. ctx := context.Background()
  16. examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. files, err := os.ReadDir(examples)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. for _, replay := range files {
  25. if !replay.IsDir() {
  26. continue
  27. }
  28. tc := replay.Name()
  29. t.Run(tc, func(t *testing.T) {
  30. t.Parallel()
  31. path := filepath.Join(examples, tc)
  32. var stderr bytes.Buffer
  33. output, err := cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
  34. if err != nil {
  35. t.Fatalf("sqlc generate failed: %s", stderr.String())
  36. }
  37. cmpDirectory(t, path, output)
  38. })
  39. }
  40. }
  41. func BenchmarkExamples(b *testing.B) {
  42. ctx := context.Background()
  43. examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
  44. if err != nil {
  45. b.Fatal(err)
  46. }
  47. files, err := os.ReadDir(examples)
  48. if err != nil {
  49. b.Fatal(err)
  50. }
  51. for _, replay := range files {
  52. if !replay.IsDir() {
  53. continue
  54. }
  55. tc := replay.Name()
  56. b.Run(tc, func(b *testing.B) {
  57. path := filepath.Join(examples, tc)
  58. for i := 0; i < b.N; i++ {
  59. var stderr bytes.Buffer
  60. cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
  61. }
  62. })
  63. }
  64. }
  65. func TestReplay(t *testing.T) {
  66. t.Parallel()
  67. ctx := context.Background()
  68. var dirs []string
  69. err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
  70. if err != nil {
  71. return err
  72. }
  73. if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
  74. dirs = append(dirs, filepath.Dir(path))
  75. return filepath.SkipDir
  76. }
  77. return nil
  78. })
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. for _, replay := range dirs {
  83. tc := replay
  84. t.Run(tc, func(t *testing.T) {
  85. t.Parallel()
  86. path, _ := filepath.Abs(tc)
  87. var stderr bytes.Buffer
  88. expected := expectedStderr(t, path)
  89. output, err := cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
  90. if len(expected) == 0 && err != nil {
  91. t.Fatalf("sqlc generate failed: %s", stderr.String())
  92. }
  93. cmpDirectory(t, path, output)
  94. if diff := cmp.Diff(expected, stderr.String()); diff != "" {
  95. t.Errorf("stderr differed (-want +got):\n%s", diff)
  96. }
  97. })
  98. }
  99. }
  100. func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
  101. expected := map[string]string{}
  102. var ff = func(path string, file os.FileInfo, err error) error {
  103. if err != nil {
  104. return err
  105. }
  106. if file.IsDir() {
  107. return nil
  108. }
  109. if !strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, ".kt") && !strings.HasSuffix(path, ".py") {
  110. return nil
  111. }
  112. if strings.Contains(path, "/kotlin/build") {
  113. return nil
  114. }
  115. if strings.HasSuffix(path, "_test.go") || strings.Contains(path, "src/test/") {
  116. return nil
  117. }
  118. if strings.Contains(path, "/python/.venv") || strings.Contains(path, "/python/src/tests/") ||
  119. strings.HasSuffix(path, "__init__.py") || strings.Contains(path, "/python/src/dbtest/") {
  120. return nil
  121. }
  122. blob, err := os.ReadFile(path)
  123. if err != nil {
  124. return err
  125. }
  126. expected[path] = string(blob)
  127. return nil
  128. }
  129. if err := filepath.Walk(dir, ff); err != nil {
  130. t.Fatal(err)
  131. }
  132. if !cmp.Equal(expected, actual, cmpopts.EquateEmpty()) {
  133. t.Errorf("%s contents differ", dir)
  134. for name, contents := range expected {
  135. name := name
  136. tn := strings.Replace(name, dir+"/", "", -1)
  137. t.Run(tn, func(t *testing.T) {
  138. if actual[name] == "" {
  139. t.Errorf("%s is empty", name)
  140. return
  141. }
  142. if diff := cmp.Diff(contents, actual[name]); diff != "" {
  143. t.Errorf("%s differed (-want +got):\n%s", name, diff)
  144. }
  145. })
  146. }
  147. }
  148. }
  149. func expectedStderr(t *testing.T, dir string) string {
  150. t.Helper()
  151. path := filepath.Join(dir, "stderr.txt")
  152. if _, err := os.Stat(path); !os.IsNotExist(err) {
  153. blob, err := os.ReadFile(path)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. return string(blob)
  158. }
  159. return ""
  160. }
  161. func BenchmarkReplay(b *testing.B) {
  162. ctx := context.Background()
  163. var dirs []string
  164. err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
  165. if err != nil {
  166. return err
  167. }
  168. if info.Name() == "sqlc.json" || info.Name() == "sqlc.yaml" {
  169. dirs = append(dirs, filepath.Dir(path))
  170. return filepath.SkipDir
  171. }
  172. return nil
  173. })
  174. if err != nil {
  175. b.Fatal(err)
  176. }
  177. for _, replay := range dirs {
  178. tc := replay
  179. b.Run(tc, func(b *testing.B) {
  180. path, _ := filepath.Abs(tc)
  181. for i := 0; i < b.N; i++ {
  182. var stderr bytes.Buffer
  183. cmd.Generate(ctx, cmd.Env{ExperimentalFeatures: true}, path, "", &stderr)
  184. }
  185. })
  186. }
  187. }