Skip to content
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

Introduce a TESTMODE, so far to just avoid a live Spinner thread. #22

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions craft_cli/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class _MessageInfo: # pylint: disable=too-many-instance-attributes
# the size of bytes chunk that the pipe reader will read at once
_PIPE_READER_CHUNK_SIZE = 4096

# set to true when running *application* tests so some behaviours change
TESTMODE = False


def _get_terminal_width() -> int:
"""Return the number of columns of the terminal."""
Expand Down Expand Up @@ -193,21 +196,26 @@ def stop(self) -> None:


class _Printer:
"""Handle writing the different messages to the different outputs (out, err and log)."""
"""Handle writing the different messages to the different outputs (out, err and log).

If TESTMODE is True, this class changes its behaviour: the spinner is never started,
so there is no thread polluting messages when running tests if they take too long to run.
"""

def __init__(self, log_filepath: pathlib.Path) -> None:
# holder of the previous message
self.prv_msg: Optional[_MessageInfo] = None

# the open log file (will be closed explicitly when the thread ends)
# open the log file (will be closed explicitly later)
self.log = open(log_filepath, "wt", encoding="utf8") # pylint: disable=consider-using-with

# keep account of output streams with unfinished lines
self.unfinished_stream: Optional[TextIO] = None

# run the spinner supervisor
self.spinner = _Spinner(self)
self.spinner.start()
if not TESTMODE:
self.spinner.start()

def _write_line(self, message: _MessageInfo, *, spintext: str = "") -> None:
"""Write a simple line message to the screen."""
Expand Down Expand Up @@ -366,7 +374,8 @@ def stop(self) -> None:
- add a new line to the screen (if needed)
- close the log file
"""
self.spinner.stop()
if not TESTMODE:
self.spinner.stop()
if self.unfinished_stream is not None:
print(flush=True, file=self.unfinished_stream)
self.log.close()
Expand Down
25 changes: 23 additions & 2 deletions tests/unit/test_messages_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,20 @@ def test_progress_bar_no_stream(recording_printer):
assert recording_printer.prv_msg is None


# -- tests for stopping the printer
# -- tests for starting/stopping the printer


def test_init_printer_ok(log_filepath):
"""Printer is initiated as usual."""
printer = _Printer(log_filepath)
assert printer.spinner.is_alive()


def test_init_printer_testmode(log_filepath, monkeypatch):
"""Printer is initiated as usual."""
monkeypatch.setattr(messages, "TESTMODE", True)
printer = _Printer(log_filepath)
assert not printer.spinner.is_alive()


def test_stop_streams_ok(capsys, log_filepath):
Expand Down Expand Up @@ -670,9 +683,17 @@ def test_stop_streams_unfinished_err(capsys, log_filepath):
assert err == "\n"


def test_stop_spinner(log_filepath):
def test_stop_spinner_ok(log_filepath):
"""Stop the spinner."""
printer = _Printer(log_filepath)
assert printer.spinner.is_alive()
printer.stop()
assert not printer.spinner.is_alive()


def test_stop_spinner_testmode(log_filepath, monkeypatch):
"""Stop the spinner."""
monkeypatch.setattr(messages, "TESTMODE", True)
printer = _Printer(log_filepath)
printer.stop()
assert not printer.spinner.is_alive()