DEV Community

Tohru
Tohru

Posted on Edited on

How a Git Diff Marker Can Hide Uncovered Code

In diff-cover 11.0.3, a coverage check could pass even when the only changed line had never been executed.

The bug was triggered by a marker Git adds when a file lacks a final newline:

\ No newline at end of file
Enter fullscreen mode Exit fullscreen mode

Counting this marker as a source line shifted the line numbers used by the coverage check.

Reproducing the Problem

With Git and Coverage.py installed, create a small repository:

mkdir diff-cover-demo
cd diff-cover-demo
git init

printf 'a = 1\nif a == 0:\n    c = 3' > file.py
git add file.py
git -c user.name="Demo" -c user.email="demo@example.invalid" commit -m "Initial version"
Enter fullscreen mode Exit fullscreen mode

The last line intentionally has no terminating newline.

Change its value and add the newline:

printf 'a = 1\nif a == 0:\n    c = 30\n' > file.py
git diff -U0 -- file.py
Enter fullscreen mode Exit fullscreen mode

Git produces the following hunk:

@@ -3 +3 @@ if a == 0:
-    c = 3
\ No newline at end of file
+    c = 30
Enter fullscreen mode Exit fullscreen mode

The backslash-prefixed marker describes the preceding line. It doesn't correspond to a line in either version of the file.

Now generate a coverage report:

python -m coverage run file.py
python -m coverage xml -o coverage.xml
Enter fullscreen mode Exit fullscreen mode

Since a is 1, the condition is false and line 3 is never executed.

The generated XML contains these hit counts:

Line Hits
1 1
2 1
3 0

Line 3 is both changed and uncovered.

However, in the reproduction documented in diff-cover PR #658, running:

diff-cover coverage.xml --fail-under 100
Enter fullscreen mode Exit fullscreen mode

reported:

No lines with coverage information in this diff.
Enter fullscreen mode Exit fullscreen mode

The command exited with status 0.

To reproduce this check in the local repository created above, use:

diff-cover coverage.xml --compare-branch HEAD --fail-under 100
Enter fullscreen mode Exit fullscreen mode

Where the Line Number Went Wrong

The diff parser maintains separate line counters for the old and new versions of a file.

Each type of line inside a diff hunk affects these counters differently:

Prefix Meaning Counter to advance
+ Addition New file
- Deletion Old file
Space Unchanged context Both
\ Missing-newline marker Neither

In diff_cover/diff_reporter.py, the original _parse_lines() method handled additions and deletions explicitly. Unchanged context lines reached this branch:

else:
    if current_line_old is not None:
        current_line_old += 1

    if current_line_new is not None:
        current_line_new += 1
Enter fullscreen mode Exit fullscreen mode

The missing-newline marker also reached this branch.

In our example, the new-file counter starts at 3. Processing the removed line leaves it unchanged, but processing the marker incorrectly increments it to 4.

When the parser reaches the added line, it records line 4 instead of line 3.

The fix in PR #658 adds a branch before the context-line handling:

elif line.startswith("\\"):
    continue
Enter fullscreen mode Exit fullscreen mode

The parser skips the marker without advancing either counter.

How the Error Affected Coverage

diff-cover intersects the changed-line numbers reported by its parser with the measured and uncovered lines from the coverage report.

For this example, the calculation with the incorrect line number can be reduced to:

changed_lines = {4}
measured_lines = {1, 2, 3}
uncovered_lines = {3}

changed_lines & measured_lines   # set()
changed_lines & uncovered_lines  # set()
Enter fullscreen mode Exit fullscreen mode

Neither intersection contains any lines.

In report_generator.py, diff-cover returns 100% when there are no measured changed lines. That percentage satisfies the --fail-under 100 threshold.

The following comparison uses an isolated reproduction of the parsing and coverage-calculation logic from versions 11.0.3 and 11.0.4, with the Git diff and XML report generated above on Linux using Python 3.13.5, Git 2.47.3 and Coverage.py 7.13.3.

Original logic Fixed logic
Changed lines [4] [3]
Measured changed lines [] [3]
Uncovered changed lines [] [3]
Calculated coverage 100% 0%

Once the parser reports line 3 correctly, the uncovered line contributes to the coverage calculation.

The fix was released in diff-cover 11.0.4.

Testing the Position of the Marker

The upstream regression test covers four hunk layouts. Running the same inputs against the isolated parser logic produces these results:

Scenario Before After
Last line modified [4] [3]
Last two lines modified [3, 4] [2, 3]
New line appended [4, 5] [3, 4]
Last line modified and final newline removed [3] [3]

In the first three cases, the marker appears before one or more added lines, shifting the reported line numbers.

The last case is a useful control: the marker appears after the added line, so its line number has already been recorded. The original parser gets this case right.

The upstream test passes complete diff fixtures to the reporter and checks the exact changed-line numbers:

assert diff.lines_changed("file.py") == expected
Enter fullscreen mode Exit fullscreen mode

An assertion that only counted the changed lines would miss the bug. Both [3] and [4] contain one element, but only one identifies the actual change.


References

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Tohru, the table that does the most work for me is the before/after one: changed lines [4] versus [3], measured changed lines [] versus [3], and calculated coverage 100% versus 0% — one phantom line flips a failing check into a passing one. The part I would underline is the second failure hiding behind the first: report_generator.py returns 100% when there are no measured changed lines, so the parser bug never had to produce a wrong percentage, only an empty set. That makes me wonder about the longer-file version of your example: in your 3-line file, line 4 does not exist in the measured set, so the intersection is empty. In a longer file, could the shifted line number land on a real, covered line further down and report the change as covered instead of uncovered? Your four hunk layouts cover the marker positions, but do any of them test that collision — a phantom line that actually exists and is covered? That is the case where this stops being a false pass on empty data and starts attributing the wrong line's coverage to the change.