Skip to content

html: clamp JS-recomputed footer percentage to mirror display_covered - #2259

Open
MsfPablo wants to merge 1 commit into
coveragepy:mainfrom
MsfPablo:fix/2256-html-footer-clamp
Open

html: clamp JS-recomputed footer percentage to mirror display_covered#2259
MsfPablo wants to merge 1 commit into
coveragepy:mainfrom
MsfPablo:fix/2256-html-footer-clamp

Conversation

@MsfPablo

Copy link
Copy Markdown

Fixes #2256.

The HTML index's <tfoot> Total percentage is recomputed in
coverage_html.js with toFixed(), which can round to 0 or 100%
even when actual coverage is not exactly 0 or 100. The <h1> heading
on the same page is server-rendered via coverage.results.display_covered,
which documents the invariant that "0" is only returned when the value
is truly zero, and "100" is only returned when the value is truly 100.
Rounding can never result in either "0" or "100".

Since the JS handler also runs on page load (it dispatches an input
event unconditionally to reapply stored filters; see the comment at
the bottom of the same function), the footer is overwritten on every
page view, so the bug shows up even without typing anything into the
filter.

This patch mirrors display_covered's clamp in JS:

  1. If pc > 0 and pc < 1/10**places, nudge up to 1/10**places.
  2. Else if pc < 100 and pc > 100 - 1/10**places, nudge down to
    100 - 1/10**places.
  3. Else round to the configured precision.

This keeps the JS-recomputed footer from disagreeing with the
server-rendered heading or the text-mode report.


Disclosed: this contribution was prepared with the help of Claude
(a large language model). The human in the loop reviewed the diff and
verified the change matches the invariant documented in
coverage/results.py::display_covered.

The HTML index's <tfoot> Total percentage is recomputed in
coverage_html.js with toFixed(), which can round to 0 or 100 even when
actual coverage is not exactly 0 or 100. The <h1> heading on the same
page is server-rendered via coverage.results.display_covered, which
documents the invariant that "0" is only returned when the value is
truly zero, and "100" is only returned when the value is truly 100.

Mirror display_covered's clamp in JS so the footer can never disagree
with the heading. Fixes coveragepy#2256.
@nedbat

nedbat commented Aug 14, 2026

Copy link
Copy Markdown
Member

#2257 is already working on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML index: footer Total is recomputed in JS with toFixed(), can show 100% when coverage isn't 100%

2 participants