ansible-later/ansiblelater/test/unit/test_logger.py

99 lines
2.3 KiB
Python
Raw Permalink Normal View History

2019-04-11 13:56:20 +00:00
"""Test logging module."""
2019-03-22 09:29:06 +00:00
from __future__ import print_function
import colorama
2019-04-11 13:56:20 +00:00
from ansiblelater import logger
2019-03-22 09:29:06 +00:00
2019-04-15 09:20:12 +00:00
def test_flag_extra():
extra = {}
extra.update(foo="bar")
flagged = logger.flag_extra(extra)
assert flagged.get("later_foo") == "bar"
2019-04-11 13:56:20 +00:00
def test_critical(capsys, mocker):
log = logger.get_logger("test_critical")
log.critical("foo")
_, stderr = capsys.readouterr()
2019-03-22 09:29:06 +00:00
2020-04-05 13:42:48 +00:00
print(
f"{colorama.Fore.RED}{colorama.Style.BRIGHT}CRITICAL:{colorama.Style.NORMAL} foo\n"
f"{colorama.Style.RESET_ALL}"
2020-04-05 13:42:48 +00:00
)
2019-03-22 09:29:06 +00:00
x, _ = capsys.readouterr()
2019-04-11 13:56:20 +00:00
assert x == stderr
2019-03-22 09:29:06 +00:00
def test_error(capsys, mocker):
2019-04-11 13:56:20 +00:00
log = logger.get_logger("test_error")
log.error("foo")
_, stderr = capsys.readouterr()
2019-03-22 09:29:06 +00:00
2020-04-05 13:42:48 +00:00
print(
f"{colorama.Fore.RED}{colorama.Style.BRIGHT}ERROR:{colorama.Style.NORMAL} foo\n"
f"{colorama.Style.RESET_ALL}"
2020-04-05 13:42:48 +00:00
)
2019-03-22 09:29:06 +00:00
x, _ = capsys.readouterr()
2019-04-11 13:56:20 +00:00
assert x == stderr
2019-03-22 09:29:06 +00:00
def test_warn(capsys, mocker):
2019-04-11 13:56:20 +00:00
log = logger.get_logger("test_warn")
log.warning("foo")
2019-03-22 09:29:06 +00:00
stdout, _ = capsys.readouterr()
2020-04-05 13:42:48 +00:00
print(
f"{colorama.Fore.YELLOW}{colorama.Style.BRIGHT}WARNING:{colorama.Style.NORMAL} foo\n"
f"{colorama.Style.RESET_ALL}"
2020-04-05 13:42:48 +00:00
)
2019-03-22 09:29:06 +00:00
x, _ = capsys.readouterr()
assert x == stdout
def test_info(capsys, mocker):
2019-04-11 13:56:20 +00:00
log = logger.get_logger("test_info")
log.info("foo")
2019-03-22 09:29:06 +00:00
stdout, _ = capsys.readouterr()
2020-04-05 13:42:48 +00:00
print(
f"{colorama.Fore.BLUE}{colorama.Style.BRIGHT}INFO:{colorama.Style.NORMAL} foo\n"
f"{colorama.Style.RESET_ALL}"
2020-04-05 13:42:48 +00:00
)
2019-03-22 09:29:06 +00:00
x, _ = capsys.readouterr()
assert x == stdout
def test_markup_detection_pycolors0(monkeypatch):
2019-04-11 13:56:20 +00:00
monkeypatch.setenv("PY_COLORS", "0")
assert not logger._should_do_markup()
2019-03-22 09:29:06 +00:00
def test_markup_detection_pycolors1(monkeypatch):
2019-04-11 13:56:20 +00:00
monkeypatch.setenv("PY_COLORS", "1")
assert logger._should_do_markup()
2019-03-22 09:29:06 +00:00
def test_markup_detection_tty_yes(mocker):
2019-04-11 13:56:20 +00:00
mocker.patch("sys.stdout.isatty", return_value=True)
mocker.patch("os.environ", {"TERM": "xterm"})
assert logger._should_do_markup()
2019-03-22 09:29:06 +00:00
mocker.resetall()
mocker.stopall()
def test_markup_detection_tty_no(mocker):
2019-04-11 13:56:20 +00:00
mocker.patch("os.environ", {})
mocker.patch("sys.stdout.isatty", return_value=False)
assert not logger._should_do_markup()
2019-03-22 09:29:06 +00:00
mocker.resetall()
mocker.stopall()