Skip to content

feat: change output if all diff output is suppressed #768

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

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,15 @@ func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, erro
}
}

if containsDiff {
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType)
} else {
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, []difflib.DiffRecord{}, entry.changeType)
diffRecords := []difflib.DiffRecord{}
switch {
case containsDiff:
diffRecords = diffs
case entry.changeType == "MODIFY":
entry.changeType = "MODIFY_SUPPRESSED"
}

filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffRecords, entry.changeType)
}

return filteredReport, nil
Expand Down
21 changes: 20 additions & 1 deletion diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,30 @@ annotations:
var buf1 bytes.Buffer
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{"apiVersion"}}

if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
if changesSeen := Manifests(specBeta, specReleaseSpec, &diffOptions, &buf1); !changesSeen {
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`")
}

require.Equal(t, `default, nginx, Deployment (apps) has changed:

kind: Deployment
metadata:
name: nginx
+ spec:
+ replicas: 3

`, buf1.String())
})

t.Run("OnChangeWithSuppressAll", func(t *testing.T) {
var buf1 bytes.Buffer
diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{"apiVersion"}}

if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen {
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`")
}

require.Equal(t, `default, nginx, Deployment (apps) has changed, but diff is empty after suppression.
`, buf1.String())
})

Expand Down
20 changes: 15 additions & 5 deletions diff/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,18 @@ func setupDiffReport(r *Report) {
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "has been removed:"}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "has changed:"}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "changed ownership:"}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
}

// print report for default output: diff
func printDiffReport(r *Report, to io.Writer) {
for _, entry := range r.entries {
_, _ = fmt.Fprintf(to, ansi.Color("%s %s", "yellow")+"\n", entry.key, r.format.changestyles[entry.changeType].message)
_, _ = fmt.Fprintf(
to,
ansi.Color("%s %s", r.format.changestyles[entry.changeType].color)+"\n",
entry.key,
r.format.changestyles[entry.changeType].message,
)
printDiffRecords(entry.suppressedKinds, entry.kind, entry.context, entry.diffs, to)
}
}
Expand All @@ -162,15 +168,17 @@ func setupSimpleReport(r *Report) {
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: "to be removed."}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: "to be changed."}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: "to change ownership."}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: "has changed, but diff is empty after suppression."}
}

// print report for simple output
func printSimpleReport(r *Report, to io.Writer) {
var summary = map[string]int{
"ADD": 0,
"REMOVE": 0,
"MODIFY": 0,
"OWNERSHIP": 0,
"ADD": 0,
"REMOVE": 0,
"MODIFY": 0,
"OWNERSHIP": 0,
"MODIFY_SUPPRESSED": 0,
}
for _, entry := range r.entries {
_, _ = fmt.Fprintf(to, ansi.Color("%s %s", r.format.changestyles[entry.changeType].color)+"\n",
Expand Down Expand Up @@ -206,6 +214,7 @@ func setupJSONReport(r *Report) {
r.format.changestyles["REMOVE"] = ChangeStyle{color: "red", message: ""}
r.format.changestyles["MODIFY"] = ChangeStyle{color: "yellow", message: ""}
r.format.changestyles["OWNERSHIP"] = ChangeStyle{color: "magenta", message: ""}
r.format.changestyles["MODIFY_SUPPRESSED"] = ChangeStyle{color: "blue+h", message: ""}
}

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

// report with template output will only have access to ReportTemplateSpec.
Expand Down
Loading