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 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" 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 Git produces the following hunk:
@@ -3 +3 @@ if a == 0: - c = 3 \ No newline at end of file + c = 30 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 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 reported:
No lines with coverage information in this diff. 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 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 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 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() 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 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.
