-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix animation of long lines #247
Changes from 13 commits
a26b2bb
0fefbf2
4bcce89
6bfe12c
c7ef9d8
4f31d46
58cbefb
9fdf83f
aec4c98
9e1eb97
29a058a
f3aa77a
c9be0a7
73b861f
49228e5
9f60dc5
443b621
f60e310
8f89f97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is great! Thanks for adding these tests. |
||
|
||
import pipx.animate | ||
from pipx.animate import ( | ||
HIDE_CURSOR, | ||
CLEAR_LINE, | ||
EMOJI_ANIMATION_FRAMES, | ||
NONEMOJI_ANIMATION_FRAMES, | ||
EMOJI_FRAME_PERIOD, | ||
NONEMOJI_FRAME_PERIOD, | ||
) | ||
|
||
|
||
def check_animate_output( | ||
capsys, test_string, frame_strings, frame_period, frames_to_test | ||
): | ||
expected_string = f"{HIDE_CURSOR}" + "".join(frame_strings) | ||
|
||
chars_to_test = 1 + len("".join(frame_strings[:frames_to_test])) | ||
|
||
with pipx.animate.animate(test_string, do_animation=True): | ||
time.sleep(frame_period * (frames_to_test - 1) + 0.1) | ||
captured = capsys.readouterr() | ||
|
||
# print for debug help if fail | ||
print("expected_string:") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding '-rvva --showlocals`` achieves the same, not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, didn't know about that. |
||
print(repr(expected_string[:chars_to_test])) | ||
print("capsys sterr:") | ||
print(repr(captured.err[:chars_to_test])) | ||
|
||
assert captured.err[:chars_to_test] == expected_string[:chars_to_test] | ||
|
||
|
||
def test_line_lengths_emoji(capsys, monkeypatch): | ||
# emoji_support and stderr_is_tty is set only at import animate.py | ||
# since we are already after that, we must override both here | ||
pipx.animate.stderr_is_tty = True | ||
pipx.animate.emoji_support = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend defining a fixture to ensure pipx.animate.stderr_is_tty, pipx.animate.emoji_support values are stored at entry to the fixture, then https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, for the hint. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used @gaborbernat 's tip about monkeypatch instead, but if things get more complicated in the future I am happy to know how to do teardown. |
||
|
||
frames_to_test = 4 | ||
# matches animate.py | ||
frame_period = EMOJI_FRAME_PERIOD | ||
|
||
# 40-char test_string counts columns e.g.: "0204060810 ... 363840" | ||
test_string = "".join([f"{x:02}" for x in range(2, 41, 2)]) | ||
|
||
columns_to_test = [45, 46, 47] | ||
expected_message = [f"{test_string:.{45-6}}...", f"{test_string}", f"{test_string}"] | ||
|
||
for i, columns in enumerate(columns_to_test): | ||
monkeypatch.setenv("COLUMNS", str(columns)) | ||
|
||
frame_strings = [ | ||
f"{CLEAR_LINE}\r{x} {expected_message[i]}" for x in EMOJI_ANIMATION_FRAMES | ||
] | ||
check_animate_output( | ||
capsys, test_string, frame_strings, frame_period, frames_to_test | ||
) | ||
|
||
# set back to test-normal | ||
pipx.animate.stderr_is_tty = False | ||
pipx.animate.emoji_support = True | ||
|
||
|
||
def test_line_lengths_no_emoji(capsys, monkeypatch): | ||
# emoji_support and stderr_is_tty is set only at import animate.py | ||
# since we are already after that, we must override both here | ||
pipx.animate.stderr_is_tty = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use monkeypatch instead of direct set to ensure their correctly reset in case tests fails for the next test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the tip! |
||
pipx.animate.emoji_support = False | ||
|
||
frames_to_test = 2 | ||
# matches animate.py | ||
frame_period = NONEMOJI_FRAME_PERIOD | ||
|
||
# 40-char test_string counts columns e.g.: "0204060810 ... 363840" | ||
test_string = "".join([f"{x:02}" for x in range(2, 41, 2)]) | ||
|
||
columns_to_test = [43, 44, 45] | ||
expected_message = [f"{test_string:.{43-4}}", f"{test_string}", f"{test_string}"] | ||
|
||
for i, columns in enumerate(columns_to_test): | ||
monkeypatch.setenv("COLUMNS", str(columns)) | ||
|
||
frame_strings = [ | ||
f"{CLEAR_LINE}\r{expected_message[i]}{x}" for x in NONEMOJI_ANIMATION_FRAMES | ||
] | ||
|
||
check_animate_output( | ||
capsys, test_string, frame_strings, frame_period, frames_to_test | ||
) | ||
|
||
# set back to test-normal | ||
pipx.animate.stderr_is_tty = False | ||
pipx.animate.emoji_support = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice