| 1 |
import contextlib |
| 2 |
import dataclasses |
| 3 |
import io |
| 4 |
import sys |
| 5 |
import unittest |
| 6 |
import unittest.mock |
| 7 |
import _colorize |
| 8 |
from test.support.os_helper import EnvironmentVarGuard |
| 9 |
|
| 10 |
|
| 11 |
@contextlib.contextmanager |
| 12 |
def clear_env(): |
| 13 |
with EnvironmentVarGuard() as mock_env: |
| 14 |
mock_env.unset("FORCE_COLOR", "NO_COLOR", "PYTHON_COLORS", "TERM") |
| 15 |
yield mock_env |
| 16 |
|
| 17 |
|
| 18 |
def supports_virtual_terminal(): |
| 19 |
if sys.platform == "win32": |
| 20 |
return unittest.mock.patch("nt._supports_virtual_terminal", return_value=True) |
| 21 |
else: |
| 22 |
return contextlib.nullcontext() |
| 23 |
|
| 24 |
|
| 25 |
class TestTheme(unittest.TestCase): |
| 26 |
|
| 27 |
def test_attributes(self): |
| 28 |
# only theme configurations attributes by default |
| 29 |
for field in dataclasses.fields(_colorize.Theme): |
| 30 |
with self.subTest(field.name): |
| 31 |
self.assertIsSubclass(field.type, _colorize.ThemeSection) |
| 32 |
self.assertIsNotNone(field.default_factory) |
| 33 |
|
| 34 |
def test_copy_with(self): |
| 35 |
theme = _colorize.Theme() |
| 36 |
|
| 37 |
copy = theme.copy_with() |
| 38 |
self.assertEqual(theme, copy) |
| 39 |
|
| 40 |
unittest_no_colors = _colorize.Unittest.no_colors() |
| 41 |
copy = theme.copy_with(unittest=unittest_no_colors) |
| 42 |
self.assertEqual(copy.argparse, theme.argparse) |
| 43 |
self.assertEqual(copy.syntax, theme.syntax) |
| 44 |
self.assertEqual(copy.traceback, theme.traceback) |
| 45 |
self.assertEqual(copy.unittest, unittest_no_colors) |
| 46 |
|
| 47 |
def test_no_colors(self): |
| 48 |
# idempotence test |
| 49 |
theme_no_colors = _colorize.Theme().no_colors() |
| 50 |
theme_no_colors_no_colors = theme_no_colors.no_colors() |
| 51 |
self.assertEqual(theme_no_colors, theme_no_colors_no_colors) |
| 52 |
|
| 53 |
# attributes check |
| 54 |
for section in dataclasses.fields(_colorize.Theme): |
| 55 |
with self.subTest(section.name): |
| 56 |
section_theme = getattr(theme_no_colors, section.name) |
| 57 |
self.assertEqual(section_theme, section.type.no_colors()) |
| 58 |
|
| 59 |
|
| 60 |
class TestColorizeFunction(unittest.TestCase): |
| 61 |
def test_colorized_detection_checks_for_environment_variables(self): |
| 62 |
def check(env, fallback, expected): |
| 63 |
with (self.subTest(env=env, fallback=fallback), |
| 64 |
clear_env() as mock_env): |
| 65 |
mock_env.update(env) |
| 66 |
isatty_mock.return_value = fallback |
| 67 |
stdout_mock.isatty.return_value = fallback |
| 68 |
self.assertEqual(_colorize.can_colorize(), expected) |
| 69 |
|
| 70 |
with (unittest.mock.patch("os.isatty") as isatty_mock, |
| 71 |
unittest.mock.patch("sys.stdout") as stdout_mock, |
| 72 |
supports_virtual_terminal()): |
| 73 |
stdout_mock.fileno.return_value = 1 |
| 74 |
|
| 75 |
for fallback in False, True: |
| 76 |
check({}, fallback, fallback) |
| 77 |
check({'TERM': 'dumb'}, fallback, False) |
| 78 |
check({'TERM': 'xterm'}, fallback, fallback) |
| 79 |
check({'TERM': ''}, fallback, fallback) |
| 80 |
check({'FORCE_COLOR': '1'}, fallback, True) |
| 81 |
check({'FORCE_COLOR': '0'}, fallback, True) |
| 82 |
check({'FORCE_COLOR': ''}, fallback, fallback) |
| 83 |
check({'NO_COLOR': '1'}, fallback, False) |
| 84 |
check({'NO_COLOR': '0'}, fallback, False) |
| 85 |
check({'NO_COLOR': ''}, fallback, fallback) |
| 86 |
|
| 87 |
check({'TERM': 'dumb', 'FORCE_COLOR': '1'}, False, True) |
| 88 |
check({'FORCE_COLOR': '1', 'NO_COLOR': '1'}, True, False) |
| 89 |
|
| 90 |
for ignore_environment in False, True: |
| 91 |
# Simulate running with or without `-E`. |
| 92 |
flags = unittest.mock.MagicMock(ignore_environment=ignore_environment) |
| 93 |
with unittest.mock.patch("sys.flags", flags): |
| 94 |
check({'PYTHON_COLORS': '1'}, True, True) |
| 95 |
check({'PYTHON_COLORS': '1'}, False, not ignore_environment) |
| 96 |
check({'PYTHON_COLORS': '0'}, True, ignore_environment) |
| 97 |
check({'PYTHON_COLORS': '0'}, False, False) |
| 98 |
for fallback in False, True: |
| 99 |
check({'PYTHON_COLORS': 'x'}, fallback, fallback) |
| 100 |
check({'PYTHON_COLORS': ''}, fallback, fallback) |
| 101 |
|
| 102 |
check({'TERM': 'dumb', 'PYTHON_COLORS': '1'}, False, not ignore_environment) |
| 103 |
check({'NO_COLOR': '1', 'PYTHON_COLORS': '1'}, False, not ignore_environment) |
| 104 |
check({'FORCE_COLOR': '1', 'PYTHON_COLORS': '0'}, True, ignore_environment) |
| 105 |
|
| 106 |
@unittest.skipUnless(sys.platform == "win32", "requires Windows") |
| 107 |
def test_colorized_detection_checks_on_windows(self): |
| 108 |
with (clear_env(), |
| 109 |
unittest.mock.patch("os.isatty") as isatty_mock, |
| 110 |
unittest.mock.patch("sys.stdout") as stdout_mock, |
| 111 |
supports_virtual_terminal() as vt_mock): |
| 112 |
stdout_mock.fileno.return_value = 1 |
| 113 |
isatty_mock.return_value = True |
| 114 |
stdout_mock.isatty.return_value = True |
| 115 |
|
| 116 |
vt_mock.return_value = True |
| 117 |
self.assertEqual(_colorize.can_colorize(), True) |
| 118 |
vt_mock.return_value = False |
| 119 |
self.assertEqual(_colorize.can_colorize(), False) |
| 120 |
import nt |
| 121 |
del nt._supports_virtual_terminal |
| 122 |
self.assertEqual(_colorize.can_colorize(), False) |
| 123 |
|
| 124 |
def test_colorized_detection_checks_for_std_streams(self): |
| 125 |
with (clear_env(), |
| 126 |
unittest.mock.patch("os.isatty") as isatty_mock, |
| 127 |
unittest.mock.patch("sys.stdout") as stdout_mock, |
| 128 |
unittest.mock.patch("sys.stderr") as stderr_mock, |
| 129 |
supports_virtual_terminal()): |
| 130 |
stdout_mock.fileno.return_value = 1 |
| 131 |
stderr_mock.fileno.side_effect = ZeroDivisionError |
| 132 |
stderr_mock.isatty.side_effect = ZeroDivisionError |
| 133 |
|
| 134 |
isatty_mock.return_value = True |
| 135 |
stdout_mock.isatty.return_value = True |
| 136 |
self.assertEqual(_colorize.can_colorize(), True) |
| 137 |
|
| 138 |
isatty_mock.return_value = False |
| 139 |
stdout_mock.isatty.return_value = False |
| 140 |
self.assertEqual(_colorize.can_colorize(), False) |
| 141 |
|
| 142 |
def test_colorized_detection_checks_for_file(self): |
| 143 |
with clear_env(), supports_virtual_terminal(): |
| 144 |
|
| 145 |
with unittest.mock.patch("os.isatty") as isatty_mock: |
| 146 |
file = unittest.mock.MagicMock() |
| 147 |
file.fileno.return_value = 1 |
| 148 |
isatty_mock.return_value = True |
| 149 |
self.assertEqual(_colorize.can_colorize(file=file), True) |
| 150 |
isatty_mock.return_value = False |
| 151 |
self.assertEqual(_colorize.can_colorize(file=file), False) |
| 152 |
|
| 153 |
# No file.fileno. |
| 154 |
with unittest.mock.patch("os.isatty", side_effect=ZeroDivisionError): |
| 155 |
file = unittest.mock.MagicMock(spec=['isatty']) |
| 156 |
file.isatty.return_value = True |
| 157 |
self.assertEqual(_colorize.can_colorize(file=file), False) |
| 158 |
|
| 159 |
# file.fileno() raises io.UnsupportedOperation. |
| 160 |
with unittest.mock.patch("os.isatty", side_effect=ZeroDivisionError): |
| 161 |
file = unittest.mock.MagicMock() |
| 162 |
file.fileno.side_effect = io.UnsupportedOperation |
| 163 |
file.isatty.return_value = True |
| 164 |
self.assertEqual(_colorize.can_colorize(file=file), True) |
| 165 |
file.isatty.return_value = False |
| 166 |
self.assertEqual(_colorize.can_colorize(file=file), False) |
| 167 |
|
| 168 |
# The documentation for file.fileno says: |
| 169 |
# > An OSError is raised if the IO object does not use a file descriptor. |
| 170 |
# gh-141570: Check OSError is caught and handled |
| 171 |
with unittest.mock.patch("os.isatty", side_effect=ZeroDivisionError): |
| 172 |
file = unittest.mock.MagicMock() |
| 173 |
file.fileno.side_effect = OSError |
| 174 |
file.isatty.return_value = True |
| 175 |
self.assertEqual(_colorize.can_colorize(file=file), True) |
| 176 |
file.isatty.return_value = False |
| 177 |
self.assertEqual(_colorize.can_colorize(file=file), False) |
| 178 |
|
| 179 |
|
| 180 |
if __name__ == "__main__": |
| 181 |
unittest.main() |