Skip to content

Commit 9f021fe

Browse files
committed
AppTests: Add tests for RefreshPrinter
Signed-off-by: Gabe Goodhart <[email protected]>
1 parent eb57995 commit 9f021fe

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

scriptit/refresh_printer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
class RefreshPrinter:
3939
__doc__ = __doc__
4040

41+
UP_LINE = "\033[F"
42+
4143
def __init__(
4244
self,
4345
do_refresh: bool = True,
@@ -90,7 +92,7 @@ def refresh(self, force: bool = False):
9092
width = shutil.get_terminal_size().columns
9193
if force or self.refresh_rate == 1 or self.refreshes % self.refresh_rate == 1:
9294
if self.do_refresh and self.last_report is not None and not self.mute:
93-
line_clear = "\033[F" + " " * width
95+
line_clear = self.UP_LINE + " " * width
9496
self.write_stream.write(
9597
line_clear * (len(self.last_report) + 1) + "\r\n"
9698
)

tests/test_refresh_printer.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Tests for RefreshPrinter
3+
"""
4+
# Standard
5+
from unittest import mock
6+
import io
7+
8+
# Third Party
9+
import pytest
10+
11+
# Local
12+
from scriptit import RefreshPrinter
13+
14+
15+
def test_refresh_printer_multi_line():
16+
"""Test that adding multiple lines refreshes correctly"""
17+
stream = io.StringIO()
18+
printer = RefreshPrinter(write_stream=stream)
19+
20+
# Perform initial add and refresh
21+
printer.add("Line one")
22+
printer.add("Line two")
23+
printer.refresh()
24+
assert printer.refreshes == 1
25+
printed_lines = stream.getvalue().split("\n")
26+
assert len(printed_lines) == 3 # Two plus final empty line
27+
assert not any(line.startswith(RefreshPrinter.UP_LINE) for line in printed_lines)
28+
29+
# Perform a second refresh and make sure clearing happens
30+
printer.add("Line three")
31+
printer.add("Line four")
32+
printer.refresh()
33+
assert printer.refreshes == 2
34+
printed_lines = stream.getvalue().split("\n")
35+
assert len(printed_lines) == 6 # First 2, clear, 2 new, final \n
36+
assert printed_lines[2].count(RefreshPrinter.UP_LINE) == 3
37+
38+
39+
def test_refresh_printer_newline_in_add():
40+
"""Make sure that a newline in add is handled as separate lines"""
41+
stream = io.StringIO()
42+
printer = RefreshPrinter(write_stream=stream)
43+
44+
# Perform initial add and refresh
45+
printer.add("Line one\nLine two")
46+
printer.refresh()
47+
assert printer.refreshes == 1
48+
printed_lines = stream.getvalue().split("\n")
49+
assert len(printed_lines) == 3 # Two plus final empty line
50+
assert not any(line.startswith(RefreshPrinter.UP_LINE) for line in printed_lines)
51+
52+
53+
@pytest.mark.parametrize("wrap", [True, False])
54+
def test_refresh_printer_wrap_lines(wrap):
55+
"""Make sure long lines get wrapped if requested"""
56+
term_size_mock = mock.MagicMock()
57+
term_width = 5
58+
term_size_mock.columns = term_width
59+
with mock.patch("shutil.get_terminal_size", return_value=term_size_mock):
60+
stream = io.StringIO()
61+
printer = RefreshPrinter(write_stream=stream)
62+
63+
# Perform initial add and refresh
64+
long_line = "*" * int(2.5 * term_width)
65+
printer.add(long_line, wrap=wrap)
66+
printer.refresh()
67+
printed_lines = stream.getvalue().split("\n")
68+
assert len(printed_lines) == 4 if wrap else 2
69+
70+
71+
def test_clear_last_report_chars():
72+
"""Make sure that shorter lines in an update don't retain characters from
73+
previous reports
74+
"""
75+
stream = io.StringIO()
76+
printer = RefreshPrinter(write_stream=stream)
77+
78+
# Perform initial add and refresh
79+
printer.add("Line one")
80+
printer.refresh()
81+
82+
# Perform a second with a shorter line and make sure that the line does not
83+
# have any of the previous report in it
84+
printer.add("two")
85+
printer.refresh()
86+
printed_lines = stream.getvalue().split("\n")
87+
assert len(printed_lines) == 4 # First 1, clear, 1 new, final \n
88+
assert printed_lines[1].count(RefreshPrinter.UP_LINE) == 2
89+
assert printed_lines[2].strip() == "two"

0 commit comments

Comments
 (0)