-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Replace t.fatalf with testify require #16038
Conversation
Signed-off-by: Ari1009 <[email protected]>
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this cleanup PR! While we're doing this, let's do it in the most idiomatic way. Please see inline comments for suggestions and issues!
if err != ErrServerEOF { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
require.Equal(t, ErrServerEOF, err, "unexpected error: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use EqualError
?
if applyIdx == 0 && cloneIdx == 0 { | ||
t.Fatalf("file doesn't contain expected contents") | ||
} | ||
assert.False(t, applyIdx == 0 && cloneIdx == 0, "file doesn't contain expected contents") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a redaction of severity from Fatalf
to assert
. Should this not be at least require
?
for i, fbt := range want { | ||
t.Errorf("Want(%v)=%v", i, fbt.statements) | ||
} | ||
assert.True(t, reflect.DeepEqual(got, want), "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.Equal
uses reflect.DeepEqual
under the hood. We should just use assert.Equal(t, want, got)
.
for i, fbt := range got { | ||
assert.Equal(t, fbt.statements, want[i].statements, "Got (%v)=%v, want (%v)=%v", i, fbt.statements, i, want[i].statements) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems superfluous given we've validated the equality of the entire slice, above?
go/vt/binlog/tables_filter_test.go
Outdated
if want != got { | ||
t.Errorf("want\n%s, got\n%s", want, got) | ||
} | ||
assert.Equal(t, want, got, "binlogConnStreamer.tablesFilterFunc(): got:\n%+v\nwant:\n%+v", got, want) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove the got:\n%+v\nwant:\n%+v
output, since testify
prints the same kind of output anyway.
go/vt/wrangler/tablet_test.go
Outdated
require.Error(t, err, "DeleteTablet on primary: want error") | ||
require.Contains(t, err.Error(), wantError, "DeleteTablet on primary: want specific error message") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use a single ErrorContains
test instead of the two tests above.
go/vt/wrangler/vexec_test.go
Outdated
if !testCase.result.Equal(result) { | ||
t.Errorf("mismatched result:\nwant: %v\ngot: %v", testCase.result, result) | ||
} | ||
assert.True(t, testCase.result.Equal(result), "mismatched result:\nwant: %v\ngot: %v", testCase.result, result) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove the got:\n%+v\nwant:\n%+v
output, since testify
prints the same kind of output anyway.
go/vt/wrangler/vexec_test.go
Outdated
if !strings.Contains(err.Error(), testCase.errorString) { | ||
t.Fatalf("Wrong error, want %s, got %s", testCase.errorString, err.Error()) | ||
} | ||
assert.Contains(t, err.Error(), testCase.errorString, "Wrong error, want %s, got %s", testCase.errorString, err.Error()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use ErrorContains()
instead.
go/vt/zkctl/zkctl_test.go
Outdated
} | ||
zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header") | ||
require.NoError(t, err, "MakeZooCfg err: %v", err) | ||
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg), "Expected tpcKeepAliveCfg in zkObservedConf") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be require
given the original Fatalf
?
go/vt/zkctl/zkctl_test.go
Outdated
zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header") | ||
require.NoError(t, err, "MakeZooCfg err: %v", err) | ||
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", tpcKeepAliveCfg), "Expected tpcKeepAliveCfg in zkObservedConf") | ||
assert.Contains(t, zkObservedConf, fmt.Sprintf("\n%s\n", adminServerCfg), "Expected adminServerCfg in zkObservedConf") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be require
?
Signed-off-by: Ari1009 <[email protected]>
Thanks, @shlomi-noach, for the review. I have made the necessary changes. Could you please check it out? |
Signed-off-by: Ari1009 <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #16038 +/- ##
=======================================
Coverage 68.23% 68.23%
=======================================
Files 1543 1543
Lines 197555 197555
=======================================
+ Hits 134796 134808 +12
+ Misses 62759 62747 -12 ☔ View full report in Codecov by Sentry. |
Static code checks fails on:
Can you please format those files via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more changes needed, as well as formatting. Thank you!
go/trace/trace_test.go
Outdated
} | ||
|
||
if tracer.name != serviceName { | ||
t.Fatalf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name) | ||
require.FailNow(t, fmt.Sprintf("expected the name to be `%v` but it was `%v`", serviceName, tracer.name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we replace with require.Equal(t, serviceName, tracer.name)
?
go/trace/trace_test.go
Outdated
@@ -62,11 +62,11 @@ func TestRegisterService(t *testing.T) { | |||
closer := StartTracing(serviceName) | |||
tracer, ok := closer.(*fakeTracer) | |||
if !ok { | |||
t.Fatalf("did not get the expected tracer, got %+v (%T)", tracer, tracer) | |||
require.FailNow(t, fmt.Sprintf("did not get the expected tracer, got %+v (%T)", tracer, tracer)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we replace this if
statement with require.True(t, ok, ...)
?
} | ||
} | ||
assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error") | ||
assert.Equal(t, want, got, "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's get rid of got:\n%+v\nwant:\n%+v
} | ||
} | ||
assert.EqualError(t, err, ErrServerEOF.Error(), "unexpected error") | ||
assert.Equal(t, want, got, "binlogConnStreamer.parseEvents(): got:\n%+v\nwant:\n%+v", got, want) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's get rid of got:\n%+v\nwant:\n%+v
Signed-off-by: Ari1009 <[email protected]>
@shlomi-noach made changes. Thanks for the quick updates. |
Signed-off-by: Ari1009 <[email protected]>
@dbussink , Just made those changes you mentioned. Sorry about the weird formatting with the license. Seems an extension I had messed things up when saving. My bad! |
Commented, but there's many other cases of |
Signed-off-by: Ari1009 <[email protected]>
Signed-off-by: Ari1009 <[email protected]>
@dbussink, I removed strings that didn't seem to have any relevant context. Are there any other strings you think might be unnecessary? |
go/vt/wrangler/tablet_test.go
Outdated
t.Fatalf("DeleteTablet failed: %v", err) | ||
} | ||
err = wr.DeleteTablet(context.Background(), tablet.Alias, true) | ||
require.NoError(t, err, "DeleteTablet failed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Ari1009 I think you can remove this one.
go/vt/wrangler/tablet_test.go
Outdated
t.Fatalf("UpdateShardFields failed: %v", err) | ||
} | ||
}) | ||
require.NoError(t, err, "UpdateShardFields failed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the last string here either.
go/vt/wrangler/tablet_test.go
Outdated
t.Fatalf("UpdateShardFields failed: %v", err) | ||
} | ||
}) | ||
require.NoError(t, err, "UpdateShardFields failed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as well.
go/vt/wrangler/tablet_test.go
Outdated
t.Fatalf("DeleteTablet failed: %v", err) | ||
} | ||
err = wr.DeleteTablet(context.Background(), tablet1.Alias, false) | ||
require.NoError(t, err, "DeleteTablet failed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too.
go/vt/zkctl/zkctl_test.go
Outdated
t.Fatalf("Expected adminServerCfg in zkObservedConf") | ||
} | ||
zkObservedConf, err := MakeZooCfg([]string{zkConf.ConfigFile()}, zkConf, "header") | ||
require.NoError(t, err, "MakeZooCfg err: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem useful to print the same error again.
go/vt/zkctl/zkctl_test.go
Outdated
t.Fatalf("Init() err: %v", err) | ||
} | ||
err = zkd.Init() | ||
require.NoError(t, err, "Init() err: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, not very useful to print the same error again.
go/vt/zkctl/zkctl_test.go
Outdated
t.Fatalf("Shutdown() err: %v", err) | ||
} | ||
err = zkd.Shutdown() | ||
require.NoError(t, err, "Shutdown() err: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need here either.
go/vt/zkctl/zkctl_test.go
Outdated
t.Fatalf("Teardown() err: %v", err) | ||
} | ||
err = zkd.Start() | ||
require.NoError(t, err, "Start() err: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need here either.
go/vt/zkctl/zkctl_test.go
Outdated
|
||
err = zkd.Teardown() | ||
require.NoError(t, err, "Teardown() err: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need here either.
I don't think it's needed anywhere, except if other / additional information is provided beyond the actual error. But I don't think that's anywhere here, so we don't really need it. Just a plain string isn't that useful either, it already points at the line where it fails so it's obvious then which operation failed, no need to repeat that in the string. |
Signed-off-by: Ari1009 <[email protected]>
@dbussink, I have completed the necessary cleanups. |
Thank you for your contribution, @Ari1009! |
Description
This PR replaces some t.fatalf instances with testify's require
Related Issue(s)
#15182
Checklist