From 0c242ca7b67ea4c0c1f75e914b9cfe4cd448adac Mon Sep 17 00:00:00 2001 From: Mike Bland Date: Mon, 10 Mar 2025 18:51:11 -0400 Subject: [PATCH] Fix silently broken `test_version.sh` tests Fixes silently broken tests depending on `test_version/test_reporter` and adds an assertion to avoid future silent breakages. Part of #1482 and #1652. - Moves `test_version/version_specific_tests_dir/scala_repositories.bzl` to `test_version`. - Updates `test_version.sh` to copy this file into test repos generated from both `version_specific_tests_dir` and `test_reporter`. - Updates `compilation_should_fail` to break if the `bazel build` output doesn't contain the expected `ErrorFile.scala` error. The affected test cases expect their underlying builds to fail because of errors in `test_version/test_reporter/ErrorFile.scala`. Failing Scala 2.x build output should contain: ```txt ErrorFile.scala:6: ')' expected but '}' found ``` Failing Scala 3.x build output should contain: ```txt -- [E040] Syntax Error: ErrorFile.scala:6:2 ------------------------------------ 6 | } | ^ | ')' expected, but '}' found ``` Using `RULES_SCALA_TEST_ONLY` to select an affected test case revealed that the underlying build actually failed because Bazel couldn't find `//:scrooge_repositories.bzl`: ```txt $ RULES_SCALA_TEST_ONLY="test_reporter 2.12.20 //:diagnostics_reporter_toolchain" \ ./test_version.sh running test test_reporter 2.12.20 //:diagnostics_reporter_toolchain Starting local Bazel server and connecting to it... Computing main repo mapping: ERROR: Error computing the main repository mapping: cannot load '//:scrooge_repositories.bzl': no such file $ echo $? 0 ``` --- test_version.sh | 23 +++++++++++++++---- .../scrooge_repositories.bzl | 0 2 files changed, 19 insertions(+), 4 deletions(-) rename test_version/{version_specific_tests_dir => }/scrooge_repositories.bzl (100%) diff --git a/test_version.sh b/test_version.sh index 2ca1ee6e6..3c813fe00 100755 --- a/test_version.sh +++ b/test_version.sh @@ -16,11 +16,26 @@ compilation_should_fail() { # runs the tests locally set +e TEST_ARG=$@ - DUMMY=$(bazel $TEST_ARG) + OUTPUT="$(bazel $TEST_ARG 2>&1)" RESPONSE_CODE=$? + set -e + if [ $RESPONSE_CODE -eq 0 ]; then echo -e "${RED} \"bazel $TEST_ARG\" should have failed but passed. $NC" + echo "$OUTPUT" return -1 + fi + + local expected_error_pattern=( + "ErrorFile\.scala:6:[[:print:][:space:]]*'[)]' expected,? but '[}]' found" + ) + + if [[ ! "$OUTPUT" =~ $expected_error_pattern ]]; then + echo -e "${RED} \"bazel $*\" failure should have matched:" + echo -e " ${expected_error_pattern}" + echo -e " got:${NC}" + echo "$OUTPUT" + return 1 else return 0 fi @@ -45,12 +60,12 @@ run_in_test_repo() { if [[ -n "$TWITTER_SCROOGE_VERSION" ]]; then local version_param="version = \"$TWITTER_SCROOGE_VERSION\"" - scrooge_ws="$version_param" + scrooge_ws="$version_param\\n" fi - sed -e "s%\${twitter_scrooge_repositories}%${scrooge_ws}\n%" \ + sed -e "s%\${twitter_scrooge_repositories}%${scrooge_ws}%" \ WORKSPACE.template >> $NEW_TEST_DIR/WORKSPACE - cp ../.bazel{rc,version} $NEW_TEST_DIR/ + cp ../.bazel{rc,version} scrooge_repositories.bzl $NEW_TEST_DIR/ cd $NEW_TEST_DIR diff --git a/test_version/version_specific_tests_dir/scrooge_repositories.bzl b/test_version/scrooge_repositories.bzl similarity index 100% rename from test_version/version_specific_tests_dir/scrooge_repositories.bzl rename to test_version/scrooge_repositories.bzl