/
golden_test.go
117 lines (101 loc) · 2.68 KB
/
golden_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2024 RunReveal Inc.
// SPDX-License-Identifier: Apache-2.0
package pql
import (
"bytes"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
var recordGoldens = flag.Bool("record", false, "output golden files")
func TestGoldens(t *testing.T) {
tests, err := findGoldenTests()
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.skip {
t.Skipf("'skip' file present in %s; skipping...", test.dir)
}
input, err := test.input()
if err != nil {
t.Fatal(err)
}
got, err := Compile(input)
if err != nil {
t.Error("Compile(...):", err)
}
outputPath := filepath.Join(test.dir, "output.sql")
if *recordGoldens {
// For easier editing, ensure there is a trailing newline.
if got != "" && !strings.HasSuffix(got, "\n") {
got += "\n"
}
if err := os.WriteFile(outputPath, []byte(got), 0o666); err != nil {
t.Fatal(err)
}
return
}
want, err := os.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
// Strip trailing newlines for comparison.
// Makes it easier to hand-edit goldens when editors place trailing newlines.
got = strings.TrimRight(got, "\n")
want = bytes.TrimRight(want, "\n")
if diff := cmp.Diff(string(want), got); diff != "" {
t.Errorf("output (-want +got):\n%s", diff)
}
})
}
}
type goldenTest struct {
name string
dir string
skip bool
unordered bool
}
func findGoldenTests() ([]goldenTest, error) {
root := filepath.Join("testdata", "Goldens")
rootListing, err := os.ReadDir(root)
if err != nil {
return nil, fmt.Errorf("find golden tests: %v", err)
}
var result []goldenTest
for _, entry := range rootListing {
fileName := entry.Name()
if !entry.IsDir() || shouldIgnoreFilename(fileName) {
continue
}
test := goldenTest{
name: fileName,
dir: filepath.Join(root, fileName),
}
if _, err := os.Stat(filepath.Join(test.dir, "skip")); err == nil {
test.skip = true
} else if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("find golden tests: check for skip: %v", err)
}
if _, err := os.Stat(filepath.Join(test.dir, "unordered")); err == nil {
test.unordered = true
} else if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("find golden tests: check for unordered: %v", err)
}
result = append(result, test)
}
return result, nil
}
func (test goldenTest) input() (string, error) {
input, err := os.ReadFile(filepath.Join(test.dir, "input.pql"))
return string(input), err
}
func shouldIgnoreFilename(name string) bool {
return strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_")
}