Skip to content

Commit

Permalink
Fix coverage tests with experimental flags
Browse files Browse the repository at this point in the history
Adds the new `test/shell/test_coverage_helper.sh` file, which defines
(amongst other helpers) `COVERAGE_FLAGS` as containing:

- `--experimental_fetch_all_coverage_outputs`
- `--experimental_split_coverage_postprocessing`

This resolves the following error under Bazel 7.4.1 with Bzlmod enabled:

```txt
/mnt/engflow/worker/work/3/exec/bazel-out/
darwin_arm64-opt-exec-ST-f4dfef26580e/bin/external/
bazel_tools~remote_coverage_tools_extension~remote_coverage_tools/Main:
  Cannot locate runfiles directory.
  (Set $JAVA_RUNFILES to inhibit searching.)
```

This error resembles bazelbuild/bazel#20577,
but wasn't due to the presence of `--nobuild_runfile_links`, but to the
lack of the aforementioned `--experimental_*` flags. I learned about
these flags from:

- bazelbuild/bazel#4685 (comment)
- bazelbuild/bazel#16556
  • Loading branch information
mbland committed Jan 10, 2025
1 parent 3fb3ddb commit 7a011b9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 40 deletions.
10 changes: 4 additions & 6 deletions test/shell/test_coverage_equals_in_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_coverage_helper.sh
runner=$(get_test_runner "${1:-local}")

# Default to 2.12.20 for `diff` tests because other versions change the output.
SCALA_VERSION="${SCALA_VERSION:-2.12.20}"
TEST_PKG="test/coverage_filename_encoding"
TARGET="name-with-equals"

test_coverage_target_name_contains_equals_sign() {
bazel coverage \
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
//test/coverage_filename_encoding:name-with-equals
diff test/coverage_filename_encoding/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_filename_encoding/name-with-equals/coverage.dat
do_test_coverage_on
}

$runner test_coverage_target_name_contains_equals_sign
27 changes: 27 additions & 0 deletions test/shell/test_coverage_helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
COVERAGE_FLAGS=(
"--experimental_fetch_all_coverage_outputs"
"--experimental_split_coverage_postprocessing"
)
TESTLOGS="$(bazel info bazel-testlogs)"

# Default to 2.12.20 for `diff` tests because other versions change the output.
SCALA_VERSION="${SCALA_VERSION:-2.12.20}"

_coverage_path() {
echo "$TESTLOGS/$TEST_PKG/$TARGET/coverage.dat"
}

do_test_coverage_on() {
bazel coverage "${COVERAGE_FLAGS[@]}" "${EXTRA_COVERAGE_FLAGS[@]}" \
--repo_env="SCALA_VERSION=${SCALA_VERSION}" "//$TEST_PKG:$TARGET"
diff "${EXPECTED_COVERAGE_PATH:-$TEST_PKG/expected-coverage.dat}" \
"$(_coverage_path)"
}

do_test_coverage_includes_test_targets() {
local test_file="$1"

bazel coverage "${COVERAGE_FLAGS[@]}" "${EXTRA_COVERAGE_FLAGS[@]}" \
--instrument_test_targets=True "//$TEST_PKG:$TARGET"
grep -q "SF:$TEST_PKG/$test_file" "$(_coverage_path)"
}
15 changes: 5 additions & 10 deletions test/shell/test_coverage_scalatest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_coverage_helper.sh
runner=$(get_test_runner "${1:-local}")

# Default to 2.12.20 for `diff` tests because other versions change the output.
SCALA_VERSION="${SCALA_VERSION:-2.12.20}"
TEST_PKG="test/coverage_scalatest"
TARGET="test-scalatest"

test_coverage_on() {
bazel coverage \
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
//test/coverage_scalatest:test-scalatest
diff test/coverage_scalatest/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_scalatest/test-scalatest/coverage.dat
do_test_coverage_on
}

test_coverage_includes_test_targets() {
bazel coverage \
--instrument_test_targets=True \
//test/coverage_scalatest:test-scalatest
grep -q "SF:test/coverage_scalatest/TestWithScalaTest.scala" $(bazel info bazel-testlogs)/test/coverage_scalatest/test-scalatest/coverage.dat
do_test_coverage_includes_test_targets "TestWithScalaTest.scala"
}

$runner test_coverage_on
Expand Down
23 changes: 12 additions & 11 deletions test/shell/test_coverage_scalatest_resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_coverage_helper.sh
runner=$(get_test_runner "${1:-local}")

# Default to 2.12.20 for `diff` tests because other versions change the output.
SCALA_VERSION="${SCALA_VERSION:-2.12.20}"
TEST_PKG="test/coverage_scalatest_resources/consumer"
TARGET="tests"

test_coverage_succeeds_resource_call() {
bazel coverage \
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
--instrumentation_filter=^//test/coverage_scalatest_resources[:/] \
//test/coverage_scalatest_resources/consumer:tests
diff test/coverage_scalatest_resources/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_scalatest_resources/consumer/tests/coverage.dat
local EXTRA_COVERAGE_FLAGS=(
"--instrumentation_filter=^//test/coverage_scalatest_resources[:/]"
)
local EXPECTED_COVERAGE_PATH=(
"test/coverage_scalatest_resources/expected-coverage.dat"
)
do_test_coverage_on
}

test_coverage_includes_resource_test_targets() {
bazel coverage \
--instrument_test_targets=True \
//test/coverage_scalatest_resources/consumer:tests
grep -q "SF:test/coverage_scalatest_resources/consumer/src/test/scala/com/example/consumer/ConsumerSpec.scala" $(bazel info bazel-testlogs)/test/coverage_scalatest_resources/consumer/tests/coverage.dat
do_test_coverage_includes_test_targets \
"src/test/scala/com/example/consumer/ConsumerSpec.scala"
}

$runner test_coverage_succeeds_resource_call
Expand Down
16 changes: 5 additions & 11 deletions test/shell/test_coverage_specs2_with_junit.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
# shellcheck source=./test_runner.sh
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_coverage_helper.sh
runner=$(get_test_runner "${1:-local}")

# Default to 2.12.20 for `diff` tests because other versions change the output.
SCALA_VERSION="${SCALA_VERSION:-2.12.20}"
TEST_PKG="test/coverage_specs2_with_junit"
TARGET="test-specs2-with-junit"

test_coverage_on() {
bazel coverage \
--repo_env="SCALA_VERSION=${SCALA_VERSION}" \
//test/coverage_specs2_with_junit:test-specs2-with-junit
diff test/coverage_specs2_with_junit/expected-coverage.dat $(bazel info bazel-testlogs)/test/coverage_specs2_with_junit/test-specs2-with-junit/coverage.dat
do_test_coverage_on
}

test_coverage_includes_test_targets() {
bazel coverage \
--instrument_test_targets=True \
//test/coverage_specs2_with_junit:test-specs2-with-junit
grep -q "SF:test/coverage_specs2_with_junit/TestWithSpecs2WithJUnit.scala" $(bazel info bazel-testlogs)/test/coverage_specs2_with_junit/test-specs2-with-junit/coverage.dat
do_test_coverage_includes_test_targets "TestWithSpecs2WithJUnit.scala"
}

$runner test_coverage_on
Expand Down
9 changes: 7 additions & 2 deletions test/shell/test_scala_jacocorunner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. "${dir}"/test_runner.sh
. "${dir}"/test_helper.sh
. "${dir}"/test_coverage_helper.sh
runner=$(get_test_runner "${1:-local}")

test_scala_jacocorunner_from_scala_toolchain_passes() {
bazel coverage --extra_toolchains="//manual_test/scala_test_jacocorunner:passing_scala_toolchain" //manual_test/scala_test_jacocorunner:empty_test
bazel coverage "${COVERAGE_FLAGS[@]}" \
--extra_toolchains="//manual_test/scala_test_jacocorunner:passing_scala_toolchain" \
//manual_test/scala_test_jacocorunner:empty_test
}

test_scala_jacocorunner_from_scala_toolchain_fails() {
action_should_fail coverage --extra_toolchains="//test_expect_failure/scala_test_jacocorunner:failing_scala_toolchain" //test_expect_failure/scala_test_jacocorunner:empty_test
action_should_fail coverage "${COVERAGE_FLAGS[@]}" \
--extra_toolchains="//test_expect_failure/scala_test_jacocorunner:failing_scala_toolchain" \
//test_expect_failure/scala_test_jacocorunner:empty_test
}

#Jacocoa support not implemented for windows just yet
Expand Down

0 comments on commit 7a011b9

Please sign in to comment.