Skip to content

Commit 7389fe9

Browse files
committed
cue/load: use tdtest for TestLoad
This makes the subsequent test difference in the next CL easier to read. For #2330. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I304c27366ac26183cbc95303d26dd3bbf0c1c78c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168846 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 28f34a2 commit 7389fe9

File tree

1 file changed

+60
-89
lines changed

1 file changed

+60
-89
lines changed

cue/load/loader_test.go

+60-89
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ import (
1818
"bytes"
1919
"os"
2020
"path/filepath"
21-
"strconv"
2221
"strings"
2322
"sync"
2423
"testing"
2524
"text/template"
2625
"unicode"
2726

28-
"github.com/google/go-cmp/cmp"
29-
3027
"cuelang.org/go/cue"
3128
"cuelang.org/go/cue/errors"
3229
"cuelang.org/go/cue/format"
3330
"cuelang.org/go/internal/str"
31+
"cuelang.org/go/internal/tdtest"
3432
)
3533

3634
// TestLoad is an end-to-end test.
@@ -48,16 +46,17 @@ func TestLoad(t *testing.T) {
4846
Dir: testMod("badmod"),
4947
}
5048

51-
args := str.StringList
52-
testCases := []struct {
49+
type loadTest struct {
5350
cfg *Config
5451
args []string
5552
want string
56-
}{{
53+
}
54+
55+
args := str.StringList
56+
testCases := []loadTest{{
5757
cfg: badModCfg,
5858
args: args("."),
59-
want: `
60-
err: module: invalid module.cue file: 2 errors in empty disjunction:
59+
want: `err: module: invalid module.cue file: 2 errors in empty disjunction:
6160
module: invalid module.cue file: conflicting values 123 and "" (mismatched types int and string):
6261
$cueroot/cue/load/moduleschema.cue:4:20
6362
$CWD/testdata/badmod/cue.mod/module.cue:2:9
@@ -69,16 +68,14 @@ path: ""
6968
module: ""
7069
root: ""
7170
dir: ""
72-
display:""
73-
`,
71+
display:""`,
7472
}, {
7573
// Even though the directory is called testdata, the last path in
7674
// the module is test. So "package test" is correctly the default
7775
// package of this directory.
7876
cfg: dirCfg,
7977
args: nil,
80-
want: `
81-
path: mod.test/test
78+
want: `path: mod.test/test
8279
module: mod.test/test
8380
root: $CWD/testdata/testmod
8481
dir: $CWD/testdata/testmod
@@ -93,8 +90,7 @@ imports:
9390
// package of this directory.
9491
cfg: dirCfg,
9592
args: args("."),
96-
want: `
97-
path: mod.test/test
93+
want: `path: mod.test/test
9894
module: mod.test/test
9995
root: $CWD/testdata/testmod
10096
dir: $CWD/testdata/testmod
@@ -108,8 +104,7 @@ imports:
108104
// - path incorrect, should be mod.test/test/other:main.
109105
cfg: dirCfg,
110106
args: args("./other/..."),
111-
want: `
112-
err: import failed: relative import paths not allowed ("./file"):
107+
want: `err: import failed: relative import paths not allowed ("./file"):
113108
$CWD/testdata/testmod/other/main.cue:6:2
114109
path: ""
115110
module: mod.test/test
@@ -119,9 +114,8 @@ display:""`,
119114
}, {
120115
cfg: dirCfg,
121116
args: args("./anon"),
122-
want: `
123-
err: build constraints exclude all CUE files in ./anon:
124-
anon/anon.cue: no package name
117+
want: `err: build constraints exclude all CUE files in ./anon:
118+
anon/anon.cue: no package name
125119
path: mod.test/test/anon
126120
module: mod.test/test
127121
root: $CWD/testdata/testmod
@@ -132,55 +126,51 @@ display:./anon`,
132126
// - paths are incorrect, should be mod.test/test/other:main.
133127
cfg: dirCfg,
134128
args: args("./other"),
135-
want: `
136-
err: import failed: relative import paths not allowed ("./file"):
129+
want: `err: import failed: relative import paths not allowed ("./file"):
137130
$CWD/testdata/testmod/other/main.cue:6:2
138131
path: mod.test/test/other:main
139132
module: mod.test/test
140133
root: $CWD/testdata/testmod
141134
dir: $CWD/testdata/testmod/other
142135
display:./other
143136
files:
144-
$CWD/testdata/testmod/other/main.cue`,
137+
$CWD/testdata/testmod/other/main.cue`,
145138
}, {
146139
// TODO:
147140
// - incorrect path, should be mod.test/test/hello:test
148141
cfg: dirCfg,
149142
args: args("./hello"),
150-
want: `
151-
path: mod.test/test/hello:test
143+
want: `path: mod.test/test/hello:test
152144
module: mod.test/test
153145
root: $CWD/testdata/testmod
154146
dir: $CWD/testdata/testmod/hello
155147
display:./hello
156148
files:
157-
$CWD/testdata/testmod/test.cue
158-
$CWD/testdata/testmod/hello/test.cue
149+
$CWD/testdata/testmod/test.cue
150+
$CWD/testdata/testmod/hello/test.cue
159151
imports:
160-
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
152+
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
161153
}, {
162154
// TODO:
163155
// - incorrect path, should be mod.test/test/hello:test
164156
cfg: dirCfg,
165157
args: args("mod.test/test/hello:test"),
166-
want: `
167-
path: mod.test/test/hello:test
158+
want: `path: mod.test/test/hello:test
168159
module: mod.test/test
169160
root: $CWD/testdata/testmod
170161
dir: $CWD/testdata/testmod/hello
171162
display:mod.test/test/hello:test
172163
files:
173-
$CWD/testdata/testmod/test.cue
174-
$CWD/testdata/testmod/hello/test.cue
164+
$CWD/testdata/testmod/test.cue
165+
$CWD/testdata/testmod/hello/test.cue
175166
imports:
176-
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
167+
mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue`,
177168
}, {
178169
// TODO:
179170
// - incorrect path, should be mod.test/test/hello:test
180171
cfg: dirCfg,
181172
args: args("mod.test/test/hello:nonexist"),
182-
want: `
183-
err: build constraints exclude all CUE files in mod.test/test/hello:nonexist:
173+
want: `err: build constraints exclude all CUE files in mod.test/test/hello:nonexist:
184174
anon.cue: no package name
185175
test.cue: package is test, want nonexist
186176
hello/test.cue: package is test, want nonexist
@@ -192,21 +182,19 @@ display:mod.test/test/hello:nonexist`,
192182
}, {
193183
cfg: dirCfg,
194184
args: args("./anon.cue", "./other/anon.cue"),
195-
want: `
196-
path: ""
185+
want: `path: ""
197186
module: ""
198187
root: $CWD/testdata/testmod
199188
dir: $CWD/testdata/testmod
200189
display:command-line-arguments
201190
files:
202-
$CWD/testdata/testmod/anon.cue
203-
$CWD/testdata/testmod/other/anon.cue`,
191+
$CWD/testdata/testmod/anon.cue
192+
$CWD/testdata/testmod/other/anon.cue`,
204193
}, {
205194
cfg: dirCfg,
206195
// Absolute file is normalized.
207196
args: args(filepath.Join(testMod("testmod"), "anon.cue")),
208-
want: `
209-
path: ""
197+
want: `path: ""
210198
module: ""
211199
root: $CWD/testdata/testmod
212200
dir: $CWD/testdata/testmod
@@ -216,8 +204,7 @@ files:
216204
}, {
217205
cfg: dirCfg,
218206
args: args("-"),
219-
want: `
220-
path: ""
207+
want: `path: ""
221208
module: ""
222209
root: $CWD/testdata/testmod
223210
dir: $CWD/testdata/testmod
@@ -228,8 +215,7 @@ files:
228215
// NOTE: dir should probably be set to $CWD/testdata, but either way.
229216
cfg: dirCfg,
230217
args: args("non-existing"),
231-
want: `
232-
err: cannot find package "non-existing"
218+
want: `err: cannot find package "non-existing"
233219
path: non-existing
234220
module: mod.test/test
235221
root: $CWD/testdata/testmod
@@ -238,8 +224,7 @@ display:non-existing`,
238224
}, {
239225
cfg: dirCfg,
240226
args: args("./empty"),
241-
want: `
242-
err: no CUE files in ./empty
227+
want: `err: no CUE files in ./empty
243228
path: mod.test/test/empty
244229
module: mod.test/test
245230
root: $CWD/testdata/testmod
@@ -248,35 +233,32 @@ display:./empty`,
248233
}, {
249234
cfg: dirCfg,
250235
args: args("./imports"),
251-
want: `
252-
path: mod.test/test/imports
236+
want: `path: mod.test/test/imports
253237
module: mod.test/test
254238
root: $CWD/testdata/testmod
255239
dir: $CWD/testdata/testmod/imports
256240
display:./imports
257241
files:
258-
$CWD/testdata/testmod/imports/imports.cue
242+
$CWD/testdata/testmod/imports/imports.cue
259243
imports:
260-
mod.test/catch: $CWD/testdata/testmod/cue.mod/pkg/mod.test/catch/catch.cue
261-
mod.test/helper:helper1: $CWD/testdata/testmod/cue.mod/pkg/mod.test/helper/helper1.cue`,
244+
mod.test/catch: $CWD/testdata/testmod/cue.mod/pkg/mod.test/catch/catch.cue
245+
mod.test/helper:helper1: $CWD/testdata/testmod/cue.mod/pkg/mod.test/helper/helper1.cue`,
262246
}, {
263247
cfg: dirCfg,
264248
args: args("./toolonly"),
265-
want: `
266-
path: mod.test/test/toolonly:foo
249+
want: `path: mod.test/test/toolonly:foo
267250
module: mod.test/test
268251
root: $CWD/testdata/testmod
269252
dir: $CWD/testdata/testmod/toolonly
270253
display:./toolonly
271254
files:
272-
$CWD/testdata/testmod/toolonly/foo_tool.cue`,
255+
$CWD/testdata/testmod/toolonly/foo_tool.cue`,
273256
}, {
274257
cfg: &Config{
275258
Dir: testdataDir,
276259
},
277260
args: args("./toolonly"),
278-
want: `
279-
err: build constraints exclude all CUE files in ./toolonly:
261+
want: `err: build constraints exclude all CUE files in ./toolonly:
280262
anon.cue: no package name
281263
test.cue: package is test, want foo
282264
toolonly/foo_tool.cue: _tool.cue files excluded in non-cmd mode
@@ -291,36 +273,33 @@ display:./toolonly`,
291273
Tags: []string{"prod"},
292274
},
293275
args: args("./tags"),
294-
want: `
295-
path: mod.test/test/tags
276+
want: `path: mod.test/test/tags
296277
module: mod.test/test
297278
root: $CWD/testdata/testmod
298279
dir: $CWD/testdata/testmod/tags
299280
display:./tags
300281
files:
301-
$CWD/testdata/testmod/tags/prod.cue`,
282+
$CWD/testdata/testmod/tags/prod.cue`,
302283
}, {
303284
cfg: &Config{
304285
Dir: testdataDir,
305286
Tags: []string{"prod", "foo=bar"},
306287
},
307288
args: args("./tags"),
308-
want: `
309-
path: mod.test/test/tags
289+
want: `path: mod.test/test/tags
310290
module: mod.test/test
311291
root: $CWD/testdata/testmod
312292
dir: $CWD/testdata/testmod/tags
313293
display:./tags
314294
files:
315-
$CWD/testdata/testmod/tags/prod.cue`,
295+
$CWD/testdata/testmod/tags/prod.cue`,
316296
}, {
317297
cfg: &Config{
318298
Dir: testdataDir,
319299
Tags: []string{"prod"},
320300
},
321301
args: args("./tagsbad"),
322-
want: `
323-
err: tag "prod" not used in any file
302+
want: `err: tag "prod" not used in any file
324303
previous declaration here:
325304
$CWD/testdata/testmod/tagsbad/prod.cue:1:1
326305
multiple @if attributes:
@@ -335,8 +314,7 @@ display:./tagsbad`,
335314
Dir: testdataDir,
336315
},
337316
args: args("./cycle"),
338-
want: `
339-
err: import failed: import failed: import failed: package import cycle not allowed:
317+
want: `err: import failed: import failed: import failed: package import cycle not allowed:
340318
$CWD/testdata/testmod/cycle/cycle.cue:3:8
341319
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/bar/bar.cue:3:8
342320
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/foo/foo.cue:3:8
@@ -348,32 +326,25 @@ display:./cycle
348326
files:
349327
$CWD/testdata/testmod/cycle/cycle.cue`,
350328
}}
351-
for i, tc := range testCases {
352-
t.Run(strconv.Itoa(i)+"/"+strings.Join(tc.args, ":"), func(t *testing.T) {
353-
pkgs := Instances(tc.args, tc.cfg)
329+
tdtest.Run(t, testCases, func(t *tdtest.T, tc *loadTest) {
330+
pkgs := Instances(tc.args, tc.cfg)
354331

355-
buf := &bytes.Buffer{}
356-
err := pkgInfo.Execute(buf, pkgs)
357-
if err != nil {
358-
t.Fatal(err)
359-
}
332+
buf := &bytes.Buffer{}
333+
err := pkgInfo.Execute(buf, pkgs)
334+
if err != nil {
335+
t.Fatal(err)
336+
}
360337

361-
got := strings.TrimSpace(buf.String())
362-
got = strings.Replace(got, cwd, "$CWD", -1)
363-
// Errors are printed with slashes, so replace
364-
// the slash-separated form of CWD too.
365-
got = strings.Replace(got, filepath.ToSlash(cwd), "$CWD", -1)
366-
// Make test work with Windows.
367-
got = strings.Replace(got, string(filepath.Separator), "/", -1)
338+
got := strings.TrimSpace(buf.String())
339+
got = strings.Replace(got, cwd, "$CWD", -1)
340+
// Errors are printed with slashes, so replace
341+
// the slash-separated form of CWD too.
342+
got = strings.Replace(got, filepath.ToSlash(cwd), "$CWD", -1)
343+
// Make test work with Windows.
344+
got = strings.Replace(got, string(filepath.Separator), "/", -1)
368345

369-
want := strings.TrimSpace(tc.want)
370-
want = strings.Replace(want, "\t", " ", -1)
371-
if got != want {
372-
t.Errorf("\n%s", cmp.Diff(want, got))
373-
t.Logf("\n%s", got)
374-
}
375-
})
376-
}
346+
t.Equal(got, tc.want)
347+
})
377348
}
378349

379350
var pkgInfo = template.Must(template.New("pkg").Funcs(template.FuncMap{

0 commit comments

Comments
 (0)