| #!/bin/bash |
| # Copyright 2026 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # This script ensures that every Go test file in the project imports the |
| # 'testsetup' package. This is required to ensure that glog and other |
| # filesystem-dependent resources are correctly isolated in parallel test runs. |
| |
| set -e |
| |
| PROJECT_ROOT="$(git rev-parse --show-toplevel)" |
| TESTSETUP_PKG="github.com/bazelbuild/rsclient/internal/pkg/testsetup" |
| |
| MISSING=() |
| # Search for all Go test files, excluding third_party and the testsetup package itself. |
| for f in $(find "${PROJECT_ROOT}/internal" "${PROJECT_ROOT}/cmd" -name "*_test.go" | grep -v "/testsetup/"); do |
| if ! grep -q "\"${TESTSETUP_PKG}\"" "$f"; then |
| MISSING+=("$f") |
| fi |
| done |
| |
| if [ ${#MISSING[@]} -ne 0 ]; then |
| echo "ERROR: The following Go test files are missing the mandatory isolation shim import:" |
| for f in "${MISSING[@]}"; do |
| # Strip the absolute path for cleaner output. |
| echo " ${f#${PROJECT_ROOT}/}" |
| done |
| echo "" |
| echo "RATIONALE:" |
| echo " Parallel test runs (e.g., --runs_per_test) can collide on shared filesystem" |
| echo " resources like /tmp/glog.INFO. These collisions cause silent 'Exit 2' crashes." |
| echo " The 'testsetup' package contains an init() shim that redirects these resources" |
| echo " to a private, isolated directory (TEST_TMPDIR)." |
| echo "" |
| echo "FIX:" |
| echo " Add a blank import to the top of each missing file:" |
| echo " import _ \"${TESTSETUP_PKG}\"" |
| echo "" |
| exit 1 |
| fi |
| |
| echo "All Go test files correctly import the isolation shim." |
| exit 0 |