diff --git a/pkg/util/cmputil/reporter.go b/pkg/util/cmputil/reporter.go index cae35bd8e20..ec97571a29f 100644 --- a/pkg/util/cmputil/reporter.go +++ b/pkg/util/cmputil/reporter.go @@ -14,7 +14,13 @@ type DiffReport []Diff func (r DiffReport) GetDiffsForField(path string) DiffReport { var result []Diff for _, diff := range r { - if strings.HasPrefix(path, diff.Path) { + if strings.HasPrefix(diff.Path, path) { + if diff.Path != path { + char := []rune(diff.Path)[len(path)] + if char != '.' && char != '[' { // if the following symbol is not a delimiter or bracket then that's not our path + continue + } + } result = append(result, diff) } } diff --git a/pkg/util/cmputil/reporter_test.go b/pkg/util/cmputil/reporter_test.go index 1ac3a096c61..7aee4cd1532 100644 --- a/pkg/util/cmputil/reporter_test.go +++ b/pkg/util/cmputil/reporter_test.go @@ -130,3 +130,68 @@ func TestIsAddedDeleted_Collections(t *testing.T) { } }) } + +func TestGetDiffsForField(t *testing.T) { + t.Run("should not include fields that starts has prefix", func(t *testing.T) { + diff := DiffReport{ + Diff{ + Path: "Property", + }, + Diff{ + Path: "PropertyData", + }, + } + + result := diff.GetDiffsForField("Property") + require.Len(t, result, 1) + require.Equal(t, "Property", result[0].Path) + }) + + t.Run("should return all changes by parent path", func(t *testing.T) { + diff := DiffReport{ + Diff{ + Path: "Property.Data.Value", + }, + Diff{ + Path: "Property.Array[0].Value", + }, + } + + result := diff.GetDiffsForField("Property") + require.Len(t, result, 2) + }) + + t.Run("should return all elements of array", func(t *testing.T) { + diff := DiffReport{ + Diff{ + Path: "Property[0].Data.Test", + }, + Diff{ + Path: "Property", + }, + Diff{ + Path: "Property[1]", + }, + } + + result := diff.GetDiffsForField("Property") + require.Len(t, result, 3) + }) + + t.Run("should find nothing if parent path does not exist", func(t *testing.T) { + diff := DiffReport{ + Diff{ + Path: "Property[0].Data.Test", + }, + Diff{ + Path: "Property", + }, + Diff{ + Path: "Property[1]", + }, + } + + result := diff.GetDiffsForField("Proper") + require.Empty(t, result) + }) +}