Skip to content

Commit

Permalink
chore: allow test-only imports in *test and /tests/** packages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N authored Jul 25, 2024
1 parent 1096c4f commit 7fdb6b6
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 16 deletions.
15 changes: 11 additions & 4 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,32 @@ function test_interface_compliance_nil {

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

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');
IMPORT_FROM_TESTS=$( echo "${NON_TEST_GO_FILES}" | xargs grep -l '"github.com/ava-labs/tests/');
IMPORT_TEST_PKG=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '"github.com/ava-labs/.*?test"');

# 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}" );
HAVE_TEST_LOGIC=$( printf "%s\n%s\n%s\n%s" "${IMPORT_TESTING}" "${IMPORT_TESTIFY}" "${IMPORT_FROM_TESTS}" "${IMPORT_TEST_PKG}" );

TAGGED_AS_TEST=$( echo "${NON_TEST_GO_FILES}" | xargs grep -lP '^\/\/go:build\s+(.+(,|\s+))?test[,\s]?');
IN_TEST_PKG=$( echo "${NON_TEST_GO_FILES}" | grep -P '.*test/[^/]+\.go$' ) # directory (hence package name) ends in "test"

# Files in /tests/ are already excluded by the `find ... ! -path`
INTENDED_FOR_TESTING=$( printf "%s\n%s" "${TAGGED_AS_TEST}" "${IN_TEST_PKG}" )

# -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 ) );
UNTAGGED=$( comm -23 <( echo "${HAVE_TEST_LOGIC}" | sort -u ) <( echo "${INTENDED_FOR_TESTING}" | 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 'Non-test Go files importing test-only packages MUST (a) have '//go:build test' tag; (b) be in *test package; or (c) be in /tests/ directory:';
echo "${UNTAGGED}";
return 1;
}
Expand Down
2 changes: 0 additions & 2 deletions snow/consensus/snowman/snowmantest/block.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

//go:build test

package snowmantest

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

//go:build test

package snowmantest

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

//go:build test

package snowmantest

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

//go:build test

package snowtest

type Status int
Expand Down

0 comments on commit 7fdb6b6

Please sign in to comment.