Skip to content

Commit ae2c2a9

Browse files
committed
tests/frontend/logging.py: Test message-lines and error-lines set to 0
Render messages with LogLine and check that no empty lines are left behind when message-lines is 0, that a truncated message still reports how many lines were left out, and that a failure message is followed by a single empty line when error-lines is 0. This is a part of #2145
1 parent 4ce0ecf commit ae2c2a9

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

‎tests/frontend/logging.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@
1515
# Pylint doesn't play well with fixtures and dependency injection from pytest
1616
# pylint: disable=redefined-outer-name
1717

18+
import datetime
1819
import os
1920
import re
2021

22+
import click
2123
import pytest
2224

2325
from buildstream._testing import create_repo
2426

2527
from buildstream import _yaml
2628
from buildstream.exceptions import ErrorDomain
29+
from buildstream._frontend.profile import Profile
30+
from buildstream._frontend.widget import LogLine
31+
from buildstream._message import Message, MessageType
2732
from buildstream._testing import cli # pylint: disable=unused-import
2833

34+
from tests.testutils import dummy_context
35+
2936
# Project directory
3037
DATA_DIR = os.path.dirname(os.path.realpath(__file__))
3138

@@ -225,3 +232,53 @@ def test_log_line_element_names(cli, datafiles):
225232
assert log_name == staged_name
226233
assert log_key == staged_key
227234
assert log_key == keys[log_name]
235+
236+
237+
# Render a message with LogLine the same way the frontend does, without colors
238+
#
239+
def render_log_line(context, message):
240+
profile = Profile()
241+
logger = LogLine(context, None, profile, profile, profile, profile, profile)
242+
return click.unstyle(logger.render(message))
243+
244+
245+
# Test that message details are truncated to the configured message-lines,
246+
# and that setting it to 0 does not leave empty lines behind.
247+
#
248+
@pytest.mark.parametrize(
249+
"message_lines,expected",
250+
[
251+
(0, "Running commands\n"),
252+
(2, "Running commands\n\n line 1\n line 2\n Message contains 1 additional lines\n\n"),
253+
],
254+
ids=["zero", "two"],
255+
)
256+
def test_message_lines(message_lines, expected):
257+
with dummy_context() as context:
258+
context.log_message_format = "%{message}"
259+
context.log_message_lines = message_lines
260+
261+
message = Message(
262+
MessageType.INFO, "Running commands", element_name="test.bst", detail="line 1\nline 2\nline 3"
263+
)
264+
assert render_log_line(context, message) == expected
265+
266+
267+
# Test that a failure message is still followed by a single empty line,
268+
# and not two, when error-lines is set to 0.
269+
#
270+
def test_error_lines_zero():
271+
with dummy_context() as context:
272+
context.log_message_format = "%{message}"
273+
context.log_verbose = True
274+
context.log_error_lines = 0
275+
276+
message = Message(
277+
MessageType.FAIL,
278+
"Running commands",
279+
element_name="test.bst",
280+
elapsed=datetime.timedelta(),
281+
logfile="test.log",
282+
scheduler=True,
283+
)
284+
assert render_log_line(context, message) == "Running commands\n\n"

0 commit comments

Comments
 (0)