summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2023-03-22 13:12:16 -0400
committerNed Batchelder <ned@nedbatchelder.com>2023-03-22 13:58:58 -0400
commit63f3e495b184ebd9aa4c9d757fb78ed148580d29 (patch)
treeaf4c68f94d50fafe94d56fce46cf92739cd8bd3f
parenta5dc01131b1a38bb7c7908ad968488d87e3664ed (diff)
downloadpython-coveragepy-git-63f3e495b184ebd9aa4c9d757fb78ed148580d29.tar.gz
style: clean up #1587
-rw-r--r--CHANGES.rst7
-rw-r--r--coverage/html.py22
-rw-r--r--coverage/htmlfiles/coverage_html.js3
-rw-r--r--coverage/htmlfiles/pyfile.html6
-rw-r--r--coverage/htmlfiles/style.css2
-rw-r--r--coverage/htmlfiles/style.scss4
-rw-r--r--tests/gold/html/contexts/two_tests_py.html6
-rw-r--r--tests/gold/html/styled/style.css2
-rw-r--r--tests/gold/html/support/coverage_html.js24
-rw-r--r--tests/gold/html/support/style.css4
10 files changed, 43 insertions, 37 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 6b12c8fc..51d6bead 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 <pull 1587_>`_.
+- 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
+ <pull 1587_>`_.
.. _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 23fba3f0..d172e896 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
@@ -370,9 +370,12 @@ class HtmlReporter:
# 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)
+ if context_codes:
+ contexts_json = json.dumps({v: k for (k, v) in context_codes.items()}, indent=2)
+ else:
+ contexts_json = None
for ldata in file_data.lines:
# Build the HTML for the line.
@@ -382,13 +385,11 @@ class HtmlReporter:
html_parts.append(escape(tok_text))
else:
tok_html = escape(tok_text) or '&nbsp;'
- html_parts.append(
- f'<span class="{tok_type}">{tok_html}</span>'
- )
+ html_parts.append(f'<span class="{tok_type}">{tok_html}</span>')
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.
@@ -422,13 +423,10 @@ class HtmlReporter:
)
ldata.css_class = ' '.join(css_classes) or "pln"
- if context_codes:
- file_data.__dict__["contexts_json"] = contexts_json
- else:
- file_data.__dict__["contexts_json"] = None
html_path = os.path.join(self.directory, ftr.html_filename)
html = self.source_tmpl.render({
**file_data.__dict__,
+ "contexts_json": contexts_json,
'prev_html': prev_html,
'next_html': next_html,
})
diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js
index 8b7fb96f..1cd2b5d4 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 1921e1c2..bc8fa697 100644
--- a/coverage/htmlfiles/pyfile.html
+++ b/coverage/htmlfiles/pyfile.html
@@ -14,7 +14,7 @@
{% if contexts_json %}
<script type="text/javascript">
- contexts = {{ contexts_json }}
+ contexts = {{ contexts_json }}
</script>
{% endif %}
@@ -125,9 +125,7 @@
</span>
{# Things that should appear below the line. #}
{% if line.context_str %}
- <span class="ctxs">
- {{ line.context_str }}
- </span>
+ <span class="ctxs">{{ line.context_str }}</span>
{% endif %}
</p>
{% endjoined %}
diff --git a/coverage/htmlfiles/style.css b/coverage/htmlfiles/style.css
index ace48c2c..11b24c4e 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 fe388487..b1465154 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 86239bd8..cbb6e573 100644
--- a/tests/gold/html/contexts/two_tests_py.html
+++ b/tests/gold/html/contexts/two_tests_py.html
@@ -6,7 +6,7 @@
<link rel="icon" sizes="32x32" href="favicon_32.png">
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript">
- contexts = {
+ contexts = {
"0": "(empty)",
"1": "two_tests.test_two",
"2": "two_tests.test_one"
@@ -72,7 +72,7 @@
<a id="nextFileLink" class="nav" href="index.html">&#xbb; next</a>
&nbsp; &nbsp; &nbsp;
<a class="nav" href="https://coverage.readthedocs.io/en/7.2.3a0.dev1">coverage.py v7.2.3a0.dev1</a>,
- created at 2023-03-22 16:13 +0100
+ created at 2023-03-22 13:16 -0400
</p>
<aside class="hidden">
<button type="button" class="button_next_chunk" data-shortcut="j"/>
@@ -118,7 +118,7 @@
<a id="nextFileLink" class="nav" href="index.html">&#xbb; next</a>
&nbsp; &nbsp; &nbsp;
<a class="nav" href="https://coverage.readthedocs.io/en/7.2.3a0.dev1">coverage.py v7.2.3a0.dev1</a>,
- created at 2023-03-22 16:13 +0100
+ created at 2023-03-22 13:16 -0400
</p>
</div>
</footer>
diff --git a/tests/gold/html/styled/style.css b/tests/gold/html/styled/style.css
index ace48c2c..11b24c4e 100644
--- a/tests/gold/html/styled/style.css
+++ b/tests/gold/html/styled/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/tests/gold/html/support/coverage_html.js b/tests/gold/html/support/coverage_html.js
index 084a4970..1cd2b5d4 100644
--- a/tests/gold/html/support/coverage_html.js
+++ b/tests/gold/html/support/coverage_html.js
@@ -166,7 +166,7 @@ coverage.wire_up_filter = function () {
// Trigger change event on setup, to force filter on page refresh
// (filter value may still be present).
- document.getElementById("filter").dispatchEvent(new Event("change"));
+ document.getElementById("filter").dispatchEvent(new Event("input"));
};
coverage.INDEX_SORT_STORAGE = "COVERAGE_INDEX_SORT_2";
@@ -212,6 +212,11 @@ coverage.index_ready = function () {
coverage.LINE_FILTERS_STORAGE = "COVERAGE_LINE_FILTERS";
coverage.pyfile_ready = function () {
+ cboxes = document.querySelectorAll('[id^=ctxs]')
+ cboxes.forEach(function(cbox) {
+ cbox.addEventListener("click", coverage.showContexts)
+ });
+
// If we're directed to a particular line number, highlight the line.
var frag = location.hash;
if (frag.length > 2 && frag[1] === 't') {
@@ -553,7 +558,7 @@ coverage.build_scroll_markers = function () {
'p.show_run, p.show_mis, p.show_exc, p.show_exc, p.show_par'
).forEach(element => {
const line_top = Math.floor(element.offsetTop * marker_scale);
- const line_number = parseInt(element.id.substr(1));
+ const line_number = parseInt(element.querySelector(".n a").id.substr(1));
if (line_number === previous_line + 1) {
// If this solid missed block just make previous mark higher.
@@ -595,10 +600,25 @@ coverage.wire_up_sticky_header = function () {
updateHeader();
};
+coverage.showContexts = function (e) {
+ span = e.target.nextElementSibling.nextElementSibling;
+ span_text = span.textContent;
+
+ if (/^[0-9,]+$/.test(span_text)) {
+ span.textContent = "";
+ span_text.split(",").forEach(function(s) {
+ ctx = contexts[s];
+ span.appendChild(document.createTextNode(ctx));
+ span.appendChild(document.createElement("br"));
+ })
+ }
+};
+
document.addEventListener("DOMContentLoaded", () => {
if (document.body.classList.contains("indexfile")) {
coverage.index_ready();
} else {
coverage.pyfile_ready();
}
+
});
diff --git a/tests/gold/html/support/style.css b/tests/gold/html/support/style.css
index d6768a35..11b24c4e 100644
--- a/tests/gold/html/support/style.css
+++ b/tests/gold/html/support/style.css
@@ -258,12 +258,10 @@ kbd { border: 1px solid black; border-color: #888 #333 #333 #888; padding: .1em
@media (prefers-color-scheme: dark) { #source p label.ctx { color: #777; } }
-#source p .ctxs { display: block; max-height: 0; overflow-y: hidden; transition: all .2s; padding: 0 .5em; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; white-space: nowrap; background: #d0e8ff; border-radius: .25em; margin-right: 1.75em; }
+#source p .ctxs { display: block; max-height: 0; overflow-y: hidden; transition: all .2s; padding: 0 .5em; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; white-space: nowrap; background: #d0e8ff; border-radius: .25em; margin-right: 1.75em; text-align: right; }
@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; }