-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
zaptest/NewLogger: fail test on internal errors #566
Conversation
Codecov Report
@@ Coverage Diff @@
## abg/expand-testingt #566 +/- ##
=======================================================
+ Coverage 97.48% 97.49% +0.01%
=======================================================
Files 40 40
Lines 2026 2035 +9
=======================================================
+ Hits 1975 1984 +9
Misses 43 43
Partials 8 8
Continue to review full report at Codecov.
|
zaptest/logger_test.go
Outdated
assert.Equal(t.TB, msgs, t.Messages, "logged messages did not match") | ||
} | ||
|
||
func (t *testLogSpy) AssertFailed(v bool) { |
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.
Can we make this an unexported method and have methods AssertFailed()
and AssertPassed()
rather than a boolean argument?
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.
Makes sense
// testLogSpy is a testing.TB that captures logged messages. | ||
type testLogSpy struct { | ||
testing.TB | ||
|
||
mu sync.Mutex | ||
mu sync.RWMutex |
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.
optional/can follow up:
this spy is only used internally, and I don't think any of the tests are concurrent, so we might be able to just remove the mutex.
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.
The spy is thread safe only because testing.TB
is supposed to be thread-
safe, but yeah, I'll drop it.
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.
2339e4e
to
e1812df
Compare
This expands the TestingT interface to a much larger subset of `testing.TB`. The following were left out: - `Error` and `Log` were left out in favor of `Errorf` and `Logf` - `Fatal*` methods were left out in favor of `Errorf` followed by `FailNow` - `Skip*` methods were left out because our test logger shouldn't be skipping tests - `Helper` was left out because not all supported verisons of Go have that
Zap's errorOutput is used exclusively to log internal errors. If this ever gets used in a test, something catastrophic happened and we should fail the test. This changes the logger returned by zaptest.NewLogger to implement this behavior by passing a `WriteSyncer` to `zap.ErrorOutput` that marks the test as failed upon being used. To test this, we set up a test logger and replace its core with one that will always fail to write.
820a61b
to
cdd3f0b
Compare
e1812df
to
2b3338c
Compare
Zap's errorOutput is used exclusively to log internal errors. If this
ever gets used in a test, something catastrophic happened and we should
fail the test.
This changes the logger returned by zaptest.NewLogger to implement this
behavior by passing a
WriteSyncer
tozap.ErrorOutput
that marks thetest as failed upon being used.
To test this, we set up a test logger and replace its core with one that
will always fail to write.