Skip to content

Commit 2630b2f

Browse files
authored
testscript: use unified diff for cmp-style comparisons (#131)
1 parent d03db15 commit 2630b2f

File tree

4 files changed

+18
-47
lines changed

4 files changed

+18
-47
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/rogpeppe/go-internal
22

33
go 1.11
44

5-
require gopkg.in/errgo.v2 v2.1.0
5+
require (
6+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
7+
gopkg.in/errgo.v2 v2.1.0
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
33
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
44
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
55
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
7+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
68
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
79
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
810
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=

testscript/cmd.go

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

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

@@ -141,7 +141,12 @@ func (ts *TestScript) doCmdCmp(args []string, env bool) {
141141
// update the script.
142142
}
143143

144-
ts.Logf("[diff -%s +%s]\n%s\n", name1, name2, textutil.Diff(text1, text2))
144+
var sb strings.Builder
145+
if err := diff.Text(name1, name2, text1, text2, &sb); err != nil {
146+
ts.Check(err)
147+
}
148+
149+
ts.Logf("%s", sb.String())
145150
ts.Fatalf("%s and %s differ", name1, name2)
146151
}
147152

testscript/testdata/long_diff.txt

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -112,56 +112,17 @@ cmpenv stdout stdout.golden
112112
-- stdout.golden --
113113

114114
> cmp a b
115-
[diff -a +b]
116-
-a
115+
--- a
116+
+++ b
117+
@@ -1,3 +1,4 @@
117118
+b
118119
a
119120
a
120121
a
122+
@@ -46,4 +47,3 @@
121123
a
122124
a
123125
a
124-
a
125-
a
126-
a
127-
a
128-
a
129-
a
130-
a
131-
a
132-
a
133-
a
134-
a
135-
a
136-
a
137-
a
138-
a
139-
a
140-
a
141-
a
142-
a
143-
a
144-
a
145-
a
146-
a
147-
a
148-
a
149-
a
150-
a
151-
a
152-
a
153-
a
154-
a
155-
a
156-
a
157-
a
158-
a
159-
a
160-
a
161-
a
162-
a
163-
a
164-
a
165-
a
126+
-a
166127

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

0 commit comments

Comments
 (0)