Skip to content
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

Merged
merged 5 commits into from
Oct 31, 2017
Merged

Break dependency between zap and zaptest #523

merged 5 commits into from
Oct 31, 2017

Conversation

akshayjshah
Copy link
Contributor

@akshayjshah akshayjshah commented Oct 30, 2017

In order to introduce new testing helpers into the zaptest package, where users will expect to find them, we need to break the dependency between zap and zaptest. (Unfortunately, we introduced zaptest 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 to internal/ztest, change the rest of zap to use the new internal helpers, and finally convert zaptest 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.

@uber-go uber-go deleted a comment from codecov bot Oct 31, 2017
@akshayjshah akshayjshah changed the title [WIP] Break dependency between zap and zaptest Break dependency between zap and zaptest Oct 31, 2017
Akshay Shah added 4 commits October 31, 2017 10:07
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`.
@codecov
Copy link

codecov bot commented Oct 31, 2017

Codecov Report

Merging #523 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
zaptest/writer.go 100% <ø> (ø)
zaptest/timeout.go 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e003780...49f1c0e. Read the comment docs.


var _timeoutScale = 1.0
"go.uber.org/zap/internal/ztest"
)

// Timeout scales the provided duration by $TEST_TIMEOUT_SCALE.
Copy link
Collaborator

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

Copy link
Contributor Author

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.
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// +build go1.9
Copy link
Collaborator

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?)

Copy link
Contributor Author

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)
Copy link
Collaborator

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

Copy link
Contributor Author

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants