From 6774d0df6229412fb683fbc3ba7d1a9502229267 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Tue, 7 Nov 2023 16:52:54 +0100 Subject: [PATCH] internal/cuetxtar: include diff between test and fallback test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: If7c5bc2e72f7e0fc368dd69d1d0a3cc578eaba5b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172009 TryBot-Result: CUEcueckoo Reviewed-by: Daniel Martí --- internal/cuetxtar/txtar.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/internal/cuetxtar/txtar.go b/internal/cuetxtar/txtar.go index d82d31aa7c1..8d026814498 100644 --- a/internal/cuetxtar/txtar.go +++ b/internal/cuetxtar/txtar.go @@ -116,7 +116,7 @@ type Test struct { func (t *Test) Write(b []byte) (n int, err error) { if t.buf == nil { t.buf = &bytes.Buffer{} - t.outFiles = append(t.outFiles, file{t.prefix, t.fallback, t.buf}) + t.outFiles = append(t.outFiles, file{t.prefix, t.fallback, t.buf, false}) } return t.buf.Write(b) } @@ -125,6 +125,7 @@ type file struct { name string fallback string buf *bytes.Buffer + diff bool // true if this contains a diff between fallback and main } // HasTag reports whether the tag with the given key is defined @@ -226,7 +227,7 @@ func (t *Test) Writer(name string) io.Writer { } w := &bytes.Buffer{} - t.outFiles = append(t.outFiles, file{name, fallback, w}) + t.outFiles = append(t.outFiles, file{name, fallback, w, false}) if name == t.prefix { t.buf = w @@ -386,6 +387,33 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) { index[f.Name] = i } + // Add diff files between fallback and main file. These are added + // as regular output files so that they can be updated as well. + for _, sub := range tc.outFiles { + if sub.fallback == sub.name { + continue + } + if j, ok := index[sub.fallback]; ok { + fallback := a.Files[j].Data + + result := sub.buf.Bytes() + if len(result) == 0 || len(fallback) == 0 { + continue + } + + diff := cmp.Diff(string(result), string(fallback)) + if len(diff) == 0 { + continue + } + + tc.outFiles = append(tc.outFiles, file{ + name: "diff/-" + sub.name + "<==>+" + sub.fallback, + buf: bytes.NewBufferString(diff), + diff: true, + }) + } + } + // Insert results of this test at first location of any existing // test or at end of list otherwise. k := len(a.Files) @@ -432,6 +460,12 @@ func (x *TxTarTest) Run(t *testing.T, f func(tc *Test)) { continue } + // Skip the test if just the diff differs. + // TODO: also fail once diffs are fully in use. + if sub.diff { + continue + } + t.Errorf("result for %s differs: (-want +got)\n%s", sub.name, cmp.Diff(string(gold.Data), string(result)),