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

91 lines
2.2 KiB
Python
Raw 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
2019-04-11 13:56:20 +00:00
print("{}{}{}".format(colorama.Fore.RED, "CRITICAL: foo".rstrip(),
2019-03-22 09:29:06 +00:00
colorama.Style.RESET_ALL))
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
2019-04-11 13:56:20 +00:00
print("{}{}{}".format(colorama.Fore.RED, "ERROR: foo".rstrip(),
2019-03-22 09:29:06 +00:00
colorama.Style.RESET_ALL))
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()
2019-04-11 13:56:20 +00:00
print("{}{}{}".format(colorama.Fore.YELLOW, "WARNING: foo".rstrip(),
2019-03-22 09:29:06 +00:00
colorama.Style.RESET_ALL))
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()
2019-04-11 13:56:20 +00:00
print("{}{}{}".format(colorama.Fore.BLUE, "INFO: foo".rstrip(),
2019-03-22 09:29:06 +00:00
colorama.Style.RESET_ALL))
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()