-
Couldn't load subscription status.
- Fork 128
internal/testrunner/runners/pipeline: replace jsondiff output with difflib #872
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,9 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/nsf/jsondiff" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/pkg/errors" | ||
| "github.com/pmezard/go-difflib/difflib" | ||
|
|
||
| "github.com/elastic/elastic-package/internal/common" | ||
| "github.com/elastic/elastic-package/internal/testrunner" | ||
|
|
@@ -103,24 +104,37 @@ func compareJsonNumbers(a, b json.Number) bool { | |
| } | ||
|
|
||
| func diffJson(want, got []byte) (string, error) { | ||
| opts := jsondiff.DefaultConsoleOptions() | ||
|
|
||
| // Remove colored output. | ||
| opts.Added = jsondiff.Tag{Begin: "+ "} | ||
| opts.Removed = jsondiff.Tag{Begin: "- "} | ||
| opts.Changed = jsondiff.Tag{} | ||
| opts.Skipped = jsondiff.Tag{} | ||
|
|
||
| // Configure diff. | ||
| opts.SkipMatches = true | ||
| opts.CompareNumbers = compareJsonNumbers | ||
| var gotVal, wantVal interface{} | ||
| err := jsonUnmarshalUsingNumber(want, &wantVal) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid want value: %w", err) | ||
| } | ||
| err = jsonUnmarshalUsingNumber(got, &gotVal) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid got value: %w", err) | ||
| } | ||
| if cmp.Equal(gotVal, wantVal, cmp.Comparer(compareJsonNumbers)) { | ||
| return "", nil | ||
| } | ||
|
|
||
| result, diff := jsondiff.Compare(want, got, &opts) | ||
|
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. We could keep this comparison method even if later we change the diff that we print, right? 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. Yes, that's right. |
||
| switch result { | ||
| case jsondiff.FirstArgIsInvalidJson, jsondiff.SecondArgIsInvalidJson, jsondiff.BothArgsAreInvalidJson: | ||
| return "", errors.New("invalid json") | ||
| got, err = marshalNormalizedJSON(gotVal) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return diff, nil | ||
| want, err = marshalNormalizedJSON(wantVal) | ||
|
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.
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. I was being conservative to ensure that the values were being treated the same way and to ensure that the reader was clear that this was happening. |
||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| err = difflib.WriteUnifiedDiff(&buf, difflib.UnifiedDiff{ | ||
| A: difflib.SplitLines(string(want)), | ||
| B: difflib.SplitLines(string(got)), | ||
| FromFile: "want", | ||
| ToFile: "got", | ||
| Context: 3, | ||
| }) | ||
| return buf.String(), err | ||
| } | ||
|
|
||
| func readExpectedTestResult(testCasePath string, config *testConfig) (*testResult, error) { | ||
|
|
@@ -228,7 +242,7 @@ func marshalTestResultDefinition(result *testResult) ([]byte, error) { | |
| // marshalNormalizedJSON marshals test results ensuring that field | ||
| // order remains consistent independent of field order returned by | ||
| // ES to minimize diff noise during changes. | ||
| func marshalNormalizedJSON(v testResultDefinition) ([]byte, error) { | ||
| func marshalNormalizedJSON(v interface{}) ([]byte, error) { | ||
| msg, err := json.Marshal(v) | ||
| if err != nil { | ||
| return msg, err | ||
|
|
||
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.
compareJsonNumbersand its test can be removed if we stop usingjsondiff.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.
I was originally thinking that, but the logic behind using it made a lot of sense, to I used it as the
Comparerforcmp.Equal.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.
Ah, you are right, I didn't see that it was still used there 👍