Skip to content

Commit 27dc5e1

Browse files
committed
AppTests: Use a resettable StringIO in printer tests
Signed-off-by: Gabe Goodhart <[email protected]>
1 parent d4e1f51 commit 27dc5e1

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Shared testing utilities
3+
"""
4+
5+
# Standard
6+
import io
7+
8+
9+
class ResettableStringIO(io.StringIO):
10+
"""TextIO that can be reset to an empty buffer for sequential outputs"""
11+
12+
def reset(self):
13+
io.StringIO.__init__(self)

tests/test_refresh_printer.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
"""
44
# Standard
55
from unittest import mock
6-
import io
76

87
# Third Party
98
import pytest
109

10+
from tests.conftest import ResettableStringIO
11+
1112
# Local
1213
from scriptit import RefreshPrinter
1314

1415

1516
def test_refresh_printer_multi_line():
1617
"""Test that adding multiple lines refreshes correctly"""
17-
stream = io.StringIO()
18+
stream = ResettableStringIO()
1819
printer = RefreshPrinter(write_stream=stream)
1920

2021
# Perform initial add and refresh
@@ -25,20 +26,21 @@ def test_refresh_printer_multi_line():
2526
printed_lines = stream.getvalue().split("\n")
2627
assert len(printed_lines) == 3 # Two plus final empty line
2728
assert not any(line.startswith(RefreshPrinter.UP_LINE) for line in printed_lines)
29+
stream.reset()
2830

2931
# Perform a second refresh and make sure clearing happens
3032
printer.add("Line three")
3133
printer.add("Line four")
3234
printer.refresh()
3335
assert printer.refreshes == 2
3436
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+
assert len(printed_lines) == 4 # Clear, 2 new, final \n
38+
assert printed_lines[0].count(RefreshPrinter.UP_LINE) == 3
3739

3840

3941
def test_refresh_printer_newline_in_add():
4042
"""Make sure that a newline in add is handled as separate lines"""
41-
stream = io.StringIO()
43+
stream = ResettableStringIO()
4244
printer = RefreshPrinter(write_stream=stream)
4345

4446
# Perform initial add and refresh
@@ -57,7 +59,7 @@ def test_refresh_printer_wrap_lines(wrap):
5759
term_width = 5
5860
term_size_mock.columns = term_width
5961
with mock.patch("shutil.get_terminal_size", return_value=term_size_mock):
60-
stream = io.StringIO()
62+
stream = ResettableStringIO()
6163
printer = RefreshPrinter(write_stream=stream)
6264

6365
# Perform initial add and refresh
@@ -72,18 +74,19 @@ def test_clear_last_report_chars():
7274
"""Make sure that shorter lines in an update don't retain characters from
7375
previous reports
7476
"""
75-
stream = io.StringIO()
77+
stream = ResettableStringIO()
7678
printer = RefreshPrinter(write_stream=stream)
7779

7880
# Perform initial add and refresh
7981
printer.add("Line one")
8082
printer.refresh()
83+
stream.reset()
8184

8285
# Perform a second with a shorter line and make sure that the line does not
8386
# have any of the previous report in it
8487
printer.add("two")
8588
printer.refresh()
8689
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"
90+
assert len(printed_lines) == 3 # Clear, 1 new, final \n
91+
assert printed_lines[0].count(RefreshPrinter.UP_LINE) == 2
92+
assert printed_lines[1].strip() == "two"

0 commit comments

Comments
 (0)