Skip to content

Commit

Permalink
utils/diff: use better error message
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Jul 21, 2020
1 parent 987c200 commit 76aa890
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 10 additions & 1 deletion pkg/utils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func ValidateSpecDiff(s1, s2 interface{}) error {
continue
}
}
msg = append(msg, fmt.Sprintf("(%s) %s '%v' -> '%v'", c.Type, buildFieldPath(c.Path), c.From, c.To))

// build error messages
switch c.Type {
case diff.CREATE:
msg = append(msg, fmt.Sprintf("added %s with value '%v'", buildFieldPath(c.Path), c.To))
case diff.DELETE:
msg = append(msg, fmt.Sprintf("removed %s with value '%v'", buildFieldPath(c.Path), c.From))
case diff.UPDATE:
msg = append(msg, fmt.Sprintf("%s changed from '%v' to '%v'", buildFieldPath(c.Path), c.From, c.To))
}
}

if len(msg) > 0 {
Expand Down
16 changes: 8 additions & 8 deletions pkg/utils/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ strs:
err = ValidateSpecDiff(d1, d2)
c.Assert(err, IsNil)

// add editable element
// add editable element (without specifing alias)
err = yaml.Unmarshal([]byte(`
ints: [11, 13, 12]
strs:
Expand All @@ -89,7 +89,7 @@ ints: [11, 12, 13, 14]
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (create) IntSlice.3 '<nil>' -> '14'")
c.Assert(err.Error(), Equals, "immutable field changed: added IntSlice.3 with value '14'")
}

func (d *diffSuite) TestValidateSpecDiff2(c *C) {
Expand Down Expand Up @@ -144,7 +144,7 @@ slice1:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (update) slice1.1.IntElem '42' -> '43'")
c.Assert(err.Error(), Equals, "immutable field changed: slice1.1.IntElem changed from '42' to '43'")

// Add item with immutable field to editable slice
err = yaml.Unmarshal([]byte(`
Expand All @@ -166,7 +166,7 @@ slice1:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (create) slice1.2.IntElem '<nil>' -> '42'")
c.Assert(err.Error(), Equals, "immutable field changed: added slice1.2.IntElem with value '42'")

// Delete item with immutable field from editable slice
err = yaml.Unmarshal([]byte(`
Expand All @@ -180,7 +180,7 @@ slice1:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (delete) slice1.1.IntElem '42' -> '<nil>'")
c.Assert(err.Error(), Equals, "immutable field changed: removed slice1.1.IntElem with value '42'")
}

func (d *diffSuite) TestValidateSpecDiff3(c *C) {
Expand Down Expand Up @@ -235,7 +235,7 @@ slice2:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (update) StructSlice2.1.IntElem '42' -> '43'")
c.Assert(err.Error(), Equals, "immutable field changed: StructSlice2.1.IntElem changed from '42' to '43'")

// Add item to immutable slice
err = yaml.Unmarshal([]byte(`
Expand All @@ -255,7 +255,7 @@ slice2:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (create) StructSlice2.2.str1 '<nil>' -> 'strv31', (create) StructSlice2.2.str2 '<nil>' -> 'strv32'")
c.Assert(err.Error(), Equals, "immutable field changed: added StructSlice2.2.str1 with value 'strv31', added StructSlice2.2.str2 with value 'strv32'")

// Remove item from immutable slice
err = yaml.Unmarshal([]byte(`
Expand All @@ -269,7 +269,7 @@ slice2:
c.Assert(err, IsNil)
err = ValidateSpecDiff(d1, d2)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "immutable field changed: (delete) StructSlice2.1.str1 'strv12' -> '<nil>', (delete) StructSlice2.1.str2 'strv22' -> '<nil>', (delete) StructSlice2.1.IntElem '42' -> '<nil>', (delete) StructSlice2.1.interface '12' -> '<nil>'")
c.Assert(err.Error(), Equals, "immutable field changed: removed StructSlice2.1.str1 with value 'strv12', removed StructSlice2.1.str2 with value 'strv22', removed StructSlice2.1.IntElem with value '42', removed StructSlice2.1.interface with value '12'")
}

func (d *diffSuite) TestValidateSpecDiff4(c *C) {
Expand Down

0 comments on commit 76aa890

Please sign in to comment.