mirror of https://github.com/fluxcd/cli-utils.git
fix: Make testutil.AssertEqual more like testify
- Swap actual and expected arguments - Add optional Sprintf arguments - Add messages to existing usages
This commit is contained in:
parent
899a44405e
commit
2ca6f199e4
|
@ -1258,7 +1258,9 @@ func TestApplier(t *testing.T) {
|
|||
sort.Sort(testutil.GroupedEventsByID(receivedEvents))
|
||||
|
||||
// Validate the rest of the events
|
||||
testutil.AssertEqual(t, receivedEvents, tc.expectedEvents)
|
||||
testutil.AssertEqual(t, tc.expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(tc.expectedEvents))
|
||||
|
||||
// Validate that the expected timeout was the cause of the run completion.
|
||||
// just in case something else cancelled the run
|
||||
|
@ -1690,7 +1692,9 @@ func TestApplierCancel(t *testing.T) {
|
|||
}
|
||||
|
||||
// Validate the rest of the events
|
||||
testutil.AssertEqual(t, receivedEvents, tc.expectedEvents)
|
||||
testutil.AssertEqual(t, tc.expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(tc.expectedEvents))
|
||||
|
||||
// Validate that the expected timeout was the cause of the run completion.
|
||||
// just in case something else cancelled the run
|
||||
|
@ -1819,8 +1823,13 @@ func TestReadAndPrepareObjects(t *testing.T) {
|
|||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
testutil.AssertEqual(t, tc.applyObjs, applyObjs)
|
||||
testutil.AssertEqual(t, tc.pruneObjs, pruneObjs)
|
||||
testutil.AssertEqual(t, applyObjs, tc.applyObjs,
|
||||
"Actual applied objects (%d) do not match expected applied objects (%d)",
|
||||
len(applyObjs), len(tc.applyObjs))
|
||||
|
||||
testutil.AssertEqual(t, pruneObjs, tc.pruneObjs,
|
||||
"Actual pruned objects (%d) do not match expected pruned objects (%d)",
|
||||
len(pruneObjs), len(tc.pruneObjs))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -375,7 +375,9 @@ func TestDestroyerCancel(t *testing.T) {
|
|||
}
|
||||
|
||||
// Validate the rest of the events
|
||||
testutil.AssertEqual(t, receivedEvents, tc.expectedEvents)
|
||||
testutil.AssertEqual(t, tc.expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(tc.expectedEvents))
|
||||
|
||||
// Validate that the expected timeout was the cause of the run completion.
|
||||
// just in case something else cancelled the run
|
||||
|
|
|
@ -232,7 +232,9 @@ func TestInvSetTask(t *testing.T) {
|
|||
t.Errorf("unexpected error running InvAddTask: %s", result.Err)
|
||||
}
|
||||
actual, _ := client.GetClusterObjs(nil, common.DryRunNone)
|
||||
testutil.AssertEqual(t, actual, tc.expectedObjs)
|
||||
testutil.AssertEqual(t, tc.expectedObjs, actual,
|
||||
"Actual cluster objects (%d) do not match expected cluster objects (%d)",
|
||||
len(actual), len(tc.expectedObjs))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -202,7 +202,9 @@ loop:
|
|||
},
|
||||
},
|
||||
}
|
||||
testutil.AssertEqual(t, receivedEvents, expectedEvents)
|
||||
testutil.AssertEqual(t, expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(expectedEvents))
|
||||
}
|
||||
|
||||
func TestWaitTask_Timeout(t *testing.T) {
|
||||
|
@ -327,7 +329,9 @@ loop:
|
|||
},
|
||||
},
|
||||
}
|
||||
testutil.AssertEqual(t, receivedEvents, expectedEvents)
|
||||
testutil.AssertEqual(t, expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(expectedEvents))
|
||||
}
|
||||
|
||||
func TestWaitTask_StartAndComplete(t *testing.T) {
|
||||
|
@ -390,7 +394,9 @@ loop:
|
|||
},
|
||||
},
|
||||
}
|
||||
testutil.AssertEqual(t, receivedEvents, expectedEvents)
|
||||
testutil.AssertEqual(t, expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(expectedEvents))
|
||||
}
|
||||
|
||||
func TestWaitTask_Cancel(t *testing.T) {
|
||||
|
@ -450,7 +456,9 @@ loop:
|
|||
},
|
||||
},
|
||||
}
|
||||
testutil.AssertEqual(t, receivedEvents, expectedEvents)
|
||||
testutil.AssertEqual(t, expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(expectedEvents))
|
||||
}
|
||||
|
||||
func TestWaitTask_SingleTaskResult(t *testing.T) {
|
||||
|
@ -531,7 +539,9 @@ loop:
|
|||
},
|
||||
},
|
||||
}
|
||||
testutil.AssertEqual(t, receivedEvents, expectedEvents)
|
||||
testutil.AssertEqual(t, expectedEvents, receivedEvents,
|
||||
"Actual events (%d) do not match expected events (%d)",
|
||||
len(receivedEvents), len(expectedEvents))
|
||||
|
||||
expectedResults := []TaskResult{
|
||||
{}, // Empty result means success
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/onsi/gomega/format"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Equal returns a matcher for use with Gomega that uses go-cmp's cmp.Equal to
|
||||
|
@ -31,21 +32,21 @@ type cmpMatcher struct {
|
|||
}
|
||||
|
||||
func (cm *cmpMatcher) Match(actual interface{}) (bool, error) {
|
||||
match := cmp.Equal(actual, cm.expected, cmpopts.EquateErrors())
|
||||
match := cmp.Equal(cm.expected, actual, cmpopts.EquateErrors())
|
||||
if !match {
|
||||
cm.explanation = errors.New(cmp.Diff(actual, cm.expected, cmpopts.EquateErrors()))
|
||||
cm.explanation = errors.New(cmp.Diff(cm.expected, actual, cmpopts.EquateErrors()))
|
||||
}
|
||||
return match, nil
|
||||
}
|
||||
|
||||
func (cm *cmpMatcher) FailureMessage(actual interface{}) string {
|
||||
return format.Message(actual, "to deeply equal", cm.expected) +
|
||||
"\nDiff:\n" + indent(cm.explanation.Error(), 1)
|
||||
return "\n" + format.Message(actual, "to deeply equal", cm.expected) +
|
||||
"\nDiff (- Expected, + Actual):\n" + indent(cm.explanation.Error(), 1)
|
||||
}
|
||||
|
||||
func (cm *cmpMatcher) NegatedFailureMessage(actual interface{}) string {
|
||||
return format.Message(actual, "not to deeply equal", cm.expected) +
|
||||
"\nDiff:\n" + indent(cm.explanation.Error(), 1)
|
||||
return "\n" + format.Message(actual, "not to deeply equal", cm.expected) +
|
||||
"\nDiff (- Expected, + Actual):\n" + indent(cm.explanation.Error(), 1)
|
||||
}
|
||||
|
||||
func indent(in string, indentation uint) string {
|
||||
|
@ -111,7 +112,7 @@ func (e equalErrorString) Is(err error) bool {
|
|||
|
||||
// AssertEqual fails the test if the actual value does not deeply equal the
|
||||
// expected value. Prints a diff on failure.
|
||||
func AssertEqual(t *testing.T, actual, expected interface{}) {
|
||||
func AssertEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
|
||||
t.Helper() // print the caller's file:line, instead of this func, on failure
|
||||
matcher := Equal(expected)
|
||||
match, err := matcher.Match(actual)
|
||||
|
@ -119,13 +120,13 @@ func AssertEqual(t *testing.T, actual, expected interface{}) {
|
|||
t.Errorf("errored testing equality: %s", err)
|
||||
}
|
||||
if !match {
|
||||
t.Error(matcher.FailureMessage(actual))
|
||||
assert.Fail(t, matcher.FailureMessage(actual), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
|
||||
// AssertNotEqual fails the test if the actual value deeply equals the
|
||||
// expected value. Prints a diff on failure.
|
||||
func AssertNotEqual(t *testing.T, actual, expected interface{}) {
|
||||
func AssertNotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) {
|
||||
t.Helper() // print the caller's file:line, instead of this func, on failure
|
||||
matcher := Equal(expected)
|
||||
match, err := matcher.Match(actual)
|
||||
|
@ -133,6 +134,6 @@ func AssertNotEqual(t *testing.T, actual, expected interface{}) {
|
|||
t.Errorf("errored testing equality: %s", err)
|
||||
}
|
||||
if match {
|
||||
t.Error(matcher.NegatedFailureMessage(actual))
|
||||
assert.Fail(t, matcher.NegatedFailureMessage(actual), msgAndArgs...)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue