Skip to content

Instantly share code, notes, and snippets.

@kevingessner
Created December 5, 2019 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevingessner/bef02f258db78f60a1ac0048cb9ccb31 to your computer and use it in GitHub Desktop.
Save kevingessner/bef02f258db78f60a1ac0048cb9ccb31 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Checks if all Java and Scala test files in the repository have corresponding Bazel test targets. If not, prints the names of
# the missing ones and exits 1.
#
# Unlike gradle, Bazel does not automatically discover new test files. This script will help us ensure that we don't
# have test files lingering that aren't being executed.
set -euo pipefail
ROOT_DIR=$(bazel info workspace)
all_test_files=$(find "$ROOT_DIR" -path '**/src/test/**/*Test.java' -or -path '**/src/e2e/**/*Test.scala' -or -path '**/src/test/**/*Test.scala' | sort)
known_test_files=$(bazel query --noshow_loading_progress 'kind("source file", filter("Test\.(java|scala)$", labels("srcs", tests(//...))))' | tr ':' '/' | sed "s=//=$ROOT_DIR/=" | sort)
if ! unknown_test_files=$(diff <(echo "$all_test_files") <(echo "$known_test_files") 2>&1)
then
echo "ERROR: One or more test files do not have Bazel test targets, and are not executed. Add targets for the following tests:"
echo "$unknown_test_files"
exit 1
else
echo "All Java and Scala test files have Bazel test targets. Good job!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment