Files
mongo/buildscripts/tests/resmokelib/core/test_pipe.py
Juan Gu 7209d62b77 SERVER-99522 Delete dead py linters (#31760)
GitOrigin-RevId: 3aedfa559dda734d4d89fefe0fe1154a5a3cf04d
2025-04-09 19:33:45 +00:00

82 lines
2.2 KiB
Python

"""Unit tests for buildscripts/resmokelib/core/pipe.py."""
from __future__ import absolute_import
import io
import logging
import unittest
import mock
from buildscripts.resmokelib.core import pipe as _pipe
class TestLoggerPipe(unittest.TestCase):
LOG_LEVEL = logging.DEBUG
@classmethod
def _get_log_calls(cls, output):
logger = logging.Logger("for_testing")
logger.log = mock.MagicMock()
logger_pipe = _pipe.LoggerPipe(
logger=logger, level=cls.LOG_LEVEL, pipe_out=io.BytesIO(output)
)
logger_pipe.wait_until_started()
logger_pipe.wait_until_finished()
return logger.log.call_args_list
def test_strips_trailing_whitespace(self):
calls = self._get_log_calls(b" a ")
self.assertEqual(calls, [mock.call(self.LOG_LEVEL, " a")])
def test_strips_trailing_newlines(self):
calls = self._get_log_calls(b"a\r\n")
self.assertEqual(calls, [mock.call(self.LOG_LEVEL, "a")])
def test_handles_invalid_utf8(self):
calls = self._get_log_calls(b"a\x80b")
self.assertEqual(calls, [mock.call(self.LOG_LEVEL, "a\ufffdb")])
def test_escapes_null_bytes(self):
calls = self._get_log_calls(b"a\0b")
self.assertEqual(calls, [mock.call(self.LOG_LEVEL, "a\\0b")])
class TestFormatLineForLogging(unittest.TestCase):
def test_strips_trailing_whitespace(self):
line = b" a "
line_output = _pipe.LoggerPipe._format_line_for_logging(line)
self.assertEqual([" a"], line_output)
def test_strips_trailing_newlines(self):
line = b"a\r\n"
line_output = _pipe.LoggerPipe._format_line_for_logging(line)
self.assertEqual(["a"], line_output)
def test_handles_invalid_utf8(self):
line = b"a\x80b"
line_output = _pipe.LoggerPipe._format_line_for_logging(line)
self.assertEqual(["a\ufffdb"], line_output)
def test_escapes_null_bytes(self):
line = b"a\0b"
line_output = _pipe.LoggerPipe._format_line_for_logging(line)
self.assertEqual(["a\\0b"], line_output)
def test_long_lines_are_split(self):
line = b"a" * 4_000_000
line_output = _pipe.LoggerPipe._format_line_for_logging(line)
self.assertEqual(2, len(line_output))