Skip to content

Commit

Permalink
proper tests for logs with colour
Browse files Browse the repository at this point in the history
  • Loading branch information
olliesilvester committed Apr 18, 2024
1 parent 04e3ed8 commit f647ef0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ophyd_async/core/log.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# The LogFormatter is adapted light from tornado, which is licensed under
# The LogFormatter is adapted light from tornado, which is licensed unders
# Apache 2.0. See other_licenses/ in the repository directory.

import logging
Expand Down Expand Up @@ -106,7 +106,7 @@ def __init__(

self._colors = {}
if color and _stderr_supports_color():
if curses is not None:
if "curses" in sys.modules:
# The curses module has some str/bytes confusion in
# python3. Until version 3.2.3, most methods return
# bytes, but only accept strings. In addition, we want to
Expand Down
23 changes: 23 additions & 0 deletions tests/core/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ def test_logger_adapter_ophyd_async_device():
assert log_buffer.getvalue().endswith("[test_device] here is a warning\n")


def test_formatter_with_colour():
log_buffer = io.StringIO()
log_stream = logging.StreamHandler(stream=log_buffer)
with (
patch("ophyd_async.core.log._stderr_supports_color", return_value=True),
patch("curses.tigetstr", return_value=bytes(4)),
patch("curses.tparm", return_value=bytes(4)),
):
log_stream.setFormatter(log.LogFormatter())


def test_formatter_with_colour_no_curses(monkeypatch):
log_buffer = io.StringIO()
log_stream = logging.StreamHandler(stream=log_buffer)
with (
patch("ophyd_async.core.log._stderr_supports_color", return_value=True),
patch("curses.tigetstr", return_value=bytes(4)),
patch("curses.tparm", return_value=bytes(4)),
):
monkeypatch.delitem(sys.modules, "curses", raising=False)
log_stream.setFormatter(log.LogFormatter())


def test_stderr_supports_color_not_atty():
with patch("sys.stderr.isatty", return_value=False):
assert not _stderr_supports_color()
Expand Down

0 comments on commit f647ef0

Please sign in to comment.