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

refactor: introduce *test packages in lieu of //go:build test #3238

Merged
merged 29 commits into from
Aug 2, 2024

Conversation

ARR4N
Copy link
Contributor

@ARR4N ARR4N commented Jul 26, 2024

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:

  1. Move all test-only code into foo/footest
    1. These are typically the foo/test_*.go files
  2. Update all tests depending on the code now in footest
  3. If a foo/*_test.go file results in a circular dependency:
    1. Change the offending file's package to foo_test instead of foo (not footest without the underscore); this will be compiled separately and then linked into foo's test binary (source)
    2. (optional) Dot-import foo into foo_test. In all other circumstances we wouldn't expect to qualify the foo.* 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

@ARR4N ARR4N marked this pull request as ready for review July 26, 2024 18:58
@ARR4N ARR4N requested review from StephenButtolph, marun and yacovm and removed request for darioush, danlaine, ceyonur, joshua-kim and dboehm-avalabs July 26, 2024 18:59
- prefix(github.com/ava-labs/avalanchego)
- alias
- dot
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

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.

@@ -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) {
Copy link
Contributor

@darioush darioush Aug 1, 2024

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

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@marun marun left a 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"
Copy link
Contributor

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.

@@ -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) {
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
Copy link
Contributor

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

Copy link
Contributor

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh that breaks the coreth dependency.

image

@ARR4N
Copy link
Contributor Author

ARR4N commented Aug 2, 2024

What's here LGTM apart from the few nits identified by @darioush. Are you intending to update the linter in this PR?

Ahh, yes, thanks for the reminder.

@StephenButtolph StephenButtolph added this to the v1.11.11 milestone Aug 2, 2024
@StephenButtolph StephenButtolph added the cleanup Code quality improvement label Aug 2, 2024
@StephenButtolph StephenButtolph added this pull request to the merge queue Aug 2, 2024
Merged via the queue into master with commit c2f1f46 Aug 2, 2024
20 checks passed
@StephenButtolph StephenButtolph deleted the arr4n/test-packages-over-tags branch August 2, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Code quality improvement
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants