-
Notifications
You must be signed in to change notification settings - Fork 675
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
Conversation
…tside of `_test.go` files
//go:build test
tag if importing "testing" outside of _test.go
files//go:build test
tag if importing testing
outside of _test.go
files
//go:build test
tag if importing testing
outside of _test.go
files//go:build test
tag if importing test packages outside of _test.go
files
This reverts commit b7f0222 because it breaks a number of CI tests. There remains an open issue to do this (in a different PR).
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 change LGTM - will wait to hear if @marun has any thoughts w.r.t. the tests/
directory
There is also a |
There's a {
"gopls": {
"build.buildFlags": [
"--tags='test'",
],
},
"go.testTags": "test",
} |
{ | ||
"gopls": { | ||
"build.buildFlags": [ | ||
// Context: https://github.com/ava-labs/avalanchego/pull/3173 |
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'm surprised to see that this works, but wouldn't touch it if it does.
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.
Is is documented somewhere that this is supported? This also surprises me.
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.
It is documented: https://code.visualstudio.com/docs/languages/json#_json-with-comments. TIL
Maru is OOO - going to merge. |
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 oftest
then the linter fails. To the best of my knowledge, there's no native tag that's set when running tests, so all runs ofgo test
andginkgo
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
Currently detects
testing
andstretchr/testify/*
packages. Asmockgen
doesn't add build tags, detection of mocks is disabled pending a PR to them. ↩