Skip to content

Commit

Permalink
Introduce --ignore-whitespace-changes flag
Browse files Browse the repository at this point in the history
Fixes: #222

Introduce flag to ignore leading and trailing whitespaces in strings.
  • Loading branch information
HeavyWombat committed Aug 2, 2024
1 parent 8e9d095 commit 774205b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
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
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

Check warning on line 78 in pkg/dyff/core.go

View check run for this annotation

Codecov / codecov/patch

pkg/dyff/core.go#L76-L78

Added lines #L76 - L78 were not covered by tests
}
}

// 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

Check warning on line 610 in pkg/dyff/core.go

View check run for this annotation

Codecov / codecov/patch

pkg/dyff/core.go#L610

Added line #L610 was not covered by tests
}

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

0 comments on commit 774205b

Please sign in to comment.