Skip to content

Commit

Permalink
Merge pull request #6 from facundobatista/trace-message
Browse files Browse the repository at this point in the history
Added a .trace method to Emitter to show trace information (CRAFT-97).
  • Loading branch information
facundobatista authored Sep 15, 2021
2 parents 4a9ab61 + 91c4c8a commit be12175
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ test-pydocstyle:
.PHONY: test-pylint
test-pylint:
pylint craft_cli
pylint tests --disable=missing-module-docstring,missing-function-docstring,redefined-outer-name,protected-access
pylint tests --disable=missing-module-docstring,missing-function-docstring,redefined-outer-name,protected-access,duplicate-code

.PHONY: test-pyright
test-pyright:
Expand Down
10 changes: 10 additions & 0 deletions craft_cli/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ def message(self, text: str, intermediate: bool = False) -> None:
)
self.printer.show(sys.stdout, text, use_timestamp=use_timestamp) # type: ignore

@_init_guard
def trace(self, text: str) -> None:
"""Trace/debug information.
This is to record everything that the user may not want to normally see, but it's
useful for postmortem analysis.
"""
stream = sys.stderr if self.mode == EmitterMode.TRACE else None
self.printer.show(stream, text, use_timestamp=True) # type: ignore

@_init_guard
def ended_ok(self) -> None:
"""Finish the messaging system gracefully."""
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_messages_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ def test_message_intermediate_verboseish(get_initiated_emitter, mode):
]


@pytest.mark.parametrize(
"mode",
[
EmitterMode.QUIET,
EmitterMode.NORMAL,
EmitterMode.VERBOSE,
],
)
def test_trace_in_non_trace_modes(get_initiated_emitter, mode):
"""Only log the message."""
emitter = get_initiated_emitter(mode)
emitter.trace("some text")

assert emitter.printer_calls == [
call().show(None, "some text", use_timestamp=True),
]


def test_trace_in_trace_mode(get_initiated_emitter):
"""Lof the message and show it in stderr."""
emitter = get_initiated_emitter(EmitterMode.TRACE)
emitter.trace("some text")

assert emitter.printer_calls == [
call().show(sys.stderr, "some text", use_timestamp=True),
]


# -- tests for stopping the machinery


Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_messages_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,40 @@ def test_01_intermediate_message_verbose(capsys, mode):
assert_outputs(capsys, emit, expected_out=expected, expected_log=expected)


@pytest.mark.parametrize(
"mode",
[
EmitterMode.QUIET,
EmitterMode.NORMAL,
EmitterMode.VERBOSE,
],
)
def test_04_5_trace_other_modes(capsys, mode, monkeypatch):
"""Internal trace for other modes."""
emit = Emitter()
emit.init(mode, "testapp", GREETING)
emit.trace("The meaning of life is 42.")
emit.ended_ok()

expected = [
Line("The meaning of life is 42.", timestamp=True),
]
assert_outputs(capsys, emit, expected_log=expected)


def test_04_5_trace_in_trace(capsys):
"""Internal trace when in trace mode."""
emit = Emitter()
emit.init(EmitterMode.TRACE, "testapp", GREETING)
emit.trace("The meaning of life is 42.")
emit.ended_ok()

expected = [
Line("The meaning of life is 42.", timestamp=True),
]
assert_outputs(capsys, emit, expected_err=expected, expected_log=expected)


@pytest.mark.parametrize(
"mode",
[
Expand All @@ -199,7 +233,9 @@ def test_initial_messages_when_quietish(capsys, mode, monkeypatch, tmp_path):

emit = Emitter()
emit.init(EmitterMode.NORMAL, "testapp", different_greeting)
emit.trace("initial trace")
emit.set_mode(mode)
emit.trace("second trace")
emit.message("final message")
emit.ended_ok()

Expand All @@ -208,6 +244,8 @@ def test_initial_messages_when_quietish(capsys, mode, monkeypatch, tmp_path):
]
expected_log = [
Line(different_greeting),
Line("initial trace"),
Line("second trace"),
Line("final message"),
]
assert_outputs(capsys, emit, expected_out=expected_out, expected_log=expected_log)
Expand All @@ -222,7 +260,9 @@ def test_initial_messages_when_verbose(capsys, tmp_path, monkeypatch):

emit = Emitter()
emit.init(EmitterMode.NORMAL, "testapp", different_greeting)
emit.trace("initial trace")
emit.set_mode(EmitterMode.VERBOSE)
emit.trace("second trace")
emit.message("final message")
emit.ended_ok()

Expand All @@ -235,6 +275,8 @@ def test_initial_messages_when_verbose(capsys, tmp_path, monkeypatch):
]
expected_log = [
Line(different_greeting),
Line("initial trace"),
Line("second trace"),
Line("final message"),
]
assert_outputs(
Expand All @@ -255,7 +297,9 @@ def test_initial_messages_when_trace(capsys, tmp_path, monkeypatch):

emit = Emitter()
emit.init(EmitterMode.NORMAL, "testapp", different_greeting)
emit.trace("initial trace")
emit.set_mode(EmitterMode.TRACE)
emit.trace("second trace")
emit.message("final message")
emit.ended_ok()

Expand All @@ -265,9 +309,12 @@ def test_initial_messages_when_trace(capsys, tmp_path, monkeypatch):
expected_err = [
Line(different_greeting, timestamp=True),
Line(f"Logging execution to '{different_logpath}'", timestamp=True),
Line("second trace", timestamp=True),
]
expected_log = [
Line(different_greeting),
Line("initial trace"),
Line("second trace"),
Line("final message"),
]
assert_outputs(
Expand Down

0 comments on commit be12175

Please sign in to comment.