From 6329d31b7df86573f7953a182305e6ba24f44690 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Thu, 16 Mar 2023 17:56:14 +0100 Subject: [PATCH 1/6] Put contexts dict in + {% endif %} + @@ -117,11 +124,9 @@

{% endif %} {# Things that should appear below the line. #} - {% if line.context_list %} + {% if line.context_str %} - {% for context in line.context_list %} - {{context}} - {% endfor %} + {{ line.context_str }} {% endif %}

From e5508692f14978456265a87fc0178fc95977d462 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Fri, 17 Mar 2023 16:31:12 +0100 Subject: [PATCH 2/6] Address review comments --- coverage/html.py | 13 ++++----- coverage/htmlfiles/coverage_html.js | 44 ++++++++++++++--------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/coverage/html.py b/coverage/html.py index 0d6f8a24c..6e249ff55 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -370,7 +370,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 = 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) @@ -387,7 +387,7 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) -> ) ldata.html = ''.join(html_parts) - ldata.context_str = ",".join([str(context_codes[c_context]) for c_context in ldata.context_list]) + ldata.context_str = ",".join(str(context_codes[c_context]) for c_context in ldata.context_list) if ldata.short_annotations: # 202F is NARROW NO-BREAK SPACE. @@ -421,14 +421,13 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) -> ) ldata.css_class = ' '.join(css_classes) or "pln" - di = file_data.__dict__ - if contexts_json == "{}": - di["contexts_json"] = None + if context_codes: + file_data.__dict__["contexts_json"] = contexts_json else: - di["contexts_json"] = contexts_json + file_data.__dict__["contexts_json"] = None html_path = os.path.join(self.directory, ftr.html_filename) html = self.source_tmpl.render({ - **di, + **file_data.__dict__, 'prev_html': prev_html, 'next_html': next_html, }) diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js index ff6bd6a0d..8b7fb96f1 100644 --- a/coverage/htmlfiles/coverage_html.js +++ b/coverage/htmlfiles/coverage_html.js @@ -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') { @@ -596,30 +601,25 @@ coverage.wire_up_sticky_header = function () { }; 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")); - }) - } + 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", () => { - cboxes = document.querySelectorAll('[id^=ctxs]') - cboxes.forEach(function(cbox) { - cbox.addEventListener("click", coverage.showContexts) - }); - - if (document.body.classList.contains("indexfile")) { - coverage.index_ready(); - } else { - coverage.pyfile_ready(); - } + if (document.body.classList.contains("indexfile")) { + coverage.index_ready(); + } else { + coverage.pyfile_ready(); + } }); From 49220af030cdc6635827250990e3f59cc9c61deb Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Fri, 17 Mar 2023 16:49:19 +0100 Subject: [PATCH 3/6] coverage/htmlfiles/style.css: Align the contexts to the right --- coverage/htmlfiles/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coverage/htmlfiles/style.css b/coverage/htmlfiles/style.css index d6768a35e..ace48c2cf 100644 --- a/coverage/htmlfiles/style.css +++ b/coverage/htmlfiles/style.css @@ -258,7 +258,7 @@ 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; } } From 9833e3d6e50c3da843f7d3f6cd59299a78fb3198 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Mon, 20 Mar 2023 11:11:27 +0100 Subject: [PATCH 4/6] coverage/htmlfiles/style.scss: Match style.css --- coverage/htmlfiles/style.scss | 1 + tests/gold/html/styled/style.css | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/coverage/htmlfiles/style.scss b/coverage/htmlfiles/style.scss index 1e9103fd1..fe3884871 100644 --- a/coverage/htmlfiles/style.scss +++ b/coverage/htmlfiles/style.scss @@ -622,6 +622,7 @@ $border-indicator-width: .2em; @include background-dark($dark-context-bg-color); border-radius: .25em; margin-right: 1.75em; + text-align: right; span { display: block; text-align: right; diff --git a/tests/gold/html/styled/style.css b/tests/gold/html/styled/style.css index d6768a35e..ace48c2cf 100644 --- a/tests/gold/html/styled/style.css +++ b/tests/gold/html/styled/style.css @@ -258,7 +258,7 @@ 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; } } From d2ef5c66dd387c704f7d14a0d8b95d2ecf67ccec Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Mon, 20 Mar 2023 21:03:05 +0100 Subject: [PATCH 5/6] coverage/html.py: Fix linting and mypy errors --- coverage/html.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/coverage/html.py b/coverage/html.py index 6e249ff55..23fba3f0a 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -10,7 +10,6 @@ import os import re import shutil -import json from collections import Counter from dataclasses import dataclass @@ -86,6 +85,7 @@ class LineData: short_annotations: List[str] long_annotations: List[str] html: str = "" + context_str: Optional[str] = None annotate: Optional[str] = None annotate_long: Optional[str] = None css_class: str = "" @@ -387,7 +387,8 @@ def write_html_file(self, ftr: FileToReport, prev_html: str, next_html: str) -> ) ldata.html = ''.join(html_parts) - ldata.context_str = ",".join(str(context_codes[c_context]) for c_context in ldata.context_list) + ldata.context_str = ",".join( + str(context_codes[c_context]) for c_context in ldata.context_list) if ldata.short_annotations: # 202F is NARROW NO-BREAK SPACE. From 809266c60459232c3e5c6cc6e36a06b00f31493d Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Wed, 22 Mar 2023 16:14:29 +0100 Subject: [PATCH 6/6] two_tests_py.html: make update-gold --- tests/gold/html/contexts/two_tests_py.html | 29 ++++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/gold/html/contexts/two_tests_py.html b/tests/gold/html/contexts/two_tests_py.html index 5e107b5eb..86239bd88 100644 --- a/tests/gold/html/contexts/two_tests_py.html +++ b/tests/gold/html/contexts/two_tests_py.html @@ -5,6 +5,13 @@ Coverage for two_tests.py: 94% + @@ -65,7 +72,7 @@

» next       coverage.py v7.2.3a0.dev1, - created at 2023-03-21 08:44 -0400 + created at 2023-03-22 16:13 +0100