1 # Copyright (c) 2020 Deutsche Telekom.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 from logging import LogRecord
15 if sys.version_info[0] < 3:
16 from mock import patch
17 if sys.version_info[0] >= 3:
18 from unittest.mock import patch
21 from onaplogging.colorFormatter import (
30 from onaplogging.utils.system import is_above_python_3_2
33 class TestColorFormatter(unittest.TestCase):
37 def test_colored_os_name_nt(self):
39 with patch("onaplogging.colorFormatter.os.name", "nt"):
41 text = colored(self.TEST_TEXT)
42 assert text == self.TEST_TEXT
44 text = colored(self.TEST_TEXT, color="black")
45 assert text == self.TEST_TEXT
47 text = colored(self.TEST_TEXT, on_color="black")
48 assert text == self.TEST_TEXT
50 text = colored(self.TEST_TEXT, attrs="bold")
51 assert text == self.TEST_TEXT
53 def test_colored_os_name_ce(self):
55 with patch("onaplogging.colorFormatter.os.name", "ce"):
57 text = colored(self.TEST_TEXT)
58 assert text == self.TEST_TEXT
60 text = colored(self.TEST_TEXT, color="black")
61 assert text == self.TEST_TEXT
63 text = colored(self.TEST_TEXT, on_color="black")
64 assert text == self.TEST_TEXT
66 text = colored(self.TEST_TEXT, attrs="bold")
67 assert text == self.TEST_TEXT
69 def test_colored_os_name_posix(self):
71 with patch("onaplogging.colorFormatter.os.name", "posix"):
72 text = colored(self.TEST_TEXT)
73 assert text == self.TEST_TEXT + RESET
75 text = colored(self.TEST_TEXT, color="black")
76 assert text == FMT_STR % (COLORS["black"], self.TEST_TEXT) + RESET
78 text = colored(self.TEST_TEXT, color="invalid")
79 assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
81 text = colored(self.TEST_TEXT, on_color="red")
82 assert text == FMT_STR % (HIGHLIGHTS["red"], self.TEST_TEXT) + RESET
84 text = colored(self.TEST_TEXT, on_color="invalid")
85 assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
87 text = colored(self.TEST_TEXT, attrs="bold")
88 assert text == FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT) + RESET
90 text = colored(self.TEST_TEXT, attrs=["bold", "blink"])
93 == FMT_STR % (ATTRIBUTES["blink"], FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT))
97 text = colored(self.TEST_TEXT, attrs="invalid")
98 assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
100 def test_base_color_formatter(self):
102 if is_above_python_3_2():
103 with pytest.raises(ValueError):
104 BaseColorFormatter(style="!")
106 TEST_MESSAGE = "TestMessage"
110 pathname="TestPathName",
117 base_formatter = BaseColorFormatter()
118 assert base_formatter.format(record) == TEST_MESSAGE + RESET
120 base_formatter = BaseColorFormatter(fmt="TEST %(message)s")
121 assert base_formatter.format(record) == "TEST " + TEST_MESSAGE + RESET
123 colorfmt = {record.levelname: {"color": "black", "highlight": "red", "attribute": "bold"}}
124 base_formatter = BaseColorFormatter(colorfmt=colorfmt)
126 base_formatter.format(record)
130 FMT_STR % (HIGHLIGHTS["red"], FMT_STR % (COLORS["black"], "TestMessage")),