-
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
Break dependency between zap and zaptest #523
Conversation
9e991ea
to
2635460
Compare
In order to add more useful functionality to the `zaptest` package, we'll need to reference the top-level `zap` package. To do that, we need to break some import cycles. As a first step, copy the current testing utilities to an internal package.
Migrate all tests in the main `zap` package to use the internal `ztest` helpers. This lets the `zaptest` package take dependencies on `zap`.
Migrate the benchmarks package to use the internal `ztest` helpers instead of the exported `zaptest` package.
Use the `ztest` internal helper utilities in `zapcore`, which lets the `zaptest` package take dependencies on `zapcore`.
2635460
to
a142dae
Compare
Codecov Report
@@ Coverage Diff @@
## master #523 +/- ##
==========================================
+ Coverage 97.03% 97.06% +0.02%
==========================================
Files 37 39 +2
Lines 2257 2279 +22
==========================================
+ Hits 2190 2212 +22
Misses 59 59
Partials 8 8
Continue to review full report at Codecov.
|
|
||
var _timeoutScale = 1.0 | ||
"go.uber.org/zap/internal/ztest" | ||
) | ||
|
||
// Timeout scales the provided duration by $TEST_TIMEOUT_SCALE. |
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 you mark this as deprecated? I don't think these functions are intended for end-users right? This should purely be for internal testing
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.
Yep, can do.
|
||
// Timeout scales the provided duration by $TEST_TIMEOUT_SCALE. | ||
func Timeout(base time.Duration) time.Duration { | ||
return time.Duration(float64(base) * _timeoutScale) | ||
return ztest.Timeout(base) | ||
} | ||
|
||
// Sleep scales the sleep duration by $TEST_TIMEOUT_SCALE. |
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 as above, should this be deprecated?
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.
Yep.
zaptest/writer_go_19.go
Outdated
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
// +build go1.9 |
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: should we call out why we need 1.9 (e.g., that we're using type aliases?)
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.
Sure, sounds good!
|
||
func readCode(t testing.TB, fname string) []string { | ||
f, err := os.Open(fname) | ||
require.NoError(t, err, "Failed to read %s.", fname) |
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.
defer f.Close()
after the require.NoError
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.
🤦♂️ Good catch.
Mitigate the distasteful copy-paste of `zaptest` by providing a path forward using Go 1.9 aliases. For Go 1.9 users, `zaptest` is now nothing more than a few type aliases; this cleans up the GoDoc output nicely, since it removes the documentation for all the methods on the testing spies while remaining fully backward-compatible. For Go 1.8 and earlier, we keep the original code but add a test to make sure that it exactly matches the version in `internal/ztest`. With this change, we're free to introduce functionality into `zaptest` that relies on `zapcore` and `zap` itself. In particular, we can return `*zap.Logger` from constructors.
a142dae
to
49f1c0e
Compare
In order to introduce new testing helpers into the
zaptest
package, where users will expect to find them, we need to break the dependency betweenzap
andzaptest
. (Unfortunately, we introducedzaptest
before Go supported internal packages and squatted on the best user-facing name.)This PR can be reviewed commit-by-commit if necessary. It makes a wordy, but logically simple series of changes: we copy the
zaptest
package tointernal/ztest
, change the rest ofzap
to use the new internal helpers, and finally convertzaptest
to a shim around the internal helpers. In Go 1.9, we use type aliases to also clean up the documentation (aliases don't have their methods documented); for older Go versions, we leave a copy of the original code and a test to make sure that the two versions don't drift out of sync.This should make #518 much nicer. Since it begins using type aliases, it depends on #524.