Skip to content

Commit

Permalink
fix: clear prefix for message() and error()
Browse files Browse the repository at this point in the history
This affects the "streaming brief" feature: the latest progress()
message is used as a prefix for incoming text, but this must be cleared
for "special", important text like message() and error().

Fixes #202
  • Loading branch information
tigarmo authored and sergiusens committed Nov 17, 2023
1 parent 9cdb9ef commit 256d8c2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions craft_cli/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ def message(self, text: str) -> None:
Normally used as the final message, to show the result of a command.
"""
stream = None if self._mode == EmitterMode.QUIET else sys.stdout
if self._streaming_brief:
# Clear the message prefix, as this message stands alone
self._printer.set_terminal_prefix("")
self._printer.show(stream, text)

@_active_guard()
Expand Down Expand Up @@ -733,6 +736,9 @@ def _report_error(self, error: errors.CraftError) -> None:
@_active_guard(ignore_when_stopped=True)
def error(self, error: errors.CraftError) -> None:
"""Handle the system's indicated error and stop machinery."""
if self._streaming_brief:
# Clear the message prefix, as this error stands alone
self._printer.set_terminal_prefix("")
self._report_error(error)
self._stop()

Expand Down
59 changes: 58 additions & 1 deletion tests/integration/test_messages_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import pytest

from craft_cli import messages, printer
from craft_cli import messages, printer, errors
from craft_cli.errors import CraftError
from craft_cli.messages import Emitter, EmitterMode

Expand Down Expand Up @@ -1421,6 +1421,63 @@ def test_streaming_brief_open_stream(capsys, logger):
assert_outputs(capsys, emit, expected_err=expected_err, expected_log=expected_log)


@pytest.mark.parametrize("output_is_terminal", [True])
def test_streaming_brief_messages(capsys, logger, monkeypatch):
"""Test that emit.message() clears the "streaming_brief" prefix."""
emit = Emitter()
emit.init(EmitterMode.BRIEF, "testapp", GREETING, streaming_brief=True)

emit.progress("Doing process.", permanent=False)
emit.message("Process finished successfully.")

emit.ended_ok()

expected_err = [
Line("Doing process.", permanent=False),
]
expected_out = [
Line("Process finished successfully.", permanent=True),
]

expected_log = [
Line("Doing process."),
Line("Process finished successfully."),
]
assert_outputs(
capsys,
emit,
expected_err=expected_err,
expected_out=expected_out,
expected_log=expected_log,
)


@pytest.mark.parametrize("output_is_terminal", [True])
def test_streaming_brief_error(capsys, logger, monkeypatch):
"""Test that emit.error() clears the "streaming_brief" prefix."""
emit = Emitter()
emit.init(EmitterMode.BRIEF, "testapp", GREETING, streaming_brief=True)

emit.progress("Doing process.", permanent=False)

error = errors.CraftError(message="An error happened!", resolution="Detailed resolution.")
emit.error(error)

expected_err = [
Line("Doing process.", permanent=False),
Line("An error happened!", permanent=True),
Line("Recommended resolution: Detailed resolution.", permanent=True),
Line(f"Full execution log: {str(emit._log_filepath)!r}"),
]
expected_log = expected_err
assert_outputs(
capsys,
emit,
expected_err=expected_err,
expected_log=expected_log,
)


@pytest.fixture
def init_emitter():
"""Empty fixture to disable the "global", autouse init_emitter."""
Expand Down

0 comments on commit 256d8c2

Please sign in to comment.