|
15 | 15 | # Pylint doesn't play well with fixtures and dependency injection from pytest |
16 | 16 | # pylint: disable=redefined-outer-name |
17 | 17 |
|
| 18 | +import datetime |
18 | 19 | import os |
19 | 20 | import re |
20 | 21 |
|
| 22 | +import click |
21 | 23 | import pytest |
22 | 24 |
|
23 | 25 | from buildstream._testing import create_repo |
24 | 26 |
|
25 | 27 | from buildstream import _yaml |
26 | 28 | 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 |
27 | 32 | from buildstream._testing import cli # pylint: disable=unused-import |
28 | 33 |
|
| 34 | +from tests.testutils import dummy_context |
| 35 | + |
29 | 36 | # Project directory |
30 | 37 | DATA_DIR = os.path.dirname(os.path.realpath(__file__)) |
31 | 38 |
|
@@ -225,3 +232,53 @@ def test_log_line_element_names(cli, datafiles): |
225 | 232 | assert log_name == staged_name |
226 | 233 | assert log_key == staged_key |
227 | 234 | 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