Skip to content

Commit 50a1440

Browse files
committed
testscript: switch to new diff package
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.
1 parent ec5cf8f commit 50a1440

File tree

4 files changed

+6
-16
lines changed

4 files changed

+6
-16
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/rogpeppe/go-internal
22

33
go 1.18
4-
5-
require github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
2-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=

testscript/cmd.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"strconv"
1717
"strings"
1818

19-
"github.com/pkg/diff"
19+
"github.com/rogpeppe/go-internal/diff"
2020
"github.com/rogpeppe/go-internal/txtar"
2121
)
2222

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

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

159-
ts.Logf("%s", sb.String())
156+
ts.Logf("%s", unifiedDiff)
160157
ts.Fatalf("%s and %s differ", name1, name2)
161158
}
162159

testscript/testdata/long_diff.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,14 @@ cmpenv stdout stdout.golden
111111
>a
112112
-- stdout.golden --
113113
> cmp a b
114+
diff a b
114115
--- a
115116
+++ b
116-
@@ -1,3 +1,4 @@
117+
@@ -1,4 +1,4 @@
118+
-a
117119
+b
118120
a
119121
a
120122
a
121-
@@ -46,4 +47,3 @@
122-
a
123-
a
124-
a
125-
-a
126123

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

0 commit comments

Comments
 (0)