Debian Codegraph @

testing/mozbase/mozlog/tests/test_terminal_colors.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 import sys
6 from io import StringIO
7
8 import mozunit
9 import pytest
10 from mozterm import Terminal
11
12
13 @pytest.fixture
14 def terminal():
15 blessed = pytest.importorskip("blessed")
16
17 kind = "xterm-256color"
18 try:
19 term = Terminal(stream=StringIO(), force_styling=True, kind=kind)
20 except blessed.curses.error:
21 pytest.skip(f"terminal '{kind}' not found")
22
23 return term
24
25
26 EXPECTED_DICT = {
27 "log_test_status_fail": "\x1b[31mlog_test_status_fail\x1b(B\x1b[m",
28 "log_process_output": "\x1b[34mlog_process_output\x1b(B\x1b[m",
29 "log_test_status_pass": "\x1b[32mlog_test_status_pass\x1b(B\x1b[m",
30 "log_test_status_unexpected_fail": "\x1b[31mlog_test_status_unexpected_fail\x1b(B\x1b[m",
31 "log_test_status_known_intermittent": "\x1b[33mlog_test_status_known_intermittent\x1b(B\x1b[m",
32 "time": "\x1b[36mtime\x1b(B\x1b[m",
33 "action": "\x1b[33maction\x1b(B\x1b[m",
34 "pid": "\x1b[36mpid\x1b(B\x1b[m",
35 "heading": "\x1b[1m\x1b[33mheading\x1b(B\x1b[m",
36 "sub_heading": "\x1b[33msub_heading\x1b(B\x1b[m",
37 "error": "\x1b[31merror\x1b(B\x1b[m",
38 "warning": "\x1b[33mwarning\x1b(B\x1b[m",
39 "bold": "\x1b[1mbold\x1b(B\x1b[m",
40 "grey": "\x1b[38;2;190;190;190mgrey\x1b(B\x1b[m",
41 "normal": "\x1b[90mnormal\x1b(B\x1b[m",
42 "bright_black": "\x1b[90mbright_black\x1b(B\x1b[m",
43 }
44
45
46 @pytest.mark.skipif(
47 not sys.platform.startswith("win"),
48 reason="Only do ANSI Escape Sequence comparisons on Windows.",
49 )
50 def test_terminal_colors(terminal):
51 from mozlog.formatters.machformatter import TerminalColors, color_dict
52
53 actual_dict = TerminalColors(terminal, color_dict)
54
55 for key in color_dict:
56 assert getattr(actual_dict, key)(key) == EXPECTED_DICT[key]
57
58
59 if __name__ == "__main__":
60 mozunit.main()