From 051272d4dd590ff889b45098e04bc0d8d3733540 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Thu, 7 Sep 2023 09:10:49 +0200 Subject: [PATCH] Rework coverage report generation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/bazelbuild/bazel/issues/14969 has been fixed, so we don’t need to work around it any more. genhtml accepts multiple tracefiles just fine, so there’s no need to merge them. --- build.py | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/build.py b/build.py index a021d651..8ff31b01 100755 --- a/build.py +++ b/build.py @@ -174,35 +174,29 @@ def coverage(self) -> None: files = re.findall(r'^ (/.+/coverage\.dat)$', output, re.MULTILINE) if not files: raise FileNotFoundError('no coverage files generated') - # Work around https://github.com/bazelbuild/bazel/issues/14969. Run the - # LCov merger binary again, filtering out macOS system headers. + # The tracefiles need some munging for genhtml to accept them. temp_dir = pathlib.Path(tempfile.mkdtemp(prefix='bazel-coverage-')) - report = temp_dir / 'report.txt' - output = temp_dir / 'coverage.info' - with report.open('xt', encoding='utf-8') as stream: - for file in files: - stream.write(file + '\n') - self._bazel('run', ['@bazel_tools//tools/test:lcov_merger', - '--reports_file=' + str(report), - '--output_file=' + str(output), - '--filter_sources=/Applications/.+']) - content = output.read_text('utf-8') - # coverage.py occasionally writes branch coverage data for line 0, which - # genhtml doesn’t accept. - content = re.sub(r'^BRDA:0,.+\n', '', content, flags=re.M) - # Make filenames absolute. - content = re.sub( - r'^SF:([^/].+)$', - 'SF:' + str(self._workspace).replace('\\', r'\\') + r'/\1', - content, flags=re.M) - output.write_text(content, 'utf-8') + outputs: list[pathlib.Path] = [] + for i, file in enumerate(sorted(files)): + content = pathlib.Path(file).read_text('utf-8') + # coverage.py occasionally writes branch coverage data for line 0, + # which genhtml doesn’t accept. + content = re.sub(r'^BRDA:0,.+\n', '', content, flags=re.M) + # Make filenames absolute. + content = re.sub( + r'^SF:([^/].+)$', + 'SF:' + str(self._workspace).replace('\\', r'\\') + r'/\1', + content, flags=re.M) + output = temp_dir / f'coverage-{i:03d}.info' + output.write_text(content, 'utf-8') + outputs.append(output) directory = self._workspace / 'coverage-report' self._run(['genhtml', '--output-directory=' + str(directory), '--branch-coverage', '--demangle-cpp', '--rc=genhtml_demangle_cpp_params=--no-strip-underscore', - '--', str(output)], + '--'] + list(map(str, outputs)), cwd=self._workspace) shutil.rmtree(temp_dir) print(f'coverage report written to {directory}')