From c6c150245c8126deb7d37cda822bd784bde26055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Wed, 28 Jun 2023 23:27:39 +0200 Subject: [PATCH 1/8] Suppress diff output by regex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- cmd/options.go | 1 + diff/diff.go | 63 +++++++++++++++++++++++++++++++++++++++++------ diff/diff_test.go | 28 ++++++++++----------- 3 files changed, 71 insertions(+), 21 deletions(-) diff --git a/cmd/options.go b/cmd/options.go index 50846887..3ff87dba 100644 --- a/cmd/options.go +++ b/cmd/options.go @@ -15,6 +15,7 @@ func AddDiffOptions(f *pflag.FlagSet, o *diff.Options) { f.StringVar(&o.OutputFormat, "output", "diff", "Possible values: diff, simple, template, dyff. When set to \"template\", use the env var HELM_DIFF_TPL to specify the template.") f.BoolVar(&o.StripTrailingCR, "strip-trailing-cr", false, "strip trailing carriage return on input") f.Float32VarP(&o.FindRenames, "find-renames", "D", 0, "Enable rename detection if set to any value greater than 0. If specified, the value denotes the maximum fraction of changed content as lines added + removed compared to total lines in a diff for considering it a rename. Only objects of the same Kind are attempted to be matched") + f.StringArrayVar(&o.SuppressedOutputLineRegex, "suppress-output-line-regex", []string{}, "a regex to suppress diff output lines that match") } // ProcessDiffOptions processes the set flags and handles possible interactions between them diff --git a/diff/diff.go b/diff/diff.go index 28d861dd..a808612a 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "math" + "regexp" "sort" "strings" @@ -20,12 +21,13 @@ import ( // Options are all the options to be passed to generate a diff type Options struct { - OutputFormat string - OutputContext int - StripTrailingCR bool - ShowSecrets bool - SuppressedKinds []string - FindRenames float32 + OutputFormat string + OutputContext int + StripTrailingCR bool + ShowSecrets bool + SuppressedKinds []string + FindRenames float32 + SuppressedOutputLineRegex []string } // Manifests diff on manifests @@ -64,8 +66,55 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O doDiff(&report, key, nil, newContent, options) } + filteredReport := Report{} + if len(options.SuppressedOutputLineRegex) > 0 { + filteredReport.format = report.format + filteredReport.entries = []ReportEntry{} + + var suppressOutputRegexes []*regexp.Regexp + + for _, suppressOutputRegex := range options.SuppressedOutputLineRegex { + regexp, err := regexp.Compile(suppressOutputRegex) + if err != nil { + panic(err) + } + + suppressOutputRegexes = append(suppressOutputRegexes, regexp) + } + + for _, entry := range report.entries { + var diffs []difflib.DiffRecord + + for _, diff := range entry.diffs { + matched := false + + for _, suppressOutputRegex := range suppressOutputRegexes { + if suppressOutputRegex.MatchString(diff.Payload) { + matched = true + break + } + } + + if !matched { + diffs = append(diffs, diff) + } + } + + // Add entry to the report, if diffs are present. + for _, diff := range diffs { + if diff.Delta.String() != " " { + filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType) + break + } + } + } + } else { + filteredReport = report + } + seenAnyChanges := len(report.entries) > 0 - report.print(to) + filteredReport.print(to) + filteredReport.clean() report.clean() return seenAnyChanges } diff --git a/diff/diff_test.go b/diff/diff_test.go index 0474e9aa..5f27f0bc 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -260,7 +260,7 @@ spec: t.Run("OnChange", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.0} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}} 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`") @@ -279,7 +279,7 @@ spec: t.Run("OnChangeRename", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.5} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}} if changesSeen := Manifests(specReleaseSpec, specReleaseRenamed, &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`") @@ -300,7 +300,7 @@ spec: t.Run("OnChangeRenameAndUpdate", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.5} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}} if changesSeen := Manifests(specReleaseSpec, specReleaseRenamedAndUpdated, &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`") @@ -322,7 +322,7 @@ spec: t.Run("OnChangeRenameAndAdded", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.5} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}} if changesSeen := Manifests(specReleaseSpec, specReleaseRenamedAndAdded, &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`") @@ -346,7 +346,7 @@ spec: t.Run("OnChangeRenameAndRemoved", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.5} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}} if changesSeen := Manifests(specReleaseRenamedAndAdded, 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`") @@ -370,7 +370,7 @@ spec: t.Run("OnNoChange", func(t *testing.T) { var buf2 bytes.Buffer - diffOptions := Options{"diff", 10, false, true, []string{}, 0.0} + diffOptions := Options{"diff", 10, false, true, []string{}, 0.0, []string{}} if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen { t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`") @@ -381,7 +381,7 @@ spec: t.Run("OnChangeSimple", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"simple", 10, false, true, []string{}, 0.0} + diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}} 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`") @@ -394,7 +394,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. t.Run("OnNoChangeSimple", func(t *testing.T) { var buf2 bytes.Buffer - diffOptions := Options{"simple", 10, false, true, []string{}, 0.0} + diffOptions := Options{"simple", 10, false, true, []string{}, 0.0, []string{}} if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen { t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`") } @@ -404,7 +404,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. t.Run("OnChangeTemplate", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"template", 10, false, true, []string{}, 0.0} + diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}} 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`") @@ -422,7 +422,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. t.Run("OnChangeJSON", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"json", 10, false, true, []string{}, 0.0} + diffOptions := Options{"json", 10, false, true, []string{}, 0.0, []string{}} 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`") @@ -440,7 +440,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. t.Run("OnNoChangeTemplate", func(t *testing.T) { var buf2 bytes.Buffer - diffOptions := Options{"template", 10, false, true, []string{}, 0.0} + diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}} if changesSeen := Manifests(specRelease, specRelease, &diffOptions, &buf2); changesSeen { t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`") @@ -452,7 +452,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. t.Run("OnChangeCustomTemplate", func(t *testing.T) { var buf1 bytes.Buffer os.Setenv("HELM_DIFF_TPL", "testdata/customTemplate.tpl") - diffOptions := Options{"template", 10, false, true, []string{}, 0.0} + diffOptions := Options{"template", 10, false, true, []string{}, 0.0, []string{}} if changesSeen := Manifests(specBeta, specRelease, &diffOptions, &buf1); !changesSeen { t.Error("Unexpected return value from Manifests: Expected the return value to be `false` to indicate that it has NOT seen any change(s), but was `true`") @@ -535,7 +535,7 @@ stringData: t.Run("OnChangeSecretWithByteData", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, false, []string{}, 0.5} //NOTE: ShowSecrets = false + diffOptions := Options{"diff", 10, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false if changesSeen := Manifests(specSecretWithByteData, specSecretWithByteDataChanged, &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`") @@ -560,7 +560,7 @@ stringData: t.Run("OnChangeSecretWithStringData", func(t *testing.T) { var buf1 bytes.Buffer - diffOptions := Options{"diff", 10, false, false, []string{}, 0.5} //NOTE: ShowSecrets = false + diffOptions := Options{"diff", 10, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false if changesSeen := Manifests(specSecretWithStringData, specSecretWithStringDataChanged, &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`") From 445d42cbc89b9e815bb532a21d64ffbafcff72e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Wed, 28 Jun 2023 23:45:39 +0200 Subject: [PATCH 2/8] Added unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- diff/diff_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/diff/diff_test.go b/diff/diff_test.go index 5f27f0bc..efa0e41f 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -277,6 +277,17 @@ spec: `, buf1.String()) }) + t.Run("OnChangeWithSuppress", 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, ``, buf1.String()) + }) + t.Run("OnChangeRename", func(t *testing.T) { var buf1 bytes.Buffer diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{}} @@ -341,6 +352,29 @@ spec: + matchLabels: + app: nginx-renamed +`, buf1.String()) + }) + + t.Run("OnChangeRenameAndAddedWithPartialSuppress", func(t *testing.T) { + var buf1 bytes.Buffer + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{"app: "}} + + if changesSeen := Manifests(specReleaseSpec, specReleaseRenamedAndAdded, &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: + + apiVersion: apps/v1 + kind: Deployment + metadata: +- name: nginx ++ name: nginx-renamed + spec: + replicas: 3 ++ selector: ++ matchLabels: + `, buf1.String()) }) @@ -365,6 +399,29 @@ spec: - matchLabels: - app: nginx-renamed +`, buf1.String()) + }) + + t.Run("OnChangeRenameAndRemovedWithPartialSuppress", func(t *testing.T) { + var buf1 bytes.Buffer + diffOptions := Options{"diff", 10, false, true, []string{}, 0.5, []string{"app: "}} + + if changesSeen := Manifests(specReleaseRenamedAndAdded, 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-renamed, Deployment (apps) has changed: + + apiVersion: apps/v1 + kind: Deployment + metadata: +- name: nginx-renamed ++ name: nginx + spec: + replicas: 3 +- selector: +- matchLabels: + `, buf1.String()) }) From 9cabf06dec5a057c1a797568a5d793c63226450a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Wed, 28 Jun 2023 23:55:45 +0200 Subject: [PATCH 3/8] Keep changed filename in output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- diff/diff.go | 10 +++++++++- diff/diff_test.go | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index a808612a..d726a800 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -100,13 +100,21 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O } } + containsDiff := false + // Add entry to the report, if diffs are present. for _, diff := range diffs { if diff.Delta.String() != " " { - filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType) + containsDiff = true break } } + + 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) + } } } else { filteredReport = report diff --git a/diff/diff_test.go b/diff/diff_test.go index efa0e41f..985c2e36 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -285,7 +285,8 @@ spec: 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, ``, buf1.String()) + require.Equal(t, `default, nginx, Deployment (apps) has changed: +`, buf1.String()) }) t.Run("OnChangeRename", func(t *testing.T) { From cfbd3604b62344ff5baed2990cd101ef716ea353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 25 Jul 2023 20:00:12 +0200 Subject: [PATCH 4/8] Update diff/diff.go Co-authored-by: Yusuke Kuoka --- diff/diff.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index d726a800..53d49ed1 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -85,19 +85,15 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O for _, entry := range report.entries { var diffs []difflib.DiffRecord + DIFFS: for _, diff := range entry.diffs { - matched := false - for _, suppressOutputRegex := range suppressOutputRegexes { if suppressOutputRegex.MatchString(diff.Payload) { - matched = true - break + continue DIFFS } } - if !matched { - diffs = append(diffs, diff) - } + diffs = append(diffs, diff) } containsDiff := false From aa009a761a99b855aa648de5dda130d63550434a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 25 Jul 2023 20:26:57 +0200 Subject: [PATCH 5/8] extract method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- diff/diff.go | 89 +++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index 53d49ed1..b9f796e1 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -66,61 +66,70 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O doDiff(&report, key, nil, newContent, options) } - filteredReport := Report{} - if len(options.SuppressedOutputLineRegex) > 0 { - filteredReport.format = report.format - filteredReport.entries = []ReportEntry{} + filteredReport, err := doSuppress(report, options.SuppressedOutputLineRegex) + if err != nil { + panic(err) + } - var suppressOutputRegexes []*regexp.Regexp + seenAnyChanges := len(report.entries) > 0 + filteredReport.print(to) + filteredReport.clean() + report.clean() + return seenAnyChanges +} - for _, suppressOutputRegex := range options.SuppressedOutputLineRegex { - regexp, err := regexp.Compile(suppressOutputRegex) - if err != nil { - panic(err) - } +func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, error) { + if len(suppressedOutputLineRegex) == 0 { + return report, nil + } - suppressOutputRegexes = append(suppressOutputRegexes, regexp) - } + filteredReport := Report{} + filteredReport.format = report.format + filteredReport.entries = []ReportEntry{} - for _, entry := range report.entries { - var diffs []difflib.DiffRecord + var suppressOutputRegexes []*regexp.Regexp - DIFFS: - for _, diff := range entry.diffs { - for _, suppressOutputRegex := range suppressOutputRegexes { - if suppressOutputRegex.MatchString(diff.Payload) { - continue DIFFS - } - } + for _, suppressOutputRegex := range suppressedOutputLineRegex { + regex, err := regexp.Compile(suppressOutputRegex) + if err != nil { + return Report{}, err + } - diffs = append(diffs, diff) - } + suppressOutputRegexes = append(suppressOutputRegexes, regex) + } - containsDiff := false + for _, entry := range report.entries { + var diffs []difflib.DiffRecord - // Add entry to the report, if diffs are present. - for _, diff := range diffs { - if diff.Delta.String() != " " { - containsDiff = true - break + DIFFS: + for _, diff := range entry.diffs { + for _, suppressOutputRegex := range suppressOutputRegexes { + if suppressOutputRegex.MatchString(diff.Payload) { + continue DIFFS } } - 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) + diffs = append(diffs, diff) + } + + containsDiff := false + + // Add entry to the report, if diffs are present. + for _, diff := range diffs { + if diff.Delta.String() != " " { + containsDiff = true + break } } - } else { - filteredReport = report + + 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) + } } - seenAnyChanges := len(report.entries) > 0 - filteredReport.print(to) - filteredReport.clean() - report.clean() - return seenAnyChanges + return filteredReport, nil } func actualChanges(diff []difflib.DiffRecord) int { From 6952cae6c3a02378748d5adb71da69dbf2ca3eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 25 Jul 2023 20:30:40 +0200 Subject: [PATCH 6/8] hide filteredReport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- diff/diff.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index b9f796e1..c10326a9 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -66,14 +66,14 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O doDiff(&report, key, nil, newContent, options) } - filteredReport, err := doSuppress(report, options.SuppressedOutputLineRegex) + seenAnyChanges := len(report.entries) > 0 + + report, err := doSuppress(report, options.SuppressedOutputLineRegex) if err != nil { panic(err) } - seenAnyChanges := len(report.entries) > 0 - filteredReport.print(to) - filteredReport.clean() + report.print(to) report.clean() return seenAnyChanges } From 3a0aa1e7371b0c915a5ed581679b7bedb6a71077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 25 Jul 2023 20:31:47 +0200 Subject: [PATCH 7/8] skip doSuppress, if report is empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- diff/diff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff/diff.go b/diff/diff.go index c10326a9..b8ebea28 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -79,7 +79,7 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O } func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, error) { - if len(suppressedOutputLineRegex) == 0 { + if len(report.entries) == 0 || len(suppressedOutputLineRegex) == 0 { return report, nil } From d9c642ab48bd4a6657d60ae8fa465a76422c2d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 25 Jul 2023 23:52:30 +0200 Subject: [PATCH 8/8] Add unit tests for DoSuppress --- diff/diff_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/diff/diff_test.go b/diff/diff_test.go index 985c2e36..28711b74 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -5,6 +5,7 @@ import ( "os" "testing" + "github.com/aryann/difflib" "github.com/mgutz/ansi" "github.com/stretchr/testify/require" @@ -640,3 +641,85 @@ stringData: `, buf1.String()) }) } + +func TestDoSuppress(t *testing.T) { + for _, tt := range []struct { + name string + input Report + supressRegex []string + expected Report + }{ + { + name: "noop", + input: Report{}, + supressRegex: []string{}, + expected: Report{}, + }, + { + name: "simple", + input: Report{ + entries: []ReportEntry{ + { + diffs: diffStrings("hello: world", "hello: world2", false), + }, + }, + }, + supressRegex: []string{}, + expected: Report{ + entries: []ReportEntry{ + { + diffs: diffStrings("hello: world", "hello: world2", false), + }, + }, + }, + }, + { + name: "ignore all", + input: Report{ + entries: []ReportEntry{ + { + diffs: diffStrings("hello: world", "hello: world2", false), + }, + }, + }, + supressRegex: []string{".*world2?"}, + expected: Report{ + entries: []ReportEntry{ + { + diffs: []difflib.DiffRecord{}, + }, + }, + }, + }, + { + name: "ignore partial", + input: Report{ + entries: []ReportEntry{ + { + diffs: diffStrings("hello: world", "hello: world2", false), + }, + }, + }, + supressRegex: []string{".*world2"}, + expected: Report{ + entries: []ReportEntry{ + { + diffs: []difflib.DiffRecord{ + { + Payload: "hello: world", + Delta: difflib.LeftOnly, + }, + }, + }, + }, + }, + }, + } { + t.Run(tt.name, func(t *testing.T) { + report, err := doSuppress(tt.input, tt.supressRegex) + require.NoError(t, err) + + require.Equal(t, tt.expected, report) + }) + } +}