Skip to content

Commit ebbcb6a

Browse files
authored
fix(coverage): generating lcov was causing issues (bazel-contrib#1734)
* examples/bzlmod - bazel coverage //tests:version_3_10_takes_3_9_subprocess_test was always failing due to .coveragerc generated file not being unique * generating the locv report resulted in messages going to stdout/stderr that resulted in test failures. To fix this, we run with --quiet. If VERBOSE_COVERAGE is defined we will output to stderr. Reproduction steps: ```bash git co main cd examples/bzlmod bazel coverage //tests/... # failures occur ```
1 parent e7f8f0f commit ebbcb6a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

.bazelci/presubmit.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ buildifier:
5959
build_targets: ["..."]
6060
test_targets: ["..."]
6161
.coverage_targets_example_bzlmod: &coverage_targets_example_bzlmod
62-
coverage_targets: ["//:test"]
62+
coverage_targets: ["..."]
6363
.coverage_targets_example_bzlmod_build_file_generation: &coverage_targets_example_bzlmod_build_file_generation
6464
coverage_targets: ["//:bzlmod_build_file_generation_test"]
6565
.coverage_targets_example_multi_python: &coverage_targets_example_multi_python
@@ -218,7 +218,7 @@ tasks:
218218
integration_test_bzlmod_ubuntu_min:
219219
<<: *minimum_supported_version
220220
<<: *reusable_build_test_all
221-
<<: *coverage_targets_example_bzlmod
221+
coverage_targets: ["//:test"]
222222
name: "examples/bzlmod: Ubuntu, minimum Bazel"
223223
working_directory: examples/bzlmod
224224
platform: ubuntu2004

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ A brief description of the categories of changes:
3838
* (bzlmod) pip.parse now does not fail with an empty `requirements.txt`.
3939
* (py_wheel) Wheels generated by `py_wheel` now preserve executable bits when
4040
being extracted by `installer` and/or `pip`.
41+
* (coverage) During the running of lcov, the stdout/stderr was causing test
42+
failures. By default, suppress output when generating lcov. This can be
43+
overridden by setting 'VERBOSE_COVERAGE'. This change only affect bazel
44+
7.x.x and above.
4145

4246
### Added
4347

python/private/python_bootstrap_template.txt

+19-12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ del sys.path[0]
2020

2121
import os
2222
import subprocess
23+
import uuid
2324

2425
def IsRunningFromZip():
2526
return %is_zipfile%
@@ -93,6 +94,10 @@ def PrintVerboseCoverage(*args):
9394
if os.environ.get("VERBOSE_COVERAGE"):
9495
print(*args, file=sys.stderr)
9596

97+
def IsVerboseCoverage():
98+
"""Returns True if VERBOSE_COVERAGE is non-empty in the environment."""
99+
return os.environ.get("VERBOSE_COVERAGE")
100+
96101
def FindCoverageEntryPoint(module_space):
97102
cov_tool = '%coverage_tool%'
98103
if cov_tool:
@@ -389,8 +394,8 @@ def _RunForCoverage(python_program, main_filename, args, env,
389394
runfiles directory if set.
390395
"""
391396
# We need for coveragepy to use relative paths. This can only be configured
392-
# via an rc file, so we need to make one.
393-
rcfile_name = os.path.join(os.environ['COVERAGE_DIR'], '.coveragerc')
397+
unique_id = uuid.uuid4()
398+
rcfile_name = os.path.join(os.environ['COVERAGE_DIR'], ".coveragerc_{}".format(unique_id))
394399
with open(rcfile_name, "w") as rcfile:
395400
rcfile.write('''[run]
396401
relative_files = True
@@ -415,18 +420,20 @@ relative_files = True
415420

416421
PrintVerboseCoverage('Converting coveragepy database to lcov:', output_filename)
417422
# Run coveragepy again to convert its .coverage database file into lcov.
423+
# Under normal conditions running lcov outputs to stdout/stderr, which causes problems for `coverage`.
424+
params = [python_program, coverage_entrypoint, "lcov", "--rcfile=" + rcfile_name, "-o", output_filename, "--quiet"]
425+
kparams = {"env": env, "cwd": workspace, "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
426+
if IsVerboseCoverage():
427+
# reconnect stdout/stderr to lcov generation. Should be useful for debugging `coverage` issues.
428+
params.remove("--quiet")
429+
kparams['stdout'] = sys.stderr
430+
kparams['stderr'] = sys.stderr
431+
418432
ret_code = subprocess.call(
419-
[
420-
python_program,
421-
coverage_entrypoint,
422-
"lcov",
423-
"--rcfile=" + rcfile_name,
424-
"-o",
425-
output_filename
426-
],
427-
env=env,
428-
cwd=workspace
433+
params,
434+
**kparams
429435
) or ret_code
436+
430437
try:
431438
os.unlink(rcfile_name)
432439
except OSError as err:

0 commit comments

Comments
 (0)