fix logger unit tests

This commit is contained in:
jbannon 2022-06-07 07:24:35 +00:00
parent 8a95ca9ff0
commit 3ff64d5d46
2 changed files with 8 additions and 3 deletions

View file

@ -74,7 +74,8 @@ class StreamToLogger(io.StringIO):
""" """
Writes to the logger and stream Writes to the logger and stream
""" """
self._logger.info(__s.removesuffix("\n")) if __s != "\n":
self._logger.info(__s.removesuffix("\n"))
return super().write(__s) return super().write(__s)

View file

@ -92,6 +92,10 @@ class TestLogger:
) )
def test_handle_external_logs(self, capsys, log_level, expected_stdout): def test_handle_external_logs(self, capsys, log_level, expected_stdout):
Logger._LOGGER_LEVEL = log_level Logger._LOGGER_LEVEL = log_level
expected_lines = [
"[ytdl-sub:name_test] test line 1\n",
"[ytdl-sub:name_test] test line 2\n",
]
with Logger.handle_external_logs(name="name_test"): with Logger.handle_external_logs(name="name_test"):
print("test line 1") print("test line 1")
print("test line 2") print("test line 2")
@ -99,11 +103,11 @@ class TestLogger:
# Ensure it goes to stdout only if it is expected to # Ensure it goes to stdout only if it is expected to
captured = capsys.readouterr() captured = capsys.readouterr()
if expected_stdout: if expected_stdout:
assert captured.out == "[ytdl-sub:name_test] test line 1\ntest line 2\n\n" assert captured.out == "".join(expected_lines)
else: else:
assert not captured.out assert not captured.out
# Ensure it always go to the debug file # Ensure it always go to the debug file
with open(Logger._DEBUG_LOGGER_FILE.name, "r", encoding="utf-8") as log_file: with open(Logger._DEBUG_LOGGER_FILE.name, "r", encoding="utf-8") as log_file:
lines = log_file.readlines() lines = log_file.readlines()
assert lines == ["[ytdl-sub:name_test] test line 1\n", "test line 2\n", "\n"] assert lines == expected_lines