@@ -49,6 +49,12 @@ type TxTarTest struct {
49
49
// TODO: by default derive from the current base directory name.
50
50
Name string
51
51
52
+ // Fallback allows the golden tests named by Fallback to pass tests in
53
+ // case the golden file corresponding to Name does not exist.
54
+ // The feature can be used to have two implementations of the same
55
+ // functionality share the same test sets.
56
+ Fallback string
57
+
52
58
// Skip is a map of tests to skip; the key is the test name; the value is the
53
59
// skip message.
54
60
Skip map [string ]string
@@ -92,6 +98,7 @@ type Test struct {
92
98
* testing.T
93
99
94
100
prefix string
101
+ fallback string
95
102
buf * bytes.Buffer // the default buffer
96
103
outFiles []file
97
104
@@ -109,14 +116,15 @@ type Test struct {
109
116
func (t * Test ) Write (b []byte ) (n int , err error ) {
110
117
if t .buf == nil {
111
118
t .buf = & bytes.Buffer {}
112
- t .outFiles = append (t .outFiles , file {t .prefix , t .buf })
119
+ t .outFiles = append (t .outFiles , file {t .prefix , t .fallback , t . buf })
113
120
}
114
121
return t .buf .Write (b )
115
122
}
116
123
117
124
type file struct {
118
- name string
119
- buf * bytes.Buffer
125
+ name string
126
+ fallback string
127
+ buf * bytes.Buffer
120
128
}
121
129
122
130
// HasTag reports whether the tag with the given key is defined
@@ -201,10 +209,13 @@ func (t *Test) WriteFile(f *ast.File) {
201
209
// in the txtar file. If name is empty, data will be written to the test
202
210
// output and checked against "out/\(testName)".
203
211
func (t * Test ) Writer (name string ) io.Writer {
212
+ var fallback string
204
213
switch name {
205
214
case "" :
206
215
name = t .prefix
216
+ fallback = t .fallback
207
217
default :
218
+ fallback = path .Join (t .fallback , name )
208
219
name = path .Join (t .prefix , name )
209
220
}
210
221
@@ -215,7 +226,7 @@ func (t *Test) Writer(name string) io.Writer {
215
226
}
216
227
217
228
w := & bytes.Buffer {}
218
- t .outFiles = append (t .outFiles , file {name , w })
229
+ t .outFiles = append (t .outFiles , file {name , fallback , w })
219
230
220
231
if name == t .prefix {
221
232
t .buf = w
@@ -326,6 +337,11 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
326
337
prefix : path .Join ("out" , x .Name ),
327
338
LoadConfig : x .LoadConfig ,
328
339
}
340
+ if x .Fallback != "" {
341
+ tc .fallback = path .Join ("out" , x .Fallback )
342
+ } else {
343
+ tc .fallback = tc .prefix
344
+ }
329
345
330
346
if tc .HasTag ("skip" ) {
331
347
t .Skip ()
@@ -341,13 +357,14 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
341
357
update := false
342
358
343
359
for i , f := range a .Files {
344
-
345
- if strings .HasPrefix (f .Name , tc .prefix ) && (f .Name == tc .prefix || f .Name [len (tc .prefix )] == '/' ) {
360
+ hasPrefix := func (s string ) bool {
346
361
// It's either "\(tc.prefix)" or "\(tc.prefix)/..." but not some other name
347
362
// that happens to start with tc.prefix.
348
- tc . hasGold = true
363
+ return strings . HasPrefix ( f . Name , s ) && ( f . Name == s || f . Name [ len ( s )] == '/' )
349
364
}
350
365
366
+ tc .hasGold = hasPrefix (tc .prefix ) || hasPrefix (tc .fallback )
367
+
351
368
// Format CUE files as required
352
369
if tc .HasTag ("noformat" ) || ! strings .HasSuffix (f .Name , ".cue" ) {
353
370
continue
@@ -377,6 +394,10 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
377
394
k = i
378
395
break
379
396
}
397
+ if i , ok := index [sub .fallback ]; ok {
398
+ k = i
399
+ break
400
+ }
380
401
}
381
402
382
403
files := a .Files [:k :k ]
@@ -394,6 +415,15 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
394
415
if bytes .Equal (gold .Data , result ) {
395
416
continue
396
417
}
418
+ } else if i , ok := index [sub .fallback ]; ok {
419
+ gold .Data = a .Files [i ].Data
420
+
421
+ // Use the golden file of the fallback set if it matches.
422
+ if bytes .Equal (gold .Data , result ) {
423
+ gold .Name = sub .fallback
424
+ delete (index , sub .fallback )
425
+ continue
426
+ }
397
427
}
398
428
399
429
if cuetest .UpdateGoldenFiles {
0 commit comments