Skip to content

Commit

Permalink
testscript: switch to new diff package
Browse files Browse the repository at this point in the history
The main reason to prefer a copy of Go's internal/diff over pkg/diff is
that internal/diff is much more efficient in both time and memory usage.
In particular, pkg/diff required quadratic space in memory,
which could easily cause "out of memory" errors in Go tests
per pkg/diff#26.

Beyond making the `cmp` command better able to handle large files,
this also moves us back to having zero external dependencies,
which is always a nice to have.

The long_diff test still appears to work well;
the output is changed since the new package produces a shorter,
but still entirely correct, diff.

It also seems like the new package includes a leading "diff" line to
show the two filenames. That seems like a harmless change.
  • Loading branch information
mvdan committed Mar 22, 2023
1 parent ec5cf8f commit 50a1440
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 16 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/rogpeppe/go-internal

go 1.18

require github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
9 changes: 3 additions & 6 deletions testscript/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"strconv"
"strings"

"github.com/pkg/diff"
"github.com/rogpeppe/go-internal/diff"
"github.com/rogpeppe/go-internal/txtar"
)

Expand Down Expand Up @@ -151,12 +151,9 @@ func (ts *TestScript) doCmdCmp(neg bool, args []string, env bool) {
return
}

var sb strings.Builder
if err := diff.Text(name1, name2, text1, text2, &sb); err != nil {
ts.Check(err)
}
unifiedDiff := diff.Diff(name1, []byte(text1), name2, []byte(text2))

ts.Logf("%s", sb.String())
ts.Logf("%s", unifiedDiff)
ts.Fatalf("%s and %s differ", name1, name2)
}

Expand Down
9 changes: 3 additions & 6 deletions testscript/testdata/long_diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,14 @@ cmpenv stdout stdout.golden
>a
-- stdout.golden --
> cmp a b
diff a b
--- a
+++ b
@@ -1,3 +1,4 @@
@@ -1,4 +1,4 @@
-a
+b
a
a
a
@@ -46,4 +47,3 @@
a
a
a
-a

FAIL: $$WORK${/}dir${/}script.txt:1: a and b differ

0 comments on commit 50a1440

Please sign in to comment.