Skip to content

Commit 2af3890

Browse files
authored
build(tests): require //go:build test tag if importing test packages outside of _test.go files (#3173)
1 parent 45a30f9 commit 2af3890

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+137
-7
lines changed

.golangci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ run:
1515
# By default, it isn't set.
1616
modules-download-mode: readonly
1717

18+
# Include non-test files tagged as test-only.
19+
# Context: https://github.com/ava-labs/avalanchego/pull/3173
20+
build-tags:
21+
- test
22+
1823
output:
1924
# Make issues output unique by line.
2025
# Default: true

.vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"gopls": {
3+
"build.buildFlags": [
4+
// Context: https://github.com/ava-labs/avalanchego/pull/3173
5+
// Without this tag, the language server won't build the test-only
6+
// code in non-_test.go files.
7+
"--tags='test'",
8+
],
9+
},
10+
"go.testTags": "test",
11+
}

cache/test_cacher.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package cache
57

68
import (

chains/atomic/test_shared_memory.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package atomic
57

68
import (

codec/test_codec.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package codec
57

68
import (

database/benchmark_database.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package database
57

68
import (

database/test_database.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package database
57

68
import (

ids/test_aliases.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package ids
57

68
import "github.com/stretchr/testify/require"

scripts/build_fuzz.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ do
2828
echo "Fuzzing $func in $file"
2929
parentDir=$(dirname "$file")
3030
# If any of the fuzz tests fail, return exit code 1
31-
if ! go test "$parentDir" -run="$func" -fuzz="$func" -fuzztime="${fuzzTime}"s; then
31+
if ! go test -tags test "$parentDir" -run="$func" -fuzz="$func" -fuzztime="${fuzzTime}"s; then
3232
failed=true
3333
fi
3434
done

scripts/build_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ fi
1818
TEST_TARGETS="$(eval "go list ./... ${EXCLUDED_TARGETS}")"
1919

2020
# shellcheck disable=SC2086
21-
go test -shuffle=on -race -timeout="${TIMEOUT:-120s}" -coverprofile="coverage.out" -covermode="atomic" ${TEST_TARGETS}
21+
go test -tags test -shuffle=on -race -timeout="${TIMEOUT:-120s}" -coverprofile="coverage.out" -covermode="atomic" ${TEST_TARGETS}

scripts/lint.sh

+26-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fi
2929
# by default, "./scripts/lint.sh" runs all lint tests
3030
# to run only "license_header" test
3131
# TESTS='license_header' ./scripts/lint.sh
32-
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_no_error_inline_func"}
32+
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_no_error_inline_func import_testing_only_in_tests"}
3333

3434
function test_golangci_lint {
3535
go install -v github.com/golangci/golangci-lint/cmd/[email protected]
@@ -86,6 +86,31 @@ function test_interface_compliance_nil {
8686
fi
8787
}
8888

89+
function test_import_testing_only_in_tests {
90+
ROOT=$( git rev-parse --show-toplevel )
91+
NON_TEST_GO_FILES=$( find "${ROOT}" -iname '*.go' ! -iname '*_test.go');
92+
93+
IMPORT_TESTING=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '^\s*(import\s+)?"testing"');
94+
IMPORT_TESTIFY=$( echo "${NON_TEST_GO_FILES}" | xargs grep -l '"github.com/stretchr/testify');
95+
# TODO(arr4n): send a PR to add support for build tags in `mockgen` and then enable this.
96+
# IMPORT_GOMOCK=$( echo "${NON_TEST_GO_FILES}" | xargs grep -l '"go.uber.org/mock');
97+
HAVE_TEST_LOGIC=$( printf "%s\n%s" "${IMPORT_TESTING}" "${IMPORT_TESTIFY}" );
98+
99+
TAGGED_AS_TEST=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '^\/\/go:build\s+(.+(,|\s+))?test[,\s]?');
100+
101+
# -3 suppresses files that have test logic and have the "test" build tag
102+
# -2 suppresses files that are tagged despite not having detectable test logic
103+
UNTAGGED=$( comm -23 <( echo "${HAVE_TEST_LOGIC}" | sort -u ) <( echo "${TAGGED_AS_TEST}" | sort -u ) );
104+
if [ -z "${UNTAGGED}" ];
105+
then
106+
return 0;
107+
fi
108+
109+
echo "Non-test Go files importing test-only packages MUST have '//go:build test' tag:";
110+
echo "${UNTAGGED}";
111+
return 1;
112+
}
113+
89114
function run {
90115
local test="${1}"
91116
shift 1

scripts/tests.e2e.existing.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function print_separator {
2222
function cleanup {
2323
print_separator
2424
echo "cleaning up reusable network"
25-
ginkgo -v ./tests/e2e/e2e.test -- --stop-network
25+
ginkgo -v --tags test ./tests/e2e/e2e.test -- --stop-network
2626
}
2727
trap cleanup EXIT
2828

scripts/tests.e2e.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ source ./scripts/constants.sh
2323
echo "building e2e.test"
2424
# to install the ginkgo binary (required for test build and run)
2525
go install -v github.com/onsi/ginkgo/v2/[email protected]
26-
ACK_GINKGO_RC=true ginkgo build ./tests/e2e
26+
ACK_GINKGO_RC=true ginkgo build --tags test ./tests/e2e
2727
./tests/e2e/e2e.test --help
2828

2929
# Enable subnet testing by building xsvm
@@ -57,4 +57,4 @@ fi
5757

5858
#################################
5959
# - Execute in random order to identify unwanted dependency
60-
ginkgo ${GINKGO_ARGS} -v --randomize-all ./tests/e2e/e2e.test -- "${E2E_ARGS[@]}" "${@}"
60+
ginkgo ${GINKGO_ARGS} -v --tags test --randomize-all ./tests/e2e/e2e.test -- "${E2E_ARGS[@]}" "${@}"

scripts/tests.upgrade.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ source ./scripts/constants.sh
6666
echo "building upgrade.test"
6767
# to install the ginkgo binary (required for test build and run)
6868
go install -v github.com/onsi/ginkgo/v2/[email protected]
69-
ACK_GINKGO_RC=true ginkgo build ./tests/upgrade
69+
ACK_GINKGO_RC=true ginkgo build --tags test ./tests/upgrade
7070
./tests/upgrade/upgrade.test --help
7171

7272
#################################

snow/consensus/snowball/test_snowflake.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package snowball
57

68
import "testing"

snow/engine/avalanche/bootstrap/queue/test_job.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package queue
57

68
import (

snow/engine/avalanche/bootstrap/queue/test_parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package queue
57

68
import (

snow/engine/avalanche/vertex/test_builder.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package vertex
57

68
import (

snow/engine/avalanche/vertex/test_manager.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package vertex
57

68
import "testing"

snow/engine/avalanche/vertex/test_parser.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package vertex
57

68
import (

snow/engine/avalanche/vertex/test_storage.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package vertex
57

68
import (

snow/engine/avalanche/vertex/test_vm.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package vertex
57

68
import (

snow/engine/common/test_bootstrap_tracker.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/common/test_bootstrapper.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/common/test_engine.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/common/test_sender.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/common/test_timer.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/common/test_vm.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package common
57

68
import (

snow/engine/snowman/block/test_batched_vm.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package block
57

68
import (

snow/engine/snowman/block/test_state_summary.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package block
57

68
import (

snow/engine/snowman/block/test_state_syncable_vm.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package block
57

68
import (

snow/engine/snowman/block/test_vm.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package block
57

68
import (

snow/networking/benchlist/test_benchable.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package benchlist
57

68
import (

snow/networking/sender/test_external_sender.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package sender
57

68
import (

snow/snowtest/snowtest.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package snowtest
57

68
import (

snow/validators/test_state.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
// TODO: https://github.com/ava-labs/avalanchego/issues/3174
5+
//go:build test || !test
6+
47
package validators
58

69
import (

tests/e2e/banff/suites.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
// Implements tests for the banff network upgrade.
57
package banff
68

tests/e2e/c/dynamic_fees.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package c
57

68
import (

tests/e2e/c/interchain_workflow.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4+
//go:build test
5+
46
package c
57

68
import (

0 commit comments

Comments
 (0)