Skip to content
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

Introduce --ignore-whitespace-changes flag #385

Merged
merged 1 commit into from
Aug 2, 2024
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
3 changes: 3 additions & 0 deletions assets/issues/issue-222/from.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
foo: |
bar
3 changes: 3 additions & 0 deletions assets/issues/issue-222/to.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
foo: |+
bar
1 change: 1 addition & 0 deletions internal/cmd/between.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ types are: YAML (http://yaml.org/) and JSON (http://json.org/).

report, err := dyff.CompareInputFiles(from, to,
dyff.IgnoreOrderChanges(reportOptions.ignoreOrderChanges),
dyff.IgnoreWhitespaceChanges(reportOptions.ignoreWhitespaceChanges),
dyff.KubernetesEntityDetection(reportOptions.kubernetesEntityDetection),
dyff.AdditionalIdentifiers(reportOptions.additionalIdentifiers...),
)
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/cmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,17 @@ spec.replicas (apps/v1/Deployment/test)
Expect(err).ToNot(HaveOccurred())
Expect(out).To(BeEquivalentTo("\n"))
})

It("should ignore the 'whitespace only' changes", func() {
out, err := dyff("between",
"--omit-header",
"--ignore-whitespace-changes",
assets("issues", "issue-222", "from.yml"),
assets("issues", "issue-222", "to.yml"))

Expect(err).ToNot(HaveOccurred())
Expect(out).To(BeEquivalentTo("\n"))
})
})

Context("last-applied command", func() {
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
type reportConfig struct {
style string
ignoreOrderChanges bool
ignoreWhitespaceChanges bool
kubernetesEntityDetection bool
noTableStyle bool
doNotInspectCerts bool
Expand All @@ -58,6 +59,7 @@ type reportConfig struct {
var defaults = reportConfig{
style: "human",
ignoreOrderChanges: false,
ignoreWhitespaceChanges: false,
kubernetesEntityDetection: true,
noTableStyle: false,
doNotInspectCerts: false,
Expand All @@ -78,6 +80,7 @@ var reportOptions reportConfig
func applyReportOptionsFlags(cmd *cobra.Command) {
// Compare options
cmd.Flags().BoolVarP(&reportOptions.ignoreOrderChanges, "ignore-order-changes", "i", defaults.ignoreOrderChanges, "ignore order changes in lists")
cmd.Flags().BoolVar(&reportOptions.ignoreWhitespaceChanges, "ignore-whitespace-changes", defaults.ignoreWhitespaceChanges, "ignore leading or trailing whitespace changes")
cmd.Flags().BoolVarP(&reportOptions.kubernetesEntityDetection, "detect-kubernetes", "", defaults.kubernetesEntityDetection, "detect kubernetes entities")
cmd.Flags().StringArrayVar(&reportOptions.additionalIdentifiers, "additional-identifier", defaults.additionalIdentifiers, "use additional identifier candidates in named entry lists")
cmd.Flags().StringSliceVar(&reportOptions.filters, "filter", defaults.filters, "filter reports to a subset of differences based on supplied arguments")
Expand Down
9 changes: 9 additions & 0 deletions pkg/dyff/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ some:
dyff.REMOVAL, yml(`version: v1`), nil,
dyff.ADDITION, nil, yml(`release: v1`))))
})

It("should ignore leading and trailing whitespace changes if configured", func() {
from := yml(`{"foo": "bar"}`)
to := yml(`{"foo": "bar "}`)

diffs, err := compare(from, to, dyff.IgnoreWhitespaceChanges(true))
Expect(err).To(BeNil())
Expect(diffs).To(BeNil())
})
})

Context("Given two YAML structures with simple lists", func() {
Expand Down
25 changes: 21 additions & 4 deletions pkg/dyff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type CompareOption func(*compareSettings)
type compareSettings struct {
NonStandardIdentifierGuessCountThreshold int
IgnoreOrderChanges bool
IgnoreWhitespaceChanges bool
KubernetesEntityDetection bool
AdditionalIdentifiers []string
}
Expand Down Expand Up @@ -71,6 +72,13 @@ func IgnoreOrderChanges(value bool) CompareOption {
}
}

// IgnoreWhitespaceChanges disables the detection for whitespace only changes
func IgnoreWhitespaceChanges(value bool) CompareOption {
return func(settings *compareSettings) {
settings.IgnoreWhitespaceChanges = value
}
}

// KubernetesEntityDetection enabled detecting entity identifiers from Kubernetes "kind:" and "metadata:" fields.
func KubernetesEntityDetection(value bool) CompareOption {
return func(settings *compareSettings) {
Expand Down Expand Up @@ -595,19 +603,24 @@ func (compare *compare) namedEntryLists(path ytbx.Path, identifier listItemIdent
}

func (compare *compare) nodeValues(path ytbx.Path, from *yamlv3.Node, to *yamlv3.Node) ([]Diff, error) {
result := make([]Diff, 0)
if strings.Compare(from.Value, to.Value) != 0 {
result = append(result, Diff{
// leave and don't report any differences if ignore whitespaces changes is
// configured and it is really only a whitespace only change between the strings
if compare.settings.IgnoreWhitespaceChanges && isWhitespaceOnlyChange(from.Value, to.Value) {
return nil, nil
}

return []Diff{{
&path,
[]Detail{{
Kind: MODIFICATION,
From: from,
To: to,
}},
})
}}, nil
}

return result, nil
return nil, nil
}

func (compare *compare) boolValues(path ytbx.Path, from *yamlv3.Node, to *yamlv3.Node) ([]Diff, error) {
Expand Down Expand Up @@ -1135,3 +1148,7 @@ func grab(node *yamlv3.Node, pathString string) (*yamlv3.Node, error) {
pathString,
)
}

func isWhitespaceOnlyChange(from string, to string) bool {
return strings.Trim(from, " \n") == strings.Trim(to, " \n")
}
4 changes: 0 additions & 4 deletions pkg/dyff/output_human.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,10 +632,6 @@ func isMultiLine(from string, to string) bool {
return strings.Contains(from, "\n") || strings.Contains(to, "\n")
}

func isWhitespaceOnlyChange(from string, to string) bool {
return strings.Trim(from, " \n") == strings.Trim(to, " \n")
}

func showWhitespaceCharacters(text string) string {
return strings.Replace(strings.Replace(text, "\n", bold("↵\n"), -1), " ", bold("·"), -1)
}
Expand Down
Loading