-
Notifications
You must be signed in to change notification settings - Fork 670
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
refactor: introduce *test
packages in lieu of //go:build test
#3238
Conversation
- prefix(github.com/ava-labs/avalanchego) | ||
- alias | ||
- dot |
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.
ni: seems can be put back
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.
I think having the dot import be the bottom makes sense
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 was a deliberate change to make the dot imports more obvious (I think it should be done for "blank" underscore imports too, but that's beyond this PR).
Both dot and underscore imports fundamentally change the way the rest of the code works, but in a way that's easily missed. Having them called out through placement is one way to mitigate this.
codec/codectest/codectest.go
Outdated
@@ -127,7 +127,7 @@ type myStruct struct { | |||
} | |||
|
|||
// Test marshaling/unmarshaling a complicated struct | |||
func TestStruct(codec GeneralCodec, t testing.TB) { | |||
func TestStruct(cdec codec.GeneralCodec, t testing.TB) { |
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.
nit: could we call the variable c
(or generalCodec
)? cdec
seemed like a misspelling to me initially
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.
Either that or alias the package import?
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.
seemed like a misspelling to me initially
Hehe I didn't like this either, and was hoping nobody would care.
Either that or alias the package import?
Good idea. There's actually prior art in this vein so I'll rename the package to codecpkg
.
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.
What's here LGTM apart from the few nits identified by @darioush. Are you intending to update the linter in this PR?
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/chains/atomic" |
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 action required) So the dot import is restricted to package renames in test files (e.g. atomic_test.go has package atomic_test and is allowed to use dot imports)? I guess that makes sense, though allowing dot import of a target package by its subordinate test package might not be so bad.
codec/codectest/codectest.go
Outdated
@@ -127,7 +127,7 @@ type myStruct struct { | |||
} | |||
|
|||
// Test marshaling/unmarshaling a complicated struct | |||
func TestStruct(codec GeneralCodec, t testing.TB) { | |||
func TestStruct(cdec codec.GeneralCodec, t testing.TB) { |
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.
Either that or alias the package import?
@@ -1,8 +1,6 @@ | |||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | |||
// See the file LICENSE for licensing terms. | |||
|
|||
//go:build test | |||
|
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 action required) Why is this file suffixed with _test if it doesn't contain a test? Also not clear why TestBenchable should be exported if it's defined in a test file from which nothing can be imported.
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.
Why is this file suffixed with _test if it doesn't contain a test?
Because it contains code that is required by the other benchlist
tests but by nothing else. The file was just incorrectly named. From the PR description: In some cases no refactoring was necessary as no other packages depended on the test-only code. These were merely renamed to include a _test.go suffix.
Also not clear why TestBenchable should be exported if it's defined in a test file from which nothing can be imported.
Typically it wouldn't be, and it has no effect either way. I'm just trying to keep this PR to the minimum required to remove the tags. Things like this and the stuttering that Stephen mentions are deliberately out of scope, lest the delta blows up.
//go:build test | ||
|
||
package cache | ||
package cachetest |
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.
I think there are a number of fields that stutter based on this rename - do we want to rename things like cachetest.TestIntSize
to cachetest.IntSize
?
If we want to move that into a followup because the change right now is "methodical" whereas this would be a bit more of a "case-by-case" basis... I think that's fine... But I think it should be a fast-follow... Not an amorphous TODO (I'd personally be in-favor of just cleaning them up in this PR)
I think this applies to most (if not all) of the test
packages in this refactor
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.
They were deliberately ignored to be allow this PR to have the smallest possible footprint required to remove test tags. I'll do them as an immediate PR (might even be ready before you re-review this one).
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.
(might even be ready before you re-review this one)
I take this back. There are sooooo many of them.
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.
- prefix(github.com/ava-labs/avalanchego) | ||
- alias | ||
- dot |
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.
I think having the dot import be the bottom makes sense
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.
Shouldn't this be the validatorstest
package? (side note - hate that it is plural)
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.
Done
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.
Ahh, yes, thanks for the reminder. |
Why this should be merged
See #3173 original rationale. Using build tags is proving problematic so this is the beginning of a refactor to move test-only code out of production packages.
How this works
Mechanical refactoring. For package
foo
:foo/footest
foo/test_*.go
filesfootest
foo/*_test.go
file results in a circular dependency:foo_test
instead offoo
(notfootest
without the underscore); this will be compiled separately and then linked intofoo
's test binary (source)foo
intofoo_test
. In all other circumstances we wouldn't expect to qualify thefoo.*
exported identifiers by prepending the package name, so the dot import mirrors this. This usage of dot imports MUST NOT be considered a precedent for introducing the pattern for any other reason.In some cases no refactoring was necessary as no other packages depended on the test-only code. These were merely renamed to include a
_test.go
suffix.How this was tested
CI