From fb2a97832e2f2a57902f8f929887b653cacfd550 Mon Sep 17 00:00:00 2001
From: Ned Batchelder
Date: Wed, 22 Mar 2023 13:12:16 -0400
Subject: [PATCH] style: clean up #1587
---
CHANGES.rst | 7 ++++---
coverage/html.py | 13 ++++++------
coverage/htmlfiles/coverage_html.js | 3 +--
coverage/htmlfiles/pyfile.html | 6 ++----
coverage/htmlfiles/style.css | 2 --
coverage/htmlfiles/style.scss | 4 ----
tests/gold/html/contexts/two_tests_py.html | 6 +++---
tests/gold/html/styled/style.css | 2 --
tests/gold/html/support/coverage_html.js | 24 ++++++++++++++++++++--
tests/gold/html/support/style.css | 4 +---
10 files changed, 40 insertions(+), 31 deletions(-)
diff --git a/CHANGES.rst b/CHANGES.rst
index 6b12c8fc8..51d6bead2 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -20,9 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------
-- Performance: HTML reports with context information are now much more compact,
- with file sizes typically as small as one-third the previous size. This
- closes `issue 1584`_ thanks to `Oleh Krehel `_.
+- Performance: HTML reports with context information are now much more compact.
+ File sizes are typically as small as one-third the previous size, but can be
+ dramatically smaller. This closes `issue 1584`_ thanks to `Oleh Krehel
+ `_.
.. _issue 1584: https://github.com/nedbat/coveragepy/issues/1584
.. _pull 1587: https://github.com/nedbat/coveragepy/pull/1587
diff --git a/coverage/html.py b/coverage/html.py
index 23fba3f0a..ab2baa9c2 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -5,12 +5,12 @@
from __future__ import annotations
+import collections
import datetime
import json
import os
import re
import shutil
-from collections import Counter
from dataclasses import dataclass
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, cast
@@ -97,6 +97,7 @@ class FileData:
relative_filename: str
nums: Numbers
lines: List[LineData]
+ contexts_json: str
class HtmlDataGeneration:
@@ -370,7 +371,7 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
# Write the HTML page for this file.
file_data = self.datagen.data_for_file(ftr.fr, ftr.analysis)
- contexts = Counter(c for cline in file_data.lines for c in cline.contexts)
+ contexts = collections.Counter(c for cline in file_data.lines for c in cline.contexts)
context_codes = {y: i for (i, y) in enumerate(x[0] for x in contexts.most_common())}
contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
@@ -386,9 +387,9 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
f'{tok_html}'
)
ldata.html = ''.join(html_parts)
-
ldata.context_str = ",".join(
- str(context_codes[c_context]) for c_context in ldata.context_list)
+ str(context_codes[c_context]) for c_context in ldata.context_list
+ )
if ldata.short_annotations:
# 202F is NARROW NO-BREAK SPACE.
@@ -423,9 +424,9 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) ->
ldata.css_class = ' '.join(css_classes) or "pln"
if context_codes:
- file_data.__dict__["contexts_json"] = contexts_json
+ file_data.contexts_json = contexts_json
else:
- file_data.__dict__["contexts_json"] = None
+ file_data.contexts_json = None
html_path = os.path.join(self.directory, ftr.html_filename)
html = self.source_tmpl.render({
**file_data.__dict__,
diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js
index 8b7fb96f1..1cd2b5d4c 100644
--- a/coverage/htmlfiles/coverage_html.js
+++ b/coverage/htmlfiles/coverage_html.js
@@ -604,8 +604,7 @@ coverage.showContexts = function (e) {
span = e.target.nextElementSibling.nextElementSibling;
span_text = span.textContent;
- if (/^[0-9,]+$/.test(span_text))
- {
+ if (/^[0-9,]+$/.test(span_text)) {
span.textContent = "";
span_text.split(",").forEach(function(s) {
ctx = contexts[s];
diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html
index 1921e1c20..bc8fa697d 100644
--- a/coverage/htmlfiles/pyfile.html
+++ b/coverage/htmlfiles/pyfile.html
@@ -14,7 +14,7 @@
{% if contexts_json %}
{% endif %}
@@ -125,9 +125,7 @@
{# Things that should appear below the line. #}
{% if line.context_str %}
-
- {{ line.context_str }}
-
+ {{ line.context_str }}
{% endif %}
{% endjoined %}
diff --git a/coverage/htmlfiles/style.css b/coverage/htmlfiles/style.css
index ace48c2cf..11b24c4e7 100644
--- a/coverage/htmlfiles/style.css
+++ b/coverage/htmlfiles/style.css
@@ -262,8 +262,6 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
@media (prefers-color-scheme: dark) { #source p .ctxs { background: #056; } }
-#source p .ctxs span { display: block; text-align: right; }
-
#index { font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 0.875em; }
#index table.index { margin-left: -.5em; }
diff --git a/coverage/htmlfiles/style.scss b/coverage/htmlfiles/style.scss
index fe3884871..b1465154e 100644
--- a/coverage/htmlfiles/style.scss
+++ b/coverage/htmlfiles/style.scss
@@ -623,10 +623,6 @@ $border-indicator-width: .2em;
border-radius: .25em;
margin-right: 1.75em;
text-align: right;
- span {
- display: block;
- text-align: right;
- }
}
}
}
diff --git a/tests/gold/html/contexts/two_tests_py.html b/tests/gold/html/contexts/two_tests_py.html
index 86239bd88..cbb6e5739 100644
--- a/tests/gold/html/contexts/two_tests_py.html
+++ b/tests/gold/html/contexts/two_tests_py.html
@@ -6,7 +6,7 @@