-
Notifications
You must be signed in to change notification settings - Fork 1.7k
assert: switch the diff library to go-diff #1546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mitioshi
wants to merge
1
commit into
stretchr:master
Choose a base branch
from
mitioshi:switch-diff-library-to-go-diff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ import ( | |
"regexp" | ||
"runtime" | ||
"runtime/debug" | ||
"strconv" | ||
"strings" | ||
"time" | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
"github.com/pmezard/go-difflib/difflib" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
|
@@ -145,8 +147,6 @@ func copyExportedFields(expected interface{}) interface{} { | |
// structures. | ||
// | ||
// This function does no assertion of any kind. | ||
// | ||
// Deprecated: Use [EqualExportedValues] instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you removing this? |
||
func ObjectsExportedFieldsAreEqual(expected, actual interface{}) bool { | ||
expectedCleaned := copyExportedFields(expected) | ||
actualCleaned := copyExportedFields(actual) | ||
|
@@ -1808,18 +1808,79 @@ func diff(expected interface{}, actual interface{}) string { | |
e = spewConfig.Sdump(expected) | ||
a = spewConfig.Sdump(actual) | ||
} | ||
structuredDiff := structuredDiff(e, a) | ||
prettyDiff := prettyDiff(structuredDiff) | ||
return "\n\nDiff:\n" + prettyDiff | ||
} | ||
|
||
func structuredDiff(e string, a string) []diffmatchpatch.Diff { | ||
dmp := diffmatchpatch.New() | ||
fromRunes, toRunes, runesToLines := dmp.DiffLinesToRunes(e, a) | ||
diffs := dmp.DiffMainRunes(fromRunes, toRunes, false) | ||
hydrated := make([]diffmatchpatch.Diff, 0, len(diffs)) | ||
for _, aDiff := range diffs { | ||
chars := strings.FieldsFunc(aDiff.Text, func(r rune) bool { | ||
return string(r) == diffmatchpatch.IndexSeparator | ||
}) | ||
text := make([]string, len(chars)) | ||
|
||
for i, char := range chars { | ||
i1, err := strconv.Atoi(char) | ||
if err == nil { | ||
text[i] = runesToLines[i1] | ||
} | ||
} | ||
for idx, line := range text { | ||
if aDiff.Type == diffmatchpatch.DiffEqual && idx < len(text)-1 { | ||
continue | ||
} | ||
hydrated = append(hydrated, diffmatchpatch.Diff{ | ||
Type: aDiff.Type, | ||
Text: line, | ||
}) | ||
} | ||
} | ||
return hydrated | ||
} | ||
|
||
func prettyDiff(diffs []diffmatchpatch.Diff) string { | ||
var diff strings.Builder | ||
|
||
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ | ||
A: difflib.SplitLines(e), | ||
B: difflib.SplitLines(a), | ||
FromFile: "Expected", | ||
FromDate: "", | ||
ToFile: "Actual", | ||
ToDate: "", | ||
Context: 1, | ||
}) | ||
diff.WriteString("--- Expected\n+++ Actual\n") | ||
|
||
return "\n\nDiff:\n" + diff | ||
for _, diffChunk := range diffs { | ||
switch diffChunk.Type { | ||
case diffmatchpatch.DiffInsert: | ||
// Make sure the different parts are on separate lines for better readability | ||
// i.e. it makes diffs like +got-expected to go as +got\n-expected\n | ||
if !strings.HasSuffix(diffChunk.Text, "\n") { | ||
diffChunk.Text = diffChunk.Text + "\n" | ||
} | ||
_, _ = fmt.Fprintf(&diff, "+%s", diffChunk.Text) | ||
case diffmatchpatch.DiffDelete: | ||
// Make sure the different parts are on separate lines for better readability | ||
// i.e. it makes diffs like +got-expected to go as +got\n-expected\n | ||
if !strings.HasSuffix(diffChunk.Text, "\n") { | ||
diffChunk.Text = diffChunk.Text + "\n" | ||
} | ||
_, _ = fmt.Fprintf(&diff, "-%s", diffChunk.Text) | ||
default: | ||
if len(diffChunk.Text) == 0 { | ||
continue | ||
} | ||
equalTextByLines := strings.SplitAfter(diffChunk.Text, "\n") | ||
var linesTrimmed []string | ||
for _, line := range equalTextByLines { | ||
if len(line) == 0 { | ||
continue | ||
} | ||
linesTrimmed = append(linesTrimmed, line) | ||
} | ||
// We're not interested in the equal parts, so only keep the last line for some context | ||
_, _ = fmt.Fprintf(&diff, " %s", equalTextByLines[len(linesTrimmed)-1]) | ||
} | ||
} | ||
return diff.String() | ||
} | ||
|
||
func isFunction(arg interface{}) bool { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,23 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= | ||
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0= | ||
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= | ||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this empty line.