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

Allow a double stop (ok/error), being a noop in that case. #23

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
7 changes: 7 additions & 0 deletions craft_cli/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ def __init__(self):
self._printer = None
self._mode = None
self._initiated = False
self._stopped = False
self._log_filepath = None
self._log_handler = None

Expand Down Expand Up @@ -670,6 +671,9 @@ def open_stream(self, text: str):
@_init_guard
def ended_ok(self) -> None:
"""Finish the messaging system gracefully."""
if self._stopped:
return
self._stopped = True
self._printer.stop() # type: ignore

def _report_error(self, error: errors.CraftError) -> None:
Expand Down Expand Up @@ -706,6 +710,9 @@ def _report_error(self, error: errors.CraftError) -> None:
@_init_guard
def error(self, error: errors.CraftError) -> None:
"""Handle the system's indicated error and stop machinery."""
if self._stopped:
return
self._stopped = True
self._report_error(error)
self._printer.stop() # type: ignore

Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_messages_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,26 @@ def test_ended_ok(get_initiated_emitter):
assert emitter.printer_calls == [call().stop()]


def test_ended_double_after_ok(get_initiated_emitter):
"""Support double ending."""
emitter = get_initiated_emitter(EmitterMode.QUIET)
emitter.ended_ok()
emitter.printer_calls.clear()

emitter.ended_ok()
assert emitter.printer_calls == []


def test_ended_double_after_error(get_initiated_emitter):
"""Support double ending."""
emitter = get_initiated_emitter(EmitterMode.QUIET)
emitter.error(CraftError("test message"))
emitter.printer_calls.clear()

emitter.ended_ok()
assert emitter.printer_calls == []


# -- tests for error reporting


Expand Down Expand Up @@ -627,3 +647,23 @@ def test_reporterror_full_complete(get_initiated_emitter):
call().show(sys.stderr, full_log_message, use_timestamp=True, end_line=True),
call().stop(),
]


def test_reporterror_double_after_ok(get_initiated_emitter):
"""Support error reporting after ending."""
emitter = get_initiated_emitter(EmitterMode.TRACE)
emitter.ended_ok()
emitter.printer_calls.clear()

emitter.error(CraftError("test message"))
assert emitter.printer_calls == []


def test_reporterror_double_after_error(get_initiated_emitter):
"""Support error reporting after ending."""
emitter = get_initiated_emitter(EmitterMode.TRACE)
emitter.error(CraftError("test message"))
emitter.printer_calls.clear()

emitter.error(CraftError("test message"))
assert emitter.printer_calls == []