From 76aa8907d85837ba4ac4b2050fcbd3937bef7a8f Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Tue, 21 Jul 2020 17:40:23 +0800 Subject: [PATCH] utils/diff: use better error message --- pkg/utils/diff.go | 11 ++++++++++- pkg/utils/diff_test.go | 16 ++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pkg/utils/diff.go b/pkg/utils/diff.go index c9d924c201..2c7e7dc3ed 100644 --- a/pkg/utils/diff.go +++ b/pkg/utils/diff.go @@ -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 { diff --git a/pkg/utils/diff_test.go b/pkg/utils/diff_test.go index c858aef5c0..24539e20bf 100644 --- a/pkg/utils/diff_test.go +++ b/pkg/utils/diff_test.go @@ -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: @@ -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 '' -> '14'") + c.Assert(err.Error(), Equals, "immutable field changed: added IntSlice.3 with value '14'") } func (d *diffSuite) TestValidateSpecDiff2(c *C) { @@ -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(` @@ -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 '' -> '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(` @@ -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' -> ''") + c.Assert(err.Error(), Equals, "immutable field changed: removed slice1.1.IntElem with value '42'") } func (d *diffSuite) TestValidateSpecDiff3(c *C) { @@ -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(` @@ -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 '' -> 'strv31', (create) StructSlice2.2.str2 '' -> '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(` @@ -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' -> '', (delete) StructSlice2.1.str2 'strv22' -> '', (delete) StructSlice2.1.IntElem '42' -> '', (delete) StructSlice2.1.interface '12' -> ''") + 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) {