Skip to content

Commit 74b1758

Browse files
committedApr 27, 2023
test: refactor and add a test of terminal link text
1 parent 3e9205f commit 74b1758

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed
 

‎coverage/html.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import re
1414
import shutil
1515
import string # pylint: disable=deprecated-module
16-
import sys
1716

1817
from dataclasses import dataclass
1918
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING, cast
@@ -23,7 +22,7 @@
2322
from coverage.exceptions import NoDataError
2423
from coverage.files import flat_rootname
2524
from coverage.misc import ensure_dir, file_be_gone, Hasher, isolate_module, format_local_datetime
26-
from coverage.misc import human_sorted, plural
25+
from coverage.misc import human_sorted, plural, stdout_link
2726
from coverage.report import get_analysis_to_report
2827
from coverage.results import Analysis, Numbers
2928
from coverage.templite import Templite
@@ -495,13 +494,8 @@ def index_file(self, first_html: str, final_html: str) -> None:
495494
index_file = os.path.join(self.directory, "index.html")
496495
write_html(index_file, html)
497496

498-
if sys.stdout.isatty():
499-
file_path = f"file://{os.path.abspath(index_file)}"
500-
print_path = f"\033]8;;{file_path}\a{index_file}\033]8;;\a"
501-
else:
502-
print_path = index_file
503-
504-
self.coverage._message(f"Wrote HTML report to {print_path}")
497+
print_href = stdout_link(index_file, f"file://{os.path.abspath(index_file)}")
498+
self.coverage._message(f"Wrote HTML report to {print_href}")
505499

506500
# Write the latest hashes for next time.
507501
self.incr.write()

‎coverage/misc.py

+12
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,15 @@ def plural(n: int, thing: str = "", things: str = "") -> str:
386386
return thing
387387
else:
388388
return things or (thing + "s")
389+
390+
391+
def stdout_link(text: str, url: str) -> str:
392+
"""Format text+url as a clickable link for stdout.
393+
394+
If attached to a terminal, use escape sequences. Otherwise, just return
395+
the text.
396+
"""
397+
if sys.stdout.isatty():
398+
return f"\033]8;;{url}\a{text}\033]8;;\a"
399+
else:
400+
return text

‎tests/test_misc.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from __future__ import annotations
77

88
import sys
9+
from unittest import mock
910

1011
import pytest
1112

1213
from coverage.exceptions import CoverageException
1314
from coverage.misc import file_be_gone
1415
from coverage.misc import Hasher, substitute_variables, import_third_party
15-
from coverage.misc import human_sorted, human_sorted_items
16+
from coverage.misc import human_sorted, human_sorted_items, stdout_link
1617

1718
from tests.coveragetest import CoverageTest
1819

@@ -153,3 +154,14 @@ def test_human_sorted_items(words: str, ordered: str) -> None:
153154
oitems = [(k, v) for k in okeys for v in [1, 2]]
154155
assert human_sorted_items(items) == oitems
155156
assert human_sorted_items(items, reverse=True) == oitems[::-1]
157+
158+
159+
def test_stdout_link_tty() -> None:
160+
with mock.patch.object(sys.stdout, "isatty", lambda:True):
161+
link = stdout_link("some text", "some url")
162+
assert link == "\033]8;;some url\asome text\033]8;;\a"
163+
164+
165+
def test_stdout_link_not_tty() -> None:
166+
# Without mocking isatty, it reports False in a pytest suite.
167+
assert stdout_link("some text", "some url") == "some text"

0 commit comments

Comments
 (0)
Please sign in to comment.