Skip to content

Commit a570f5f

Browse files
keithcopybara-github
authored andcommitted
Fix coverage runfiles directory issue
As discovered in bazelbuild#15030 there was another bug with the short circuiting coverage logic where tests were not run from the correct directory. In that PR this issue is fixed directly, with this change we instead make missing LCOV_MERGER be deferred until after all test setup and run, which I think is more future proof to other changes here, and also allows users to know their tests are being run with coverage, and output the correct files, even in the case they don't setup the coverage merger infrastructure. Closes bazelbuild#15031. PiperOrigin-RevId: 434715347
1 parent f8707c0 commit a570f5f

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

src/test/shell/bazel/bazel_coverage_starlark_test.sh

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,22 @@ function test_starlark_rule_without_lcov_merger() {
4040
cat <<EOF > rules.bzl
4141
def _impl(ctx):
4242
output = ctx.actions.declare_file(ctx.attr.name)
43-
ctx.actions.write(output, "", is_executable = True)
44-
return [DefaultInfo(executable=output)]
43+
ctx.actions.write(output, """\
44+
#!/bin/bash
45+
46+
if [[ ! -r extra ]]; then
47+
echo "extra file not found" >&2
48+
exit 1
49+
fi
50+
51+
if [[ -z \$COVERAGE ]]; then
52+
echo "COVERAGE environment variable not set, coverage not run."
53+
exit 1
54+
fi
55+
""", is_executable = True)
56+
extra_file = ctx.actions.declare_file("extra")
57+
ctx.actions.write(extra_file, "extra")
58+
return [DefaultInfo(executable=output, runfiles=ctx.runfiles(files=[extra_file]))]
4559
4660
custom_test = rule(
4761
implementation = _impl,

tools/test/collect_coverage.sh

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,6 @@ if [[ -n "$VERBOSE_COVERAGE" ]]; then
3030
set -x
3131
fi
3232

33-
if [[ -z "$LCOV_MERGER" ]]; then
34-
# this can happen if a rule returns an InstrumentedFilesInfo (which all do
35-
# following 5b216b2) but does not define an _lcov_merger attribute.
36-
# Unfortunately, we cannot simply stop this script being called in this case
37-
# due to conflicts with how things work within Google.
38-
# The file creation is required because TestActionBuilder has already declared
39-
# it.
40-
# TODO(cmita): Improve this situation so this early-exit isn't required.
41-
touch $COVERAGE_OUTPUT_FILE
42-
# Execute the test.
43-
"$@"
44-
TEST_STATUS=$?
45-
exit "$TEST_STATUS"
46-
fi
47-
4833
function resolve_links() {
4934
local name="$1"
5035

@@ -101,15 +86,6 @@ export JAVA_COVERAGE_FILE=$COVERAGE_DIR/jvcov.dat
10186
export COVERAGE=1
10287
export BULK_COVERAGE_RUN=1
10388

104-
105-
for name in "$LCOV_MERGER"; do
106-
if [[ ! -e $name ]]; then
107-
echo --
108-
echo Coverage runner: cannot locate file $name
109-
exit 1
110-
fi
111-
done
112-
11389
# Setting up the environment for executing the C++ tests.
11490
if [[ -z "$GCOV_PREFIX_STRIP" ]]; then
11591
# TODO: GCOV_PREFIX_STRIP=3 is incorrect on MacOS in the default setup
@@ -202,6 +178,24 @@ if [[ "$CC_CODE_COVERAGE_SCRIPT" ]]; then
202178
eval "${CC_CODE_COVERAGE_SCRIPT}"
203179
fi
204180

181+
if [[ -z "$LCOV_MERGER" ]]; then
182+
# this can happen if a rule returns an InstrumentedFilesInfo (which all do
183+
# following 5b216b2) but does not define an _lcov_merger attribute.
184+
# Unfortunately, we cannot simply stop this script being called in this case
185+
# due to conflicts with how things work within Google.
186+
# The file creation is required because TestActionBuilder has already declared
187+
# it.
188+
exit 0
189+
fi
190+
191+
for name in "$LCOV_MERGER"; do
192+
if [[ ! -e $name ]]; then
193+
echo --
194+
echo Coverage runner: cannot locate file $name
195+
exit 1
196+
fi
197+
done
198+
205199
# Export the command line that invokes LcovMerger with the flags:
206200
# --coverage_dir The absolute path of the directory where the
207201
# intermediate coverage reports are located.

0 commit comments

Comments
 (0)