Skip to content

Commit

Permalink
internal/cuetxtar: include diff between test and fallback test
Browse files Browse the repository at this point in the history
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 <mpvl@gmail.com>
Change-Id: If7c5bc2e72f7e0fc368dd69d1d0a3cc578eaba5b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1172009
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed Nov 29, 2023
1 parent 265a7a0 commit 6774d0d
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions internal/cuetxtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 6774d0d

Please sign in to comment.