|
1 | 1 | package diff |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/aryann/difflib" |
6 | 8 | "github.com/stretchr/testify/require" |
7 | 9 | ) |
8 | 10 |
|
@@ -34,3 +36,142 @@ func TestLoadFromKey(t *testing.T) { |
34 | 36 | require.Equal(t, expectedTemplateSpec, *templateSpec) |
35 | 37 | } |
36 | 38 | } |
| 39 | + |
| 40 | +func TestPrintDyffReport(t *testing.T) { |
| 41 | + report := &Report{ |
| 42 | + Entries: []ReportEntry{ |
| 43 | + { |
| 44 | + Key: "default, nginx, Deployment (apps)", |
| 45 | + Kind: "Deployment", |
| 46 | + ChangeType: "MODIFY", |
| 47 | + Diffs: []difflib.DiffRecord{ |
| 48 | + {Payload: "apiVersion: apps/v1", Delta: difflib.Common}, |
| 49 | + {Payload: "kind: Deployment", Delta: difflib.Common}, |
| 50 | + {Payload: "metadata:", Delta: difflib.Common}, |
| 51 | + {Payload: " name: nginx", Delta: difflib.Common}, |
| 52 | + {Payload: "spec:", Delta: difflib.Common}, |
| 53 | + {Payload: " replicas: 2", Delta: difflib.LeftOnly}, |
| 54 | + {Payload: " replicas: 3", Delta: difflib.RightOnly}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + var buf bytes.Buffer |
| 61 | + printDyffReport(report, &buf) |
| 62 | + |
| 63 | + output := buf.String() |
| 64 | + require.NotEmpty(t, output) |
| 65 | + require.Contains(t, output, "replicas", "Expected dyff output to mention replicas field") |
| 66 | + require.Contains(t, output, "- 2", "Expected dyff output to show original replicas value") |
| 67 | + require.Contains(t, output, "+ 3", "Expected dyff output to show updated replicas value") |
| 68 | +} |
| 69 | + |
| 70 | +func TestPrintDyffReportWithAddAndRemove(t *testing.T) { |
| 71 | + report := &Report{ |
| 72 | + Entries: []ReportEntry{ |
| 73 | + { |
| 74 | + Key: "default, old-app, Deployment (apps)", |
| 75 | + Kind: "Deployment", |
| 76 | + ChangeType: "REMOVE", |
| 77 | + Diffs: []difflib.DiffRecord{ |
| 78 | + {Payload: "apiVersion: apps/v1", Delta: difflib.LeftOnly}, |
| 79 | + {Payload: "kind: Deployment", Delta: difflib.LeftOnly}, |
| 80 | + {Payload: "metadata:", Delta: difflib.LeftOnly}, |
| 81 | + {Payload: " name: old-app", Delta: difflib.LeftOnly}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + Key: "default, new-app, Deployment (apps)", |
| 86 | + Kind: "Deployment", |
| 87 | + ChangeType: "ADD", |
| 88 | + Diffs: []difflib.DiffRecord{ |
| 89 | + {Payload: "apiVersion: apps/v1", Delta: difflib.RightOnly}, |
| 90 | + {Payload: "kind: Deployment", Delta: difflib.RightOnly}, |
| 91 | + {Payload: "metadata:", Delta: difflib.RightOnly}, |
| 92 | + {Payload: " name: new-app", Delta: difflib.RightOnly}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + var buf bytes.Buffer |
| 99 | + printDyffReport(report, &buf) |
| 100 | + |
| 101 | + output := buf.String() |
| 102 | + require.NotEmpty(t, output) |
| 103 | + require.Contains(t, output, "old-app", "Expected dyff output to show removed resource old-app") |
| 104 | + require.Contains(t, output, "new-app", "Expected dyff output to show added resource new-app") |
| 105 | +} |
| 106 | + |
| 107 | +func TestPrintDyffReportDoesNotMergeAddRemove(t *testing.T) { |
| 108 | + addRemoveReport := &Report{ |
| 109 | + Entries: []ReportEntry{ |
| 110 | + { |
| 111 | + Key: "default, old-app, Deployment (apps)", |
| 112 | + Kind: "Deployment", |
| 113 | + ChangeType: "REMOVE", |
| 114 | + Diffs: []difflib.DiffRecord{ |
| 115 | + {Payload: "apiVersion: apps/v1", Delta: difflib.LeftOnly}, |
| 116 | + {Payload: "kind: Deployment", Delta: difflib.LeftOnly}, |
| 117 | + {Payload: "metadata:", Delta: difflib.LeftOnly}, |
| 118 | + {Payload: " name: old-app", Delta: difflib.LeftOnly}, |
| 119 | + }, |
| 120 | + }, |
| 121 | + { |
| 122 | + Key: "default, new-app, Deployment (apps)", |
| 123 | + Kind: "Deployment", |
| 124 | + ChangeType: "ADD", |
| 125 | + Diffs: []difflib.DiffRecord{ |
| 126 | + {Payload: "apiVersion: apps/v1", Delta: difflib.RightOnly}, |
| 127 | + {Payload: "kind: Deployment", Delta: difflib.RightOnly}, |
| 128 | + {Payload: "metadata:", Delta: difflib.RightOnly}, |
| 129 | + {Payload: " name: new-app", Delta: difflib.RightOnly}, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + modifyReport := &Report{ |
| 136 | + Entries: []ReportEntry{ |
| 137 | + { |
| 138 | + Key: "default, app, Deployment (apps)", |
| 139 | + Kind: "Deployment", |
| 140 | + ChangeType: "MODIFY", |
| 141 | + Diffs: []difflib.DiffRecord{ |
| 142 | + {Payload: "apiVersion: apps/v1", Delta: difflib.Common}, |
| 143 | + {Payload: "kind: Deployment", Delta: difflib.Common}, |
| 144 | + {Payload: "metadata:", Delta: difflib.Common}, |
| 145 | + {Payload: " name: app", Delta: difflib.Common}, |
| 146 | + {Payload: " name: old-app", Delta: difflib.LeftOnly}, |
| 147 | + {Payload: " name: new-app", Delta: difflib.RightOnly}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + var addRemoveBuf bytes.Buffer |
| 154 | + printDyffReport(addRemoveReport, &addRemoveBuf) |
| 155 | + addRemoveOutput := addRemoveBuf.String() |
| 156 | + |
| 157 | + var modifyBuf bytes.Buffer |
| 158 | + printDyffReport(modifyReport, &modifyBuf) |
| 159 | + modifyOutput := modifyBuf.String() |
| 160 | + |
| 161 | + require.NotEqual(t, addRemoveOutput, modifyOutput, |
| 162 | + "ADD+REMOVE output should differ from MODIFY output to verify dyff does not merge them as a rename") |
| 163 | + require.Contains(t, addRemoveOutput, "old-app") |
| 164 | + require.Contains(t, addRemoveOutput, "new-app") |
| 165 | +} |
| 166 | + |
| 167 | +func TestPrintDyffReportEmpty(t *testing.T) { |
| 168 | + report := &Report{ |
| 169 | + Entries: []ReportEntry{}, |
| 170 | + } |
| 171 | + |
| 172 | + var buf bytes.Buffer |
| 173 | + printDyffReport(report, &buf) |
| 174 | + |
| 175 | + output := buf.String() |
| 176 | + require.Equal(t, "\n", output) |
| 177 | +} |
0 commit comments