Skip to content

Incorrect Line Coverage Parsing #310

@TaylorCarpenter

Description

@TaylorCarpenter

When using GCov, the "lines covered by test" is not calculated properly. In gcov.py, after the coverage report is parsed, there is the following post-processing done:

def _corrected_lines(self,
                         relative_filename: str,
                         lines: set[int],
                         ) -> set[int]:
        if os.path.isabs(relative_filename):
            absolute_filename = relative_filename
        else:
            absolute_filename = os.path.join(self._source_directory, relative_filename)

        instrumented_filenames = set(f.filename for f in self._files_to_instrument)
        if absolute_filename not in instrumented_filenames:
            logger.trace(f"file was not instrumented: {absolute_filename}")
            return lines

        lines = lines - _LINES_TO_REMOVE
        return set(i - _NUM_INSTRUMENTATION_LINES for i in lines)

The intent is to account for the instrumentation code that is added, so that the "lines" refers to the location is the original file. The problem is _NUM_INSTRUMENTATION_LINES is always being subtracted from the entries, even if the relevant lines come before the instrumentation code (and thus are not moved).

This offset handling is only correct if the instrumentation code is put at the very beginning of the file. Given that the location at which the instrumentation code is injected can be changed (and if fact by default the code is being placed at the end of the file), the offset correction should only be applied to lines that come after the instrumentation injection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions