From a26b2bb868f2587c7251f0bf0f1cc3b2456644c0 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 11:26:40 -0700 Subject: [PATCH 01/17] Truncate long lines with animation, to prevent multi-line. --- pipx/animate.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pipx/animate.py b/pipx/animate.py index 5db2d45ee1..b8ea41911e 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -2,11 +2,14 @@ from contextlib import contextmanager from threading import Event, Thread from typing import Generator, List +import shutil from pipx.constants import emoji_support stderr_is_tty = sys.stderr.isatty() +(TERM_COLS, TERM_ROWS) = shutil.get_terminal_size(fallback=(9999, 24)) + @contextmanager def animate(message: str, do_animation: bool) -> Generator[None, None, None]: @@ -68,7 +71,10 @@ def print_animation( clear_line() sys.stderr.write("\r") - sys.stderr.write(cur_line) + if len(cur_line) > TERM_COLS: + sys.stderr.write(f"{cur_line:.{TERM_COLS-4}}...") + else: + sys.stderr.write(cur_line) if event.wait(period): break From 0fefbf21342b5550ae390149d25145ad9a307f6f Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 16:48:31 -0700 Subject: [PATCH 02/17] Remove unused TERM_ROWS. --- pipx/animate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipx/animate.py b/pipx/animate.py index b8ea41911e..caf1e28b04 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -8,7 +8,7 @@ stderr_is_tty = sys.stderr.isatty() -(TERM_COLS, TERM_ROWS) = shutil.get_terminal_size(fallback=(9999, 24)) +(TERM_COLS, _) = shutil.get_terminal_size(fallback=(9999, 24)) @contextmanager From 4bcce897f4fd59c63f523ed0aaae2c6379df4fa4 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 16:53:42 -0700 Subject: [PATCH 03/17] Tailor truncation depending on animate_at_beginning_of_line. --- pipx/animate.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index caf1e28b04..e6e03d6f85 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -65,16 +65,19 @@ def print_animation( while not event.wait(0): for s in symbols: if animate_at_beginning_of_line: - cur_line = f"{s} {message}" + if len(message) < TERM_COLS - 2: + cur_line = f"{s} {message}" + else: + cur_line = f"{s} {message:.{TERM_COLS-6}}..." else: - cur_line = f"{message}{s}" + if len(message) < TERM_COLS - 3: + cur_line = f"{message}{s}" + else: + cur_line = f"{message:.{TERM_COLS-4}}{s}" clear_line() sys.stderr.write("\r") - if len(cur_line) > TERM_COLS: - sys.stderr.write(f"{cur_line:.{TERM_COLS-4}}...") - else: - sys.stderr.write(cur_line) + sys.stderr.write(cur_line) if event.wait(period): break From 6bfe12c6ab1191f2db64e2cc707a75b586b59060 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 21:28:09 -0700 Subject: [PATCH 04/17] Make if test correspond to string format precision. --- pipx/animate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index e6e03d6f85..c3f17d3081 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -65,12 +65,12 @@ def print_animation( while not event.wait(0): for s in symbols: if animate_at_beginning_of_line: - if len(message) < TERM_COLS - 2: + if len(message) <= TERM_COLS - 3: cur_line = f"{s} {message}" else: cur_line = f"{s} {message:.{TERM_COLS-6}}..." else: - if len(message) < TERM_COLS - 3: + if len(message) <= TERM_COLS - 4: cur_line = f"{message}{s}" else: cur_line = f"{message:.{TERM_COLS-4}}{s}" From c7ef9d887a17490db32851080e0b4307bf57c297 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 23:26:15 -0700 Subject: [PATCH 05/17] Move get_terminal_size() inside of animate(). --- pipx/animate.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index c3f17d3081..02e285488a 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -8,8 +8,6 @@ stderr_is_tty = sys.stderr.isatty() -(TERM_COLS, _) = shutil.get_terminal_size(fallback=(9999, 24)) - @contextmanager def animate(message: str, do_animation: bool) -> Generator[None, None, None]: @@ -62,18 +60,19 @@ def print_animation( period: float, animate_at_beginning_of_line: bool, ): + (term_cols, _) = shutil.get_terminal_size(fallback=(9999, 24)) while not event.wait(0): for s in symbols: if animate_at_beginning_of_line: if len(message) <= TERM_COLS - 3: cur_line = f"{s} {message}" else: - cur_line = f"{s} {message:.{TERM_COLS-6}}..." + cur_line = f"{s} {message:.{term_cols-6}}..." else: if len(message) <= TERM_COLS - 4: cur_line = f"{message}{s}" else: - cur_line = f"{message:.{TERM_COLS-4}}{s}" + cur_line = f"{message:.{term_cols-4}}{s}" clear_line() sys.stderr.write("\r") From 4f31d4622a15dd7c2d9d68ab3e4eb83554d6d16c Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 20 Oct 2019 23:29:15 -0700 Subject: [PATCH 06/17] Fix bug from last commit. --- pipx/animate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index 02e285488a..b868abdaff 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -64,12 +64,12 @@ def print_animation( while not event.wait(0): for s in symbols: if animate_at_beginning_of_line: - if len(message) <= TERM_COLS - 3: + if len(message) <= term_cols - 3: cur_line = f"{s} {message}" else: cur_line = f"{s} {message:.{term_cols-6}}..." else: - if len(message) <= TERM_COLS - 4: + if len(message) <= term_cols - 4: cur_line = f"{message}{s}" else: cur_line = f"{message:.{term_cols-4}}{s}" From 58cbefb1017600367ffdf8a1499fdd631af5815a Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Mon, 21 Oct 2019 17:12:40 -0700 Subject: [PATCH 07/17] Make max message length method a bit clearer. --- pipx/animate.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index b868abdaff..e41afbe70b 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -64,15 +64,13 @@ def print_animation( while not event.wait(0): for s in symbols: if animate_at_beginning_of_line: - if len(message) <= term_cols - 3: - cur_line = f"{s} {message}" - else: - cur_line = f"{s} {message:.{term_cols-6}}..." + max_message_len = term_cols - len(f"{s} ... ") + cur_line = f"{s} {message:.{max_message_len}}" + if len(message) > max_message_len: + cur_line += "..." else: - if len(message) <= term_cols - 4: - cur_line = f"{message}{s}" - else: - cur_line = f"{message:.{term_cols-4}}{s}" + max_message_len = term_cols - len("... ") + cur_line = f"{message:.{max_message_len}}{s}" clear_line() sys.stderr.write("\r") From 9fdf83fd9d5b0a5d86e74bb464e70af857d296cc Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Thu, 24 Oct 2019 14:48:18 -0700 Subject: [PATCH 08/17] Add tests for message truncation. --- tests/test_animate.py | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/test_animate.py diff --git a/tests/test_animate.py b/tests/test_animate.py new file mode 100644 index 0000000000..9a696cb7be --- /dev/null +++ b/tests/test_animate.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 +import sys +import time + +import pipx.animate + + +HIDE_CURSOR = "\033[?25l" +SHOW_CURSOR = "\033[?25h" +CLEAR_LINE = "\033[K" + + +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:") + 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 + + frames_to_test = 4 + # matches animate.py + frame_period = 0.1 + + # 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 ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"] + ] + 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 + pipx.animate.emoji_support = False + + frames_to_test = 2 + # matches animate.py + frame_period = 1 + + # 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 ["", ".", "..", "..."] + ] + + 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 From aec4c98d9d619d25c9a6253d077e583e7d61acdd Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Thu, 24 Oct 2019 14:51:11 -0700 Subject: [PATCH 09/17] Deleted unused import sys. --- tests/test_animate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index 9a696cb7be..b1e9a3a283 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import sys import time import pipx.animate From 9e1eb97fd664b80927419d95232faa3d29a4540f Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 15:44:37 -0700 Subject: [PATCH 10/17] Add HIDE_CURSOR, SHOW_CURSOR, CLEAR_LINE global strings to animate.py. --- pipx/animate.py | 13 +++++++++---- tests/test_animate.py | 6 +----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index e41afbe70b..682001276b 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -9,6 +9,11 @@ stderr_is_tty = sys.stderr.isatty() +HIDE_CURSOR = "\033[?25l" +SHOW_CURSOR = "\033[?25h" +CLEAR_LINE = "\033[K" + + @contextmanager def animate(message: str, do_animation: bool) -> Generator[None, None, None]: @@ -80,13 +85,13 @@ def print_animation( def hide_cursor(): - sys.stderr.write("\033[?25l") + sys.stderr.write(f"{HIDE_CURSOR}") def show_cursor(): - sys.stderr.write("\033[?25h") + sys.stderr.write(f"{SHOW_CURSOR}") def clear_line(): - sys.stderr.write("\033[K") - sys.stdout.write("\033[K") + sys.stderr.write(f"{CLEAR_LINE}") + sys.stdout.write(f"{CLEAR_LINE}") diff --git a/tests/test_animate.py b/tests/test_animate.py index b1e9a3a283..7f837f7b05 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -2,11 +2,7 @@ import time import pipx.animate - - -HIDE_CURSOR = "\033[?25l" -SHOW_CURSOR = "\033[?25h" -CLEAR_LINE = "\033[K" +from pipx.animate import HIDE_CURSOR, CLEAR_LINE def check_animate_output( From 29a058a3e0dec46a8e38bba2bb99ea82f0b9e01d Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 15:49:32 -0700 Subject: [PATCH 11/17] Add EMOJI_ANIMATION_FRAMES, NONEMOJI_ANIMATION_FRAMES globals to animate.py. --- pipx/animate.py | 6 ++++-- tests/test_animate.py | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index 682001276b..710f06a09d 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -12,6 +12,8 @@ HIDE_CURSOR = "\033[?25l" SHOW_CURSOR = "\033[?25h" CLEAR_LINE = "\033[K" +EMOJI_ANIMATION_FRAMES = ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"] +NONEMOJI_ANIMATION_FRAMES = ["", ".", "..", "..."] @contextmanager @@ -26,11 +28,11 @@ def animate(message: str, do_animation: bool) -> Generator[None, None, None]: if emoji_support: animate_at_beginning_of_line = True - symbols = ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"] + symbols = EMOJI_ANIMATION_FRAMES period = 0.1 else: animate_at_beginning_of_line = False - symbols = ["", ".", "..", "..."] + symbols = NONEMOJI_ANIMATION_FRAMES period = 1 thread_kwargs = { diff --git a/tests/test_animate.py b/tests/test_animate.py index 7f837f7b05..57ee4b928f 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -2,7 +2,12 @@ import time import pipx.animate -from pipx.animate import HIDE_CURSOR, CLEAR_LINE +from pipx.animate import ( + HIDE_CURSOR, + CLEAR_LINE, + EMOJI_ANIMATION_FRAMES, + NONEMOJI_ANIMATION_FRAMES, +) def check_animate_output( @@ -45,8 +50,7 @@ def test_line_lengths_emoji(capsys, monkeypatch): monkeypatch.setenv("COLUMNS", str(columns)) frame_strings = [ - f"{CLEAR_LINE}\r{x} {expected_message[i]}" - for x in ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"] + 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 @@ -77,7 +81,7 @@ def test_line_lengths_no_emoji(capsys, monkeypatch): monkeypatch.setenv("COLUMNS", str(columns)) frame_strings = [ - f"{CLEAR_LINE}\r{expected_message[i]}{x}" for x in ["", ".", "..", "..."] + f"{CLEAR_LINE}\r{expected_message[i]}{x}" for x in NONEMOJI_ANIMATION_FRAMES ] check_animate_output( From f3aa77a197c53ebb79b902c2399a3591584fa187 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 15:53:37 -0700 Subject: [PATCH 12/17] Add EMOJI_ANIMATION_PERIOD, NONEMOJI_ANIMATION_PERIOD globals to animate.py. --- pipx/animate.py | 6 ++++-- tests/test_animate.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pipx/animate.py b/pipx/animate.py index 710f06a09d..14ac656df0 100644 --- a/pipx/animate.py +++ b/pipx/animate.py @@ -14,6 +14,8 @@ CLEAR_LINE = "\033[K" EMOJI_ANIMATION_FRAMES = ["⣷", "⣯", "⣟", "⡿", "⢿", "⣻", "⣽", "⣾"] NONEMOJI_ANIMATION_FRAMES = ["", ".", "..", "..."] +EMOJI_FRAME_PERIOD = 0.1 +NONEMOJI_FRAME_PERIOD = 1 @contextmanager @@ -29,11 +31,11 @@ def animate(message: str, do_animation: bool) -> Generator[None, None, None]: if emoji_support: animate_at_beginning_of_line = True symbols = EMOJI_ANIMATION_FRAMES - period = 0.1 + period = EMOJI_FRAME_PERIOD else: animate_at_beginning_of_line = False symbols = NONEMOJI_ANIMATION_FRAMES - period = 1 + period = NONEMOJI_FRAME_PERIOD thread_kwargs = { "message": message, diff --git a/tests/test_animate.py b/tests/test_animate.py index 57ee4b928f..ec4ebb9b78 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -7,6 +7,8 @@ CLEAR_LINE, EMOJI_ANIMATION_FRAMES, NONEMOJI_ANIMATION_FRAMES, + EMOJI_FRAME_PERIOD, + NONEMOJI_FRAME_PERIOD, ) @@ -38,7 +40,7 @@ def test_line_lengths_emoji(capsys, monkeypatch): frames_to_test = 4 # matches animate.py - frame_period = 0.1 + 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)]) @@ -69,7 +71,7 @@ def test_line_lengths_no_emoji(capsys, monkeypatch): frames_to_test = 2 # matches animate.py - frame_period = 1 + 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)]) From c9be0a75d2d2a97f4cd4ac3355e0b9537305e55f Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 15:54:52 -0700 Subject: [PATCH 13/17] Remove python shebang. --- tests/test_animate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index ec4ebb9b78..7176f51847 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import time import pipx.animate From 73b861f44e69f3557afa3ede36b4b5731eaa2032 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 22:58:53 -0700 Subject: [PATCH 14/17] Implement teardown to re-do original terminal state. --- tests/test_animate.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index 7176f51847..535d49a03a 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -1,5 +1,7 @@ import time +import pytest # type: ignore + import pipx.animate from pipx.animate import ( HIDE_CURSOR, @@ -11,6 +13,17 @@ ) +@pytest.fixture(scope="module") +def terminal_state(): + """Implement tear-down to restore terminal state after test is done. + """ + original_stderr_is_tty = pipx.animate.stderr_is_tty + original_emoji_support = pipx.animate.emoji_support + yield + pipx.animate.stderr_is_tty = original_stderr_is_tty + pipx.animate.emoji_support = original_emoji_support + + def check_animate_output( capsys, test_string, frame_strings, frame_period, frames_to_test ): @@ -22,16 +35,10 @@ def check_animate_output( time.sleep(frame_period * (frames_to_test - 1) + 0.1) captured = capsys.readouterr() - # print for debug help if fail - print("expected_string:") - 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): +def test_line_lengths_emoji(capsys, monkeypatch, terminal_state): # 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 @@ -57,12 +64,8 @@ def test_line_lengths_emoji(capsys, monkeypatch): 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): +def test_line_lengths_no_emoji(capsys, monkeypatch, terminal_state): # 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 @@ -88,7 +91,3 @@ def test_line_lengths_no_emoji(capsys, monkeypatch): 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 From 49228e5c536e353becb9835fb0fbafede29734ef Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 23:08:49 -0700 Subject: [PATCH 15/17] Use monkeypatch instead of terminal_state fixture. --- tests/test_animate.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index 535d49a03a..5cff15fc2b 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -13,17 +13,6 @@ ) -@pytest.fixture(scope="module") -def terminal_state(): - """Implement tear-down to restore terminal state after test is done. - """ - original_stderr_is_tty = pipx.animate.stderr_is_tty - original_emoji_support = pipx.animate.emoji_support - yield - pipx.animate.stderr_is_tty = original_stderr_is_tty - pipx.animate.emoji_support = original_emoji_support - - def check_animate_output( capsys, test_string, frame_strings, frame_period, frames_to_test ): @@ -38,11 +27,11 @@ def check_animate_output( assert captured.err[:chars_to_test] == expected_string[:chars_to_test] -def test_line_lengths_emoji(capsys, monkeypatch, terminal_state): +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 + monkeypatch.setattr(pipx.animate, "stderr_is_tty", True) + monkeypatch.setattr(pipx.animate, "emoji_support", True) frames_to_test = 4 # matches animate.py @@ -65,11 +54,11 @@ def test_line_lengths_emoji(capsys, monkeypatch, terminal_state): ) -def test_line_lengths_no_emoji(capsys, monkeypatch, terminal_state): +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 - pipx.animate.emoji_support = False + monkeypatch.setattr(pipx.animate, "stderr_is_tty", True) + monkeypatch.setattr(pipx.animate, "emoji_support", False) frames_to_test = 2 # matches animate.py From 9f60dc56b46d34d58497d9936a799284c5c3af5a Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 23:11:18 -0700 Subject: [PATCH 16/17] Refactor and remove unused import pytest. --- tests/test_animate.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index 5cff15fc2b..80be3a7b3b 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -1,7 +1,5 @@ import time -import pytest # type: ignore - import pipx.animate from pipx.animate import ( HIDE_CURSOR, @@ -34,8 +32,6 @@ def test_line_lengths_emoji(capsys, monkeypatch): monkeypatch.setattr(pipx.animate, "emoji_support", True) 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)]) @@ -50,7 +46,7 @@ def test_line_lengths_emoji(capsys, monkeypatch): 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 + capsys, test_string, frame_strings, EMOJI_FRAME_PERIOD, frames_to_test ) @@ -61,8 +57,6 @@ def test_line_lengths_no_emoji(capsys, monkeypatch): monkeypatch.setattr(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)]) @@ -78,5 +72,5 @@ def test_line_lengths_no_emoji(capsys, monkeypatch): ] check_animate_output( - capsys, test_string, frame_strings, frame_period, frames_to_test + capsys, test_string, frame_strings, NONEMOJI_FRAME_PERIOD, frames_to_test ) From 443b621b9b339225dfebc91ae093ea066da9d402 Mon Sep 17 00:00:00 2001 From: Matthew Clapp Date: Sun, 27 Oct 2019 23:21:27 -0700 Subject: [PATCH 17/17] Increase sleep time to ensure all frames print. --- tests/test_animate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_animate.py b/tests/test_animate.py index 80be3a7b3b..0389fafeb9 100644 --- a/tests/test_animate.py +++ b/tests/test_animate.py @@ -19,7 +19,7 @@ def check_animate_output( 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) + time.sleep(frame_period * (frames_to_test - 1) + 0.2) captured = capsys.readouterr() assert captured.err[:chars_to_test] == expected_string[:chars_to_test]