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

build(tests): require //go:build test tag if importing test packages outside of _test.go files #3173

Merged
merged 18 commits into from
Jul 8, 2024

Conversation

ARR4N
Copy link
Contributor

@ARR4N ARR4N commented Jul 4, 2024

Why this should be merged

Including test-only code in non-test files risks leaking said functionality into production.

How this works

A new linter finds all *.go files that (i) aren't _test.go; and (ii) import test packages1. If any of these files don't include a build-constraint tag of test then the linter fails. To the best of my knowledge, there's no native tag that's set when running tests, so all runs of go test and ginkgo now need to include -tags test.

How this was tested

Manually: I wrote the linter first as a way to find the offending files.

Future work

See #3174 for files flagged by the linter that have been temporarily ignored.

Footnotes

  1. Currently detects testing and stretchr/testify/* packages. As mockgen doesn't add build tags, detection of mocks is disabled pending a PR to them.

@ARR4N ARR4N changed the title build(tests): require //go:build test tag if importing "testing" outside of _test.go files build(tests): require //go:build test tag if importing testing outside of _test.go files Jul 4, 2024
@ARR4N ARR4N marked this pull request as ready for review July 4, 2024 18:51
@ARR4N ARR4N requested review from marun and removed request for dhrubabasu, danlaine and abi87 July 4, 2024 18:59
@ARR4N ARR4N requested a review from joshua-kim as a code owner July 5, 2024 08:46
@ARR4N ARR4N changed the title build(tests): require //go:build test tag if importing testing outside of _test.go files build(tests): require //go:build test tag if importing test packages outside of _test.go files Jul 5, 2024
ARR4N added 3 commits July 5, 2024 15:30
This reverts commit b7f0222 because it breaks a number of CI tests. There remains an open issue to do this (in a different PR).
StephenButtolph
StephenButtolph previously approved these changes Jul 5, 2024
Copy link
Contributor

@StephenButtolph StephenButtolph left a comment

Choose a reason for hiding this comment

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

This change LGTM - will wait to hear if @marun has any thoughts w.r.t. the tests/ directory

@StephenButtolph StephenButtolph dismissed their stale review July 5, 2024 17:41

still looking

@StephenButtolph
Copy link
Contributor

This change seems to break the default behavior for running tests in VS code.. ex:
image
image

Running the test results in:

go test -timeout 30s -coverprofile=/var/folders/5l/z00z8zbd0dbd6lf8djdgl_000000gn/T/vscode-godMWCa1/go-code-cover -run ^TestGossiperGossip$ github.com/ava-labs/avalanchego/network/p2p/gossip -coverpkg=all

# github.com/ava-labs/avalanchego/network/p2p/gossip [github.com/ava-labs/avalanchego/network/p2p/gossip.test]
/Users/stephen/go/src/github.com/ava-labs/avalanchego/network/p2p/gossip/gossip_test.go:107:30: undefined: common.FakeSender
/Users/stephen/go/src/github.com/ava-labs/avalanchego/network/p2p/gossip/gossip_test.go:136:29: undefined: common.FakeSender
/Users/stephen/go/src/github.com/ava-labs/avalanchego/network/p2p/gossip/gossip_test.go:512:22: undefined: common.FakeSender
FAIL	github.com/ava-labs/avalanchego/network/p2p/gossip [build failed]
FAIL

Should we add a .vscode/settings.json file? It would specify:

{
    "go.buildTags": "test"
}

If we do this then we end up with:

go test -timeout 30s -tags test -coverprofile=/var/folders/5l/z00z8zbd0dbd6lf8djdgl_000000gn/T/vscode-go731twr/go-code-cover -run ^TestGossiperGossip$ github.com/ava-labs/avalanchego/network/p2p/gossip -coverpkg=all

ok  	github.com/ava-labs/avalanchego/network/p2p/gossip	0.876s	coverage: 9.8% of statements in all

which works as expected.

@StephenButtolph
Copy link
Contributor

StephenButtolph commented Jul 5, 2024

There is also a go.testTags option, but it seems like that doesn't fix the errors presented by the IDE 😞 (although it would fix the running of the tests)

@StephenButtolph StephenButtolph added the cleanup Code quality improvement label Jul 6, 2024
@StephenButtolph StephenButtolph added this to the v1.11.10 milestone Jul 6, 2024
@ARR4N
Copy link
Contributor Author

ARR4N commented Jul 8, 2024

There is also a go.testTags option, but it seems like that doesn't fix the errors presented by the IDE 😞 (although it would fix the running of the tests)

There's a gopls build-flag setting that fixes the IDE complaints:

{
    "gopls": {
        "build.buildFlags": [
            "--tags='test'",
        ],
    },
    "go.testTags": "test",
}

{
"gopls": {
"build.buildFlags": [
// Context: https://github.com/ava-labs/avalanchego/pull/3173
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm surprised to see that this works, but wouldn't touch it if it does.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is is documented somewhere that this is supported? This also surprises me.

Copy link
Contributor

Choose a reason for hiding this comment

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

@StephenButtolph
Copy link
Contributor

Maru is OOO - going to merge.

@StephenButtolph StephenButtolph added this pull request to the merge queue Jul 8, 2024
Merged via the queue into master with commit 2af3890 Jul 8, 2024
20 checks passed
@StephenButtolph StephenButtolph deleted the arr4n/test-only-build-tags branch July 8, 2024 16:55
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