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
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ run:
# By default, it isn't set.
modules-download-mode: readonly

# Include non-test files tagged as test-only.
# Context: https://github.com/ava-labs/avalanchego/pull/3173
build-tags:
- test

output:
# Make issues output unique by line.
# Default: true
Expand Down
2 changes: 2 additions & 0 deletions cache/test_cacher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package cache

import (
Expand Down
2 changes: 2 additions & 0 deletions chains/atomic/test_shared_memory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package atomic

import (
Expand Down
2 changes: 2 additions & 0 deletions codec/test_codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package codec

import (
Expand Down
2 changes: 2 additions & 0 deletions database/benchmark_database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package database

import (
Expand Down
2 changes: 2 additions & 0 deletions database/test_database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package database

import (
Expand Down
2 changes: 2 additions & 0 deletions ids/test_aliases.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package ids

import "github.com/stretchr/testify/require"
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_fuzz.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ do
echo "Fuzzing $func in $file"
parentDir=$(dirname "$file")
# If any of the fuzz tests fail, return exit code 1
if ! go test "$parentDir" -run="$func" -fuzz="$func" -fuzztime="${fuzzTime}"s; then
if ! go test -tags test "$parentDir" -run="$func" -fuzz="$func" -fuzztime="${fuzzTime}"s; then
failed=true
fi
done
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ fi
TEST_TARGETS="$(eval "go list ./... ${EXCLUDED_TARGETS}")"

# shellcheck disable=SC2086
go test -shuffle=on -race -timeout="${TIMEOUT:-120s}" -coverprofile="coverage.out" -covermode="atomic" ${TEST_TARGETS}
go test -tags test -shuffle=on -race -timeout="${TIMEOUT:-120s}" -coverprofile="coverage.out" -covermode="atomic" ${TEST_TARGETS}
27 changes: 26 additions & 1 deletion scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fi
# by default, "./scripts/lint.sh" runs all lint tests
# to run only "license_header" test
# TESTS='license_header' ./scripts/lint.sh
TESTS=${TESTS:-"golangci_lint license_header require_error_is_no_funcs_as_params single_import interface_compliance_nil require_no_error_inline_func"}
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"}

function test_golangci_lint {
go install -v github.com/golangci/golangci-lint/cmd/golangci-lint@v1.58.1
Expand Down Expand Up @@ -86,6 +86,31 @@ function test_interface_compliance_nil {
fi
}

function test_import_testing_only_in_tests {
ROOT=$( git rev-parse --show-toplevel )
NON_TEST_GO_FILES=$( find "${ROOT}" -iname '*.go' ! -iname '*_test.go');

IMPORT_TESTING=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '^\s*(import\s+)?"testing"');
IMPORT_TESTIFY=$( echo "${NON_TEST_GO_FILES}" | xargs grep -l '"github.com/stretchr/testify');
# TODO(arr4n): send a PR to add support for build tags in `mockgen` and then enable this.
# IMPORT_GOMOCK=$( echo "${NON_TEST_GO_FILES}" | xargs grep -l '"go.uber.org/mock');
HAVE_TEST_LOGIC=$( printf "%s\n%s" "${IMPORT_TESTING}" "${IMPORT_TESTIFY}" );

TAGGED_AS_TEST=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '^\/\/go:build\s+(.+(,|\s+))?test[,\s]?');

# -3 suppresses files that have test logic and have the "test" build tag
# -2 suppresses files that are tagged despite not having detectable test logic
UNTAGGED=$( comm -23 <( echo "${HAVE_TEST_LOGIC}" | sort -u ) <( echo "${TAGGED_AS_TEST}" | sort -u ) );
if [ -z "${UNTAGGED}" ];
then
return 0;
fi

echo "Non-test Go files importing test-only packages MUST have '//go:build test' tag:";
echo "${UNTAGGED}";
return 1;
}

function run {
local test="${1}"
shift 1
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests.e2e.existing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function print_separator {
function cleanup {
print_separator
echo "cleaning up reusable network"
ginkgo -v ./tests/e2e/e2e.test -- --stop-network
ginkgo -v --tags test ./tests/e2e/e2e.test -- --stop-network
}
trap cleanup EXIT

Expand Down
4 changes: 2 additions & 2 deletions scripts/tests.e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ source ./scripts/constants.sh
echo "building e2e.test"
# to install the ginkgo binary (required for test build and run)
go install -v github.com/onsi/ginkgo/v2/ginkgo@v2.13.1
ACK_GINKGO_RC=true ginkgo build ./tests/e2e
ACK_GINKGO_RC=true ginkgo build --tags test ./tests/e2e
./tests/e2e/e2e.test --help

# Enable subnet testing by building xsvm
Expand Down Expand Up @@ -57,4 +57,4 @@ fi

#################################
# - Execute in random order to identify unwanted dependency
ginkgo ${GINKGO_ARGS} -v --randomize-all ./tests/e2e/e2e.test -- "${E2E_ARGS[@]}" "${@}"
ginkgo ${GINKGO_ARGS} -v --tags test --randomize-all ./tests/e2e/e2e.test -- "${E2E_ARGS[@]}" "${@}"
2 changes: 1 addition & 1 deletion scripts/tests.upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ source ./scripts/constants.sh
echo "building upgrade.test"
# to install the ginkgo binary (required for test build and run)
go install -v github.com/onsi/ginkgo/v2/ginkgo@v2.13.1
ACK_GINKGO_RC=true ginkgo build ./tests/upgrade
ACK_GINKGO_RC=true ginkgo build --tags test ./tests/upgrade
./tests/upgrade/upgrade.test --help

#################################
Expand Down
2 changes: 2 additions & 0 deletions snow/consensus/snowball/test_snowflake.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package snowball

import "testing"
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/bootstrap/queue/test_job.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package queue

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/bootstrap/queue/test_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package queue

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/vertex/test_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package vertex

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/vertex/test_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package vertex

import "testing"
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/vertex/test_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package vertex

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/vertex/test_storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package vertex

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/avalanche/vertex/test_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package vertex

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_bootstrap_tracker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_bootstrapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_engine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_sender.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_timer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/common/test_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package common

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/snowman/block/test_batched_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package block

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/snowman/block/test_state_summary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package block

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/snowman/block/test_state_syncable_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package block

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/engine/snowman/block/test_vm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package block

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/networking/benchlist/test_benchable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package benchlist

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/networking/sender/test_external_sender.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package sender

import (
Expand Down
2 changes: 2 additions & 0 deletions snow/snowtest/snowtest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package snowtest

import (
Expand Down
3 changes: 3 additions & 0 deletions snow/validators/test_state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// TODO: https://github.com/ava-labs/avalanchego/issues/3174
//go:build test || !test

package validators

import (
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/banff/suites.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

// Implements tests for the banff network upgrade.
package banff

Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/c/dynamic_fees.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package c

import (
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/c/interchain_workflow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package c

import (
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/faultinjection/duplicate_node_id.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package faultinjection

import (
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/p/interchain_workflow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package p

import (
Expand Down
Loading
Loading