Skip to content

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
@StephenButtolph StephenButtolph deleted the arr4n/test-only-build-tags branch July 8, 2024 16:55
demosdemon added a commit to ava-labs/firewood that referenced this pull request Aug 2, 2025
This allows us to get more useful output when there is a change; e.g.,:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh
'ffi/.golangci.yaml' has unexpected changes from AvalancheGo.
View the upstream changes at: https://github.com/ava-labs/avalanchego/commits/master/.golangci.yml and apply them if necessary.

Current changes:
--- .github/.golangci.yaml      2025-08-02 10:04:10
+++ ffi/.golangci.yaml  2025-08-02 10:01:55
@@ -12,7 +12,7 @@
   #
   # Allowed values: readonly|vendor|mod
   # By default, it isn't set.
-  modules-download-mode: mod
+  modules-download-mode: readonly

   # Include non-test files tagged as test-only.
   # Context: ava-labs/avalanchego#3173
```

Additionally, we can run `apply` to apply our patch against the upstream file and refresh the patch file:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh apply
patching file '.github/.golangci.yaml'
Successfully applied the patch from .github/.golangci.yaml.patch to ffi/.golangci.yaml
Updated expected changes in .github/.golangci.yaml.patch
```
demosdemon added a commit to ava-labs/firewood that referenced this pull request Aug 2, 2025
I also got bored and updated the script. This allows us to get more
useful output when there is a change; e.g.,:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh
'ffi/.golangci.yaml' has unexpected changes from AvalancheGo.
View the upstream changes at: https://github.com/ava-labs/avalanchego/commits/master/.golangci.yml and apply them if necessary.

Current changes:
--- .github/.golangci.yaml      2025-08-02 10:04:10
+++ ffi/.golangci.yaml  2025-08-02 10:01:55
@@ -12,7 +12,7 @@
   #
   # Allowed values: readonly|vendor|mod
   # By default, it isn't set.
-  modules-download-mode: mod
+  modules-download-mode: readonly
 
   # Include non-test files tagged as test-only.
   # Context: ava-labs/avalanchego#3173
```

Additionally, we can run `apply` to apply our patch against the upstream
file and refresh the patch file:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh apply
patching file '.github/.golangci.yaml'
Successfully applied the patch from .github/.golangci.yaml.patch to ffi/.golangci.yaml
Updated expected changes in .github/.golangci.yaml.patch
```

fixes #1164
bernard-avalabs pushed a commit to ava-labs/firewood that referenced this pull request Aug 10, 2025
I also got bored and updated the script. This allows us to get more
useful output when there is a change; e.g.,:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh
'ffi/.golangci.yaml' has unexpected changes from AvalancheGo.
View the upstream changes at: https://github.com/ava-labs/avalanchego/commits/master/.golangci.yml and apply them if necessary.

Current changes:
--- .github/.golangci.yaml      2025-08-02 10:04:10
+++ ffi/.golangci.yaml  2025-08-02 10:01:55
@@ -12,7 +12,7 @@
   #
   # Allowed values: readonly|vendor|mod
   # By default, it isn't set.
-  modules-download-mode: mod
+  modules-download-mode: readonly
 
   # Include non-test files tagged as test-only.
   # Context: ava-labs/avalanchego#3173
```

Additionally, we can run `apply` to apply our patch against the upstream
file and refresh the patch file:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh apply
patching file '.github/.golangci.yaml'
Successfully applied the patch from .github/.golangci.yaml.patch to ffi/.golangci.yaml
Updated expected changes in .github/.golangci.yaml.patch
```

fixes #1164
aiden94a added a commit to aiden94a/firewood that referenced this pull request Sep 23, 2025
I also got bored and updated the script. This allows us to get more
useful output when there is a change; e.g.,:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh
'ffi/.golangci.yaml' has unexpected changes from AvalancheGo.
View the upstream changes at: https://github.com/ava-labs/avalanchego/commits/master/.golangci.yml and apply them if necessary.

Current changes:
--- .github/.golangci.yaml      2025-08-02 10:04:10
+++ ffi/.golangci.yaml  2025-08-02 10:01:55
@@ -12,7 +12,7 @@
   #
   # Allowed values: readonly|vendor|mod
   # By default, it isn't set.
-  modules-download-mode: mod
+  modules-download-mode: readonly
 
   # Include non-test files tagged as test-only.
   # Context: ava-labs/avalanchego#3173
```

Additionally, we can run `apply` to apply our patch against the upstream
file and refresh the patch file:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh apply
patching file '.github/.golangci.yaml'
Successfully applied the patch from .github/.golangci.yaml.patch to ffi/.golangci.yaml
Updated expected changes in .github/.golangci.yaml.patch
```

fixes #1164
night-hacker630n5 added a commit to night-hacker630n5/firewood that referenced this pull request Sep 29, 2025
I also got bored and updated the script. This allows us to get more
useful output when there is a change; e.g.,:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh
'ffi/.golangci.yaml' has unexpected changes from AvalancheGo.
View the upstream changes at: https://github.com/ava-labs/avalanchego/commits/master/.golangci.yml and apply them if necessary.

Current changes:
--- .github/.golangci.yaml      2025-08-02 10:04:10
+++ ffi/.golangci.yaml  2025-08-02 10:01:55
@@ -12,7 +12,7 @@
   #
   # Allowed values: readonly|vendor|mod
   # By default, it isn't set.
-  modules-download-mode: mod
+  modules-download-mode: readonly
 
   # Include non-test files tagged as test-only.
   # Context: ava-labs/avalanchego#3173
```

Additionally, we can run `apply` to apply our patch against the upstream
file and refresh the patch file:

```console
$ .github/scripts/verify_golangci_yaml_changes.sh apply
patching file '.github/.golangci.yaml'
Successfully applied the patch from .github/.golangci.yaml.patch to ffi/.golangci.yaml
Updated expected changes in .github/.golangci.yaml.patch
```

fixes #1164
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