Skip to content

Commit b0d2e61

Browse files
authored
feat: change output if all diff output is suppressed (#768)
1 parent 5ebd83c commit b0d2e61

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

diff/diff.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,15 @@ func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, erro
138138
}
139139
}
140140

141-
if containsDiff {
142-
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType)
143-
} else {
144-
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, []difflib.DiffRecord{}, entry.changeType)
141+
diffRecords := []difflib.DiffRecord{}
142+
switch {
143+
case containsDiff:
144+
diffRecords = diffs
145+
case entry.changeType == "MODIFY":
146+
entry.changeType = "MODIFY_SUPPRESSED"
145147
}
148+
149+
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffRecords, entry.changeType)
146150
}
147151

148152
return filteredReport, nil

diff/diff_test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,30 @@ annotations:
298298
var buf1 bytes.Buffer
299299
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{"apiVersion"}}
300300

301-
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
301+
if changesSeen := Manifests(specBeta, specReleaseSpec, &diffOptions, &buf1); !changesSeen {
302302
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
303303
}
304304

305305
require.Equal(t, `default, nginx, Deployment (apps) has changed:
306+
307+
kind: Deployment
308+
metadata:
309+
name: nginx
310+
+ spec:
311+
+ replicas: 3
312+
313+
`, buf1.String())
314+
})
315+
316+
t.Run("OnChangeWithSuppressAll", func(t *testing.T) {
317+
var buf1 bytes.Buffer
318+
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{"apiVersion"}}
319+
320+
if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
321+
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
322+
}
323+
324+
require.Equal(t, `default, nginx, Deployment (apps) has changed, but diff is empty after suppression.
306325
`, buf1.String())
307326
})
308327

diff/report.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,18 @@ func setupDiffReport(r *Report) {
144144
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "has been removed:"}
145145
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "has changed:"}
146146
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "changed ownership:"}
147+
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
147148
}
148149

149150
// print report for default output: diff
150151
func printDiffReport(r *Report, to io.Writer) {
151152
for _, entry := range r.entries {
152-
_, _ = fmt.Fprintf(to, ansi.Color("%s %s", "yellow")+"\n", entry.key, r.format.changestyles[entry.changeType].message)
153+
_, _ = fmt.Fprintf(
154+
to,
155+
ansi.Color("%s %s", r.format.changestyles[entry.changeType].color)+"\n",
156+
entry.key,
157+
r.format.changestyles[entry.changeType].message,
158+
)
153159
printDiffRecords(entry.suppressedKinds, entry.kind, entry.context, entry.diffs, to)
154160
}
155161
}
@@ -162,15 +168,17 @@ func setupSimpleReport(r *Report) {
162168
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "to be removed."}
163169
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "to be changed."}
164170
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "to change ownership."}
171+
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
165172
}
166173

167174
// print report for simple output
168175
func printSimpleReport(r *Report, to io.Writer) {
169176
var summary = map[string]int{
170-
"ADD": 0,
171-
"REMOVE": 0,
172-
"MODIFY": 0,
173-
"OWNERSHIP": 0,
177+
"ADD": 0,
178+
"REMOVE": 0,
179+
"MODIFY": 0,
180+
"OWNERSHIP": 0,
181+
"MODIFY_SUPPRESSED": 0,
174182
}
175183
for _, entry := range r.entries {
176184
_, _ = fmt.Fprintf(to, ansi.Color("%s %s", r.format.changestyles[entry.changeType].color)+"\n",
@@ -206,6 +214,7 @@ func setupJSONReport(r *Report) {
206214
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: ""}
207215
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: ""}
208216
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: ""}
217+
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: ""}
209218
}
210219

211220
// setup report for template output
@@ -237,6 +246,7 @@ func setupTemplateReport(r *Report) {
237246
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: ""}
238247
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: ""}
239248
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: ""}
249+
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: ""}
240250
}
241251

242252
// report with template output will only have access to ReportTemplateSpec.

0 commit comments

Comments
 (0)