Skip to content

Commit 6774d0d

Browse files
committed
internal/cuetxtar: include diff between test and fallback test
This allows a quick glance as to what, if any, differences there are between eval and evalalpha. Especially cases where just error positions change, versus more serious semantic changes, are easily identified. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: If7c5bc2e72f7e0fc368dd69d1d0a3cc578eaba5b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172009 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 265a7a0 commit 6774d0d

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

internal/cuetxtar/txtar.go

+36-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type Test struct {
116116
func (t *Test) Write(b []byte) (n int, err error) {
117117
if t.buf == nil {
118118
t.buf = &bytes.Buffer{}
119-
t.outFiles = append(t.outFiles, file{t.prefix, t.fallback, t.buf})
119+
t.outFiles = append(t.outFiles, file{t.prefix, t.fallback, t.buf, false})
120120
}
121121
return t.buf.Write(b)
122122
}
@@ -125,6 +125,7 @@ type file struct {
125125
name string
126126
fallback string
127127
buf *bytes.Buffer
128+
diff bool // true if this contains a diff between fallback and main
128129
}
129130

130131
// HasTag reports whether the tag with the given key is defined
@@ -226,7 +227,7 @@ func (t *Test) Writer(name string) io.Writer {
226227
}
227228

228229
w := &bytes.Buffer{}
229-
t.outFiles = append(t.outFiles, file{name, fallback, w})
230+
t.outFiles = append(t.outFiles, file{name, fallback, w, false})
230231

231232
if name == t.prefix {
232233
t.buf = w
@@ -386,6 +387,33 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
386387
index[f.Name] = i
387388
}
388389

390+
// Add diff files between fallback and main file. These are added
391+
// as regular output files so that they can be updated as well.
392+
for _, sub := range tc.outFiles {
393+
if sub.fallback == sub.name {
394+
continue
395+
}
396+
if j, ok := index[sub.fallback]; ok {
397+
fallback := a.Files[j].Data
398+
399+
result := sub.buf.Bytes()
400+
if len(result) == 0 || len(fallback) == 0 {
401+
continue
402+
}
403+
404+
diff := cmp.Diff(string(result), string(fallback))
405+
if len(diff) == 0 {
406+
continue
407+
}
408+
409+
tc.outFiles = append(tc.outFiles, file{
410+
name: "diff/-" + sub.name + "<==>+" + sub.fallback,
411+
buf: bytes.NewBufferString(diff),
412+
diff: true,
413+
})
414+
}
415+
}
416+
389417
// Insert results of this test at first location of any existing
390418
// test or at end of list otherwise.
391419
k := len(a.Files)
@@ -432,6 +460,12 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) {
432460
continue
433461
}
434462

463+
// Skip the test if just the diff differs.
464+
// TODO: also fail once diffs are fully in use.
465+
if sub.diff {
466+
continue
467+
}
468+
435469
t.Errorf("result for %s differs: (-want +got)\n%s",
436470
sub.name,
437471
cmp.Diff(string(gold.Data), string(result)),

0 commit comments

Comments
 (0)