Skip to content

Commit

Permalink
testscript: use unified diff for cmp-style comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
myitcv committed Mar 1, 2021
1 parent d03db15 commit 506c7df
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 47 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/rogpeppe/go-internal

go 1.11

require gopkg.in/errgo.v2 v2.1.0
require (
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e
gopkg.in/errgo.v2 v2.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
Expand Down
9 changes: 7 additions & 2 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/rogpeppe/go-internal/internal/textutil"
"github.com/pkg/diff"
"github.com/rogpeppe/go-internal/txtar"
)

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

ts.Logf("[diff -%s +%s]\n%s\n", name1, name2, textutil.Diff(text1, text2))
var sb strings.Builder
if err := diff.Text(name1, name2, text1, text2, &sb); err != nil {
ts.Check(err)
}

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

Expand Down
49 changes: 5 additions & 44 deletions testscript/testdata/long_diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,56 +112,17 @@ cmpenv stdout stdout.golden
-- stdout.golden --

> cmp a b
[diff -a +b]
-a
--- a
+++ b
@@ -1,3 +1,4 @@
+b
a
a
a
@@ -46,4 +47,3 @@
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
-a

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

0 comments on commit 506c7df

Please sign in to comment.