-
Notifications
You must be signed in to change notification settings - Fork 322
fix: make --find-renames work with --output=dyff #961
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 |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/aryann/difflib" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
|
|
@@ -34,3 +36,142 @@ func TestLoadFromKey(t *testing.T) { | |
| require.Equal(t, expectedTemplateSpec, *templateSpec) | ||
| } | ||
| } | ||
|
|
||
| func TestPrintDyffReport(t *testing.T) { | ||
| report := &Report{ | ||
| Entries: []ReportEntry{ | ||
| { | ||
| Key: "default, nginx, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "MODIFY", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.Common}, | ||
| {Payload: "kind: Deployment", Delta: difflib.Common}, | ||
| {Payload: "metadata:", Delta: difflib.Common}, | ||
| {Payload: " name: nginx", Delta: difflib.Common}, | ||
| {Payload: "spec:", Delta: difflib.Common}, | ||
| {Payload: " replicas: 2", Delta: difflib.LeftOnly}, | ||
| {Payload: " replicas: 3", Delta: difflib.RightOnly}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| printDyffReport(report, &buf) | ||
|
|
||
| output := buf.String() | ||
| require.NotEmpty(t, output) | ||
| require.Contains(t, output, "replicas", "Expected dyff output to mention replicas field") | ||
| require.Contains(t, output, "- 2", "Expected dyff output to show original replicas value") | ||
| require.Contains(t, output, "+ 3", "Expected dyff output to show updated replicas value") | ||
| } | ||
|
|
||
| func TestPrintDyffReportWithAddAndRemove(t *testing.T) { | ||
| report := &Report{ | ||
| Entries: []ReportEntry{ | ||
| { | ||
| Key: "default, old-app, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "REMOVE", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.LeftOnly}, | ||
| {Payload: "kind: Deployment", Delta: difflib.LeftOnly}, | ||
| {Payload: "metadata:", Delta: difflib.LeftOnly}, | ||
| {Payload: " name: old-app", Delta: difflib.LeftOnly}, | ||
| }, | ||
| }, | ||
| { | ||
| Key: "default, new-app, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "ADD", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.RightOnly}, | ||
| {Payload: "kind: Deployment", Delta: difflib.RightOnly}, | ||
| {Payload: "metadata:", Delta: difflib.RightOnly}, | ||
| {Payload: " name: new-app", Delta: difflib.RightOnly}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| printDyffReport(report, &buf) | ||
|
|
||
| output := buf.String() | ||
| require.NotEmpty(t, output) | ||
| require.Contains(t, output, "old-app", "Expected dyff output to show removed resource old-app") | ||
| require.Contains(t, output, "new-app", "Expected dyff output to show added resource new-app") | ||
| } | ||
|
|
||
| func TestPrintDyffReportDoesNotMergeAddRemove(t *testing.T) { | ||
| addRemoveReport := &Report{ | ||
| Entries: []ReportEntry{ | ||
| { | ||
| Key: "default, old-app, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "REMOVE", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.LeftOnly}, | ||
| {Payload: "kind: Deployment", Delta: difflib.LeftOnly}, | ||
| {Payload: "metadata:", Delta: difflib.LeftOnly}, | ||
| {Payload: " name: old-app", Delta: difflib.LeftOnly}, | ||
| }, | ||
| }, | ||
| { | ||
| Key: "default, new-app, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "ADD", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.RightOnly}, | ||
| {Payload: "kind: Deployment", Delta: difflib.RightOnly}, | ||
| {Payload: "metadata:", Delta: difflib.RightOnly}, | ||
| {Payload: " name: new-app", Delta: difflib.RightOnly}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| modifyReport := &Report{ | ||
| Entries: []ReportEntry{ | ||
| { | ||
| Key: "default, app, Deployment (apps)", | ||
| Kind: "Deployment", | ||
| ChangeType: "MODIFY", | ||
| Diffs: []difflib.DiffRecord{ | ||
| {Payload: "apiVersion: apps/v1", Delta: difflib.Common}, | ||
| {Payload: "kind: Deployment", Delta: difflib.Common}, | ||
| {Payload: "metadata:", Delta: difflib.Common}, | ||
| {Payload: " name: app", Delta: difflib.Common}, | ||
| {Payload: " name: old-app", Delta: difflib.LeftOnly}, | ||
| {Payload: " name: new-app", Delta: difflib.RightOnly}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| var addRemoveBuf bytes.Buffer | ||
| printDyffReport(addRemoveReport, &addRemoveBuf) | ||
| addRemoveOutput := addRemoveBuf.String() | ||
|
|
||
| var modifyBuf bytes.Buffer | ||
| printDyffReport(modifyReport, &modifyBuf) | ||
| modifyOutput := modifyBuf.String() | ||
|
|
||
| require.NotEqual(t, addRemoveOutput, modifyOutput, | ||
| "ADD+REMOVE output should differ from MODIFY output to verify dyff does not merge them as a rename") | ||
| require.Contains(t, addRemoveOutput, "old-app") | ||
| require.Contains(t, addRemoveOutput, "new-app") | ||
| } | ||
|
Comment on lines
+70
to
+165
|
||
|
|
||
| func TestPrintDyffReportEmpty(t *testing.T) { | ||
| report := &Report{ | ||
| Entries: []ReportEntry{}, | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| printDyffReport(report, &buf) | ||
|
|
||
| output := buf.String() | ||
| require.Equal(t, "\n", output) | ||
| } | ||
|
Comment on lines
+167
to
+177
|
||
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.
The return error from ytbx.LoadFiles is currently ignored. If loading/parsing either temp file fails, dyff comparison may silently produce empty/incorrect output. Consider checking the error and writing a clear message (or falling back to the default diff output) instead of proceeding with a potentially invalid input state.