Skip to content

Commit c37bc53

Browse files
committed
ztest: allow multiple ztests per YAML file
Specify multiple ztests in a single file by separating them with a line containing three hyphens ("---"). spq: values 1 output: | 1 --- spq: values 2 output: | 2
1 parent b2b8889 commit c37bc53

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

compiler/parser/parser_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ func searchForSuperSQL() ([]string, error) {
2323
re := regexp.MustCompile(pattern)
2424
err := filepath.Walk("..", func(path string, info os.FileInfo, err error) error {
2525
if !info.IsDir() && strings.HasSuffix(path, ".yaml") && re.MatchString(path) {
26-
zt, err := ztest.FromYAMLFile(path)
26+
ztests, err := ztest.FromYAMLFile(path)
2727
if err != nil {
2828
return fmt.Errorf("%s: %w", path, err)
2929
}
30-
q := zt.SPQ
31-
if q == "" || q == "*" {
32-
return nil
30+
for _, z := range ztests {
31+
if s := z.SPQ; s != "" {
32+
queries = append(queries, s)
33+
}
3334
}
34-
queries = append(queries, q)
3535
}
3636
return err
3737
})

ztest/ztest.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
// simply running "go test" compared replicating the test using "go run".
77
// Script-style tests don't have this convenience.
88
//
9+
// Ztests are specified in YAML files. Multiple ztests can be specified in a
10+
// single file by separating them with a line containing three dashes ("---").
11+
//
912
// In the SPQ style, ztest runs a SuperSQL program on an input and checks
1013
// for an expected output.
1114
//
12-
// A SPQ-style test is defined in a YAML file.
13-
//
1415
// spq: count()
1516
//
1617
// input: |
@@ -171,8 +172,15 @@ func Load(dirname string) ([]Bundle, error) {
171172
}
172173
testname := strings.TrimSuffix(filename, dotyaml)
173174
filename = filepath.Join(dirname, filename)
174-
zt, err := FromYAMLFile(filename)
175-
bundles = append(bundles, Bundle{testname, filename, zt, err})
175+
zts, err := FromYAMLFile(filename)
176+
if err != nil {
177+
bundles = append(bundles, Bundle{testname, filename, nil, err})
178+
continue
179+
}
180+
for _, zt := range zts {
181+
testname := fmt.Sprintf("%s/%d", testname, zt.Line)
182+
bundles = append(bundles, Bundle{testname, filename, zt, nil})
183+
}
176184
}
177185
return bundles, nil
178186
}
@@ -254,6 +262,7 @@ func (f *File) load(dir string) ([]byte, *regexp.Regexp, error) {
254262

255263
// ZTest defines a ztest.
256264
type ZTest struct {
265+
Line int `yaml:"-"`
257266
Skip string `yaml:"skip,omitempty"`
258267
Tag string `yaml:"tag,omitempty"`
259268

@@ -297,20 +306,27 @@ func (z *ZTest) check() error {
297306
return nil
298307
}
299308

300-
// FromYAMLFile loads a ZTest from the YAML file named filename.
301-
func FromYAMLFile(filename string) (*ZTest, error) {
302-
f, err := yamlparser.ParseFile(filename, 0)
309+
// FromYAMLFile loads ztests from the named YAML file.
310+
func FromYAMLFile(name string) ([]*ZTest, error) {
311+
f, err := yamlparser.ParseFile(name, 0)
303312
if err != nil {
304313
return nil, err
305314
}
306-
if len(f.Docs) != 1 {
307-
return nil, errors.New("file must contain one YAML document")
308-
}
309-
var z ZTest
310-
if err := yaml.NodeToValue(f.Docs[0].Body, &z, yaml.DisallowUnknownField()); err != nil {
311-
return nil, err
315+
var zs []*ZTest
316+
for _, d := range f.Docs {
317+
var z ZTest
318+
if err := yaml.NodeToValue(d.Body, &z, yaml.DisallowUnknownField()); err != nil {
319+
return nil, err
320+
}
321+
if s := d.Start; s != nil {
322+
// s is a YAML directives end marker ("---").
323+
z.Line = s.Position.Line + 1
324+
} else {
325+
z.Line = 1
326+
}
327+
zs = append(zs, &z)
312328
}
313-
return &z, nil
329+
return zs, nil
314330
}
315331

316332
func (z *ZTest) ShouldSkip(path string) string {
@@ -392,7 +408,7 @@ func (z *ZTest) Run(t *testing.T, path, filename string) {
392408
err = z.RunInternal(t.Context())
393409
}
394410
if err != nil {
395-
t.Fatalf("%s: %s", filename, err)
411+
t.Fatalf("%s:%d: %s", filename, z.Line, err)
396412
}
397413
}
398414

0 commit comments

Comments
 (0)