@@ -18,19 +18,17 @@ import (
18
18
"bytes"
19
19
"os"
20
20
"path/filepath"
21
- "strconv"
22
21
"strings"
23
22
"sync"
24
23
"testing"
25
24
"text/template"
26
25
"unicode"
27
26
28
- "github.com/google/go-cmp/cmp"
29
-
30
27
"cuelang.org/go/cue"
31
28
"cuelang.org/go/cue/errors"
32
29
"cuelang.org/go/cue/format"
33
30
"cuelang.org/go/internal/str"
31
+ "cuelang.org/go/internal/tdtest"
34
32
)
35
33
36
34
// TestLoad is an end-to-end test.
@@ -48,16 +46,17 @@ func TestLoad(t *testing.T) {
48
46
Dir : testMod ("badmod" ),
49
47
}
50
48
51
- args := str .StringList
52
- testCases := []struct {
49
+ type loadTest struct {
53
50
cfg * Config
54
51
args []string
55
52
want string
56
- }{{
53
+ }
54
+
55
+ args := str .StringList
56
+ testCases := []loadTest {{
57
57
cfg : badModCfg ,
58
58
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:
61
60
module: invalid module.cue file: conflicting values 123 and "" (mismatched types int and string):
62
61
$cueroot/cue/load/moduleschema.cue:4:20
63
62
$CWD/testdata/badmod/cue.mod/module.cue:2:9
@@ -69,16 +68,14 @@ path: ""
69
68
module: ""
70
69
root: ""
71
70
dir: ""
72
- display:""
73
- ` ,
71
+ display:""` ,
74
72
}, {
75
73
// Even though the directory is called testdata, the last path in
76
74
// the module is test. So "package test" is correctly the default
77
75
// package of this directory.
78
76
cfg : dirCfg ,
79
77
args : nil ,
80
- want : `
81
- path: mod.test/test
78
+ want : `path: mod.test/test
82
79
module: mod.test/test
83
80
root: $CWD/testdata/testmod
84
81
dir: $CWD/testdata/testmod
@@ -93,8 +90,7 @@ imports:
93
90
// package of this directory.
94
91
cfg : dirCfg ,
95
92
args : args ("." ),
96
- want : `
97
- path: mod.test/test
93
+ want : `path: mod.test/test
98
94
module: mod.test/test
99
95
root: $CWD/testdata/testmod
100
96
dir: $CWD/testdata/testmod
@@ -108,8 +104,7 @@ imports:
108
104
// - path incorrect, should be mod.test/test/other:main.
109
105
cfg : dirCfg ,
110
106
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"):
113
108
$CWD/testdata/testmod/other/main.cue:6:2
114
109
path: ""
115
110
module: mod.test/test
@@ -119,9 +114,8 @@ display:""`,
119
114
}, {
120
115
cfg : dirCfg ,
121
116
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
125
119
path: mod.test/test/anon
126
120
module: mod.test/test
127
121
root: $CWD/testdata/testmod
@@ -132,55 +126,51 @@ display:./anon`,
132
126
// - paths are incorrect, should be mod.test/test/other:main.
133
127
cfg : dirCfg ,
134
128
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"):
137
130
$CWD/testdata/testmod/other/main.cue:6:2
138
131
path: mod.test/test/other:main
139
132
module: mod.test/test
140
133
root: $CWD/testdata/testmod
141
134
dir: $CWD/testdata/testmod/other
142
135
display:./other
143
136
files:
144
- $CWD/testdata/testmod/other/main.cue` ,
137
+ $CWD/testdata/testmod/other/main.cue` ,
145
138
}, {
146
139
// TODO:
147
140
// - incorrect path, should be mod.test/test/hello:test
148
141
cfg : dirCfg ,
149
142
args : args ("./hello" ),
150
- want : `
151
- path: mod.test/test/hello:test
143
+ want : `path: mod.test/test/hello:test
152
144
module: mod.test/test
153
145
root: $CWD/testdata/testmod
154
146
dir: $CWD/testdata/testmod/hello
155
147
display:./hello
156
148
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
159
151
imports:
160
- mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue` ,
152
+ mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue` ,
161
153
}, {
162
154
// TODO:
163
155
// - incorrect path, should be mod.test/test/hello:test
164
156
cfg : dirCfg ,
165
157
args : args ("mod.test/test/hello:test" ),
166
- want : `
167
- path: mod.test/test/hello:test
158
+ want : `path: mod.test/test/hello:test
168
159
module: mod.test/test
169
160
root: $CWD/testdata/testmod
170
161
dir: $CWD/testdata/testmod/hello
171
162
display:mod.test/test/hello:test
172
163
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
175
166
imports:
176
- mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue` ,
167
+ mod.test/test/sub: $CWD/testdata/testmod/sub/sub.cue` ,
177
168
}, {
178
169
// TODO:
179
170
// - incorrect path, should be mod.test/test/hello:test
180
171
cfg : dirCfg ,
181
172
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:
184
174
anon.cue: no package name
185
175
test.cue: package is test, want nonexist
186
176
hello/test.cue: package is test, want nonexist
@@ -192,21 +182,19 @@ display:mod.test/test/hello:nonexist`,
192
182
}, {
193
183
cfg : dirCfg ,
194
184
args : args ("./anon.cue" , "./other/anon.cue" ),
195
- want : `
196
- path: ""
185
+ want : `path: ""
197
186
module: ""
198
187
root: $CWD/testdata/testmod
199
188
dir: $CWD/testdata/testmod
200
189
display:command-line-arguments
201
190
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` ,
204
193
}, {
205
194
cfg : dirCfg ,
206
195
// Absolute file is normalized.
207
196
args : args (filepath .Join (testMod ("testmod" ), "anon.cue" )),
208
- want : `
209
- path: ""
197
+ want : `path: ""
210
198
module: ""
211
199
root: $CWD/testdata/testmod
212
200
dir: $CWD/testdata/testmod
@@ -216,8 +204,7 @@ files:
216
204
}, {
217
205
cfg : dirCfg ,
218
206
args : args ("-" ),
219
- want : `
220
- path: ""
207
+ want : `path: ""
221
208
module: ""
222
209
root: $CWD/testdata/testmod
223
210
dir: $CWD/testdata/testmod
@@ -228,8 +215,7 @@ files:
228
215
// NOTE: dir should probably be set to $CWD/testdata, but either way.
229
216
cfg : dirCfg ,
230
217
args : args ("non-existing" ),
231
- want : `
232
- err: cannot find package "non-existing"
218
+ want : `err: cannot find package "non-existing"
233
219
path: non-existing
234
220
module: mod.test/test
235
221
root: $CWD/testdata/testmod
@@ -238,8 +224,7 @@ display:non-existing`,
238
224
}, {
239
225
cfg : dirCfg ,
240
226
args : args ("./empty" ),
241
- want : `
242
- err: no CUE files in ./empty
227
+ want : `err: no CUE files in ./empty
243
228
path: mod.test/test/empty
244
229
module: mod.test/test
245
230
root: $CWD/testdata/testmod
@@ -248,35 +233,32 @@ display:./empty`,
248
233
}, {
249
234
cfg : dirCfg ,
250
235
args : args ("./imports" ),
251
- want : `
252
- path: mod.test/test/imports
236
+ want : `path: mod.test/test/imports
253
237
module: mod.test/test
254
238
root: $CWD/testdata/testmod
255
239
dir: $CWD/testdata/testmod/imports
256
240
display:./imports
257
241
files:
258
- $CWD/testdata/testmod/imports/imports.cue
242
+ $CWD/testdata/testmod/imports/imports.cue
259
243
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` ,
262
246
}, {
263
247
cfg : dirCfg ,
264
248
args : args ("./toolonly" ),
265
- want : `
266
- path: mod.test/test/toolonly:foo
249
+ want : `path: mod.test/test/toolonly:foo
267
250
module: mod.test/test
268
251
root: $CWD/testdata/testmod
269
252
dir: $CWD/testdata/testmod/toolonly
270
253
display:./toolonly
271
254
files:
272
- $CWD/testdata/testmod/toolonly/foo_tool.cue` ,
255
+ $CWD/testdata/testmod/toolonly/foo_tool.cue` ,
273
256
}, {
274
257
cfg : & Config {
275
258
Dir : testdataDir ,
276
259
},
277
260
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:
280
262
anon.cue: no package name
281
263
test.cue: package is test, want foo
282
264
toolonly/foo_tool.cue: _tool.cue files excluded in non-cmd mode
@@ -291,36 +273,33 @@ display:./toolonly`,
291
273
Tags : []string {"prod" },
292
274
},
293
275
args : args ("./tags" ),
294
- want : `
295
- path: mod.test/test/tags
276
+ want : `path: mod.test/test/tags
296
277
module: mod.test/test
297
278
root: $CWD/testdata/testmod
298
279
dir: $CWD/testdata/testmod/tags
299
280
display:./tags
300
281
files:
301
- $CWD/testdata/testmod/tags/prod.cue` ,
282
+ $CWD/testdata/testmod/tags/prod.cue` ,
302
283
}, {
303
284
cfg : & Config {
304
285
Dir : testdataDir ,
305
286
Tags : []string {"prod" , "foo=bar" },
306
287
},
307
288
args : args ("./tags" ),
308
- want : `
309
- path: mod.test/test/tags
289
+ want : `path: mod.test/test/tags
310
290
module: mod.test/test
311
291
root: $CWD/testdata/testmod
312
292
dir: $CWD/testdata/testmod/tags
313
293
display:./tags
314
294
files:
315
- $CWD/testdata/testmod/tags/prod.cue` ,
295
+ $CWD/testdata/testmod/tags/prod.cue` ,
316
296
}, {
317
297
cfg : & Config {
318
298
Dir : testdataDir ,
319
299
Tags : []string {"prod" },
320
300
},
321
301
args : args ("./tagsbad" ),
322
- want : `
323
- err: tag "prod" not used in any file
302
+ want : `err: tag "prod" not used in any file
324
303
previous declaration here:
325
304
$CWD/testdata/testmod/tagsbad/prod.cue:1:1
326
305
multiple @if attributes:
@@ -335,8 +314,7 @@ display:./tagsbad`,
335
314
Dir : testdataDir ,
336
315
},
337
316
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:
340
318
$CWD/testdata/testmod/cycle/cycle.cue:3:8
341
319
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/bar/bar.cue:3:8
342
320
$CWD/testdata/testmod/cue.mod/pkg/mod.test/cycle/foo/foo.cue:3:8
@@ -348,32 +326,25 @@ display:./cycle
348
326
files:
349
327
$CWD/testdata/testmod/cycle/cycle.cue` ,
350
328
}}
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 )
354
331
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
+ }
360
337
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 )
368
345
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
+ })
377
348
}
378
349
379
350
var pkgInfo = template .Must (template .New ("pkg" ).Funcs (template.FuncMap {
0 commit comments