@@ -20,6 +20,8 @@ from typing import List
20
20
import pytest
21
21
# None means coverage wasn't enabled
22
22
cov = None
23
+ # For workaround of https://github.com/nedbat/coveragepy/issues/963
24
+ coveragepy_absfile_mapping = {}
23
25
24
26
# Since our py_test had InstrumentedFilesInfo, we know Bazel will hand us this environment variable.
25
27
# https://bazel.build/rules/lib/providers/InstrumentedFilesInfo
@@ -28,9 +30,13 @@ if "COVERAGE_MANIFEST" in os.environ:
28
30
import coverage
29
31
# The lines are files that matched the --instrumentation_filter flag
30
32
with open(os.getenv("COVERAGE_MANIFEST"), "r") as mf:
31
- cov = coverage.Coverage(include = mf.read().splitlines())
33
+ manifest_entries = mf.read().splitlines()
34
+ cov = coverage.Coverage(include = manifest_entries)
35
+ # coveragepy incorrectly converts our entries by following symlinks
36
+ # record a mapping of their conversion so we can undo it later in reporting the coverage
37
+ coveragepy_absfile_mapping = {coverage.files.abs_file(mfe): mfe for mfe in manifest_entries}
32
38
cov.start()
33
- except Exception as e:
39
+ except ModuleNotFoundError as e:
34
40
print("WARNING: python coverage setup failed. Do you need to include the 'coverage' library as a dependency of py_pytest_main?", e)
35
41
pass
36
42
@@ -95,15 +101,22 @@ if __name__ == "__main__":
95
101
# https://bazel.build/configure/coverage
96
102
coverage_output_file = os.getenv("COVERAGE_OUTPUT_FILE")
97
103
98
- # Workaround https://github.com/bazelbuild/bazel/issues/25118
99
- # by removing 'end line number' from FN: records
100
104
unfixed_dat = coverage_output_file + ".tmp"
101
105
cov.lcov_report(outfile = unfixed_dat)
102
106
cov.save()
103
107
104
108
with open(unfixed_dat, "r") as unfixed:
105
109
with open(coverage_output_file, "w") as output_file:
106
110
for line in unfixed:
111
+ # Workaround https://github.com/nedbat/coveragepy/issues/963
112
+ # by mapping SF: records to un-do the symlink-following
113
+ if line.startswith('SF:'):
114
+ sourcefile = line[3:].rstrip()
115
+ if sourcefile in coveragepy_absfile_mapping:
116
+ output_file.write(f"SF:{coveragepy_absfile_mapping[sourcefile]}\n")
117
+ continue
118
+ # Workaround https://github.com/bazelbuild/bazel/issues/25118
119
+ # by removing 'end line number' from FN: records
107
120
if line.startswith('FN:'):
108
121
parts = line[3:].split(",") # Remove 'FN:' and split by commas
109
122
if len(parts) == 3:
0 commit comments