Загрузить файлы в «venv/Lib/site-packages/colorama/tests»
This commit is contained in:
1
venv/Lib/site-packages/colorama/tests/__init__.py
Normal file
1
venv/Lib/site-packages/colorama/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
|
||||||
76
venv/Lib/site-packages/colorama/tests/ansi_test.py
Normal file
76
venv/Lib/site-packages/colorama/tests/ansi_test.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
|
||||||
|
import sys
|
||||||
|
from unittest import TestCase, main
|
||||||
|
|
||||||
|
from ..ansi import Back, Fore, Style
|
||||||
|
from ..ansitowin32 import AnsiToWin32
|
||||||
|
|
||||||
|
stdout_orig = sys.stdout
|
||||||
|
stderr_orig = sys.stderr
|
||||||
|
|
||||||
|
|
||||||
|
class AnsiTest(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# sanity check: stdout should be a file or StringIO object.
|
||||||
|
# It will only be AnsiToWin32 if init() has previously wrapped it
|
||||||
|
self.assertNotEqual(type(sys.stdout), AnsiToWin32)
|
||||||
|
self.assertNotEqual(type(sys.stderr), AnsiToWin32)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
sys.stdout = stdout_orig
|
||||||
|
sys.stderr = stderr_orig
|
||||||
|
|
||||||
|
|
||||||
|
def testForeAttributes(self):
|
||||||
|
self.assertEqual(Fore.BLACK, '\033[30m')
|
||||||
|
self.assertEqual(Fore.RED, '\033[31m')
|
||||||
|
self.assertEqual(Fore.GREEN, '\033[32m')
|
||||||
|
self.assertEqual(Fore.YELLOW, '\033[33m')
|
||||||
|
self.assertEqual(Fore.BLUE, '\033[34m')
|
||||||
|
self.assertEqual(Fore.MAGENTA, '\033[35m')
|
||||||
|
self.assertEqual(Fore.CYAN, '\033[36m')
|
||||||
|
self.assertEqual(Fore.WHITE, '\033[37m')
|
||||||
|
self.assertEqual(Fore.RESET, '\033[39m')
|
||||||
|
|
||||||
|
# Check the light, extended versions.
|
||||||
|
self.assertEqual(Fore.LIGHTBLACK_EX, '\033[90m')
|
||||||
|
self.assertEqual(Fore.LIGHTRED_EX, '\033[91m')
|
||||||
|
self.assertEqual(Fore.LIGHTGREEN_EX, '\033[92m')
|
||||||
|
self.assertEqual(Fore.LIGHTYELLOW_EX, '\033[93m')
|
||||||
|
self.assertEqual(Fore.LIGHTBLUE_EX, '\033[94m')
|
||||||
|
self.assertEqual(Fore.LIGHTMAGENTA_EX, '\033[95m')
|
||||||
|
self.assertEqual(Fore.LIGHTCYAN_EX, '\033[96m')
|
||||||
|
self.assertEqual(Fore.LIGHTWHITE_EX, '\033[97m')
|
||||||
|
|
||||||
|
|
||||||
|
def testBackAttributes(self):
|
||||||
|
self.assertEqual(Back.BLACK, '\033[40m')
|
||||||
|
self.assertEqual(Back.RED, '\033[41m')
|
||||||
|
self.assertEqual(Back.GREEN, '\033[42m')
|
||||||
|
self.assertEqual(Back.YELLOW, '\033[43m')
|
||||||
|
self.assertEqual(Back.BLUE, '\033[44m')
|
||||||
|
self.assertEqual(Back.MAGENTA, '\033[45m')
|
||||||
|
self.assertEqual(Back.CYAN, '\033[46m')
|
||||||
|
self.assertEqual(Back.WHITE, '\033[47m')
|
||||||
|
self.assertEqual(Back.RESET, '\033[49m')
|
||||||
|
|
||||||
|
# Check the light, extended versions.
|
||||||
|
self.assertEqual(Back.LIGHTBLACK_EX, '\033[100m')
|
||||||
|
self.assertEqual(Back.LIGHTRED_EX, '\033[101m')
|
||||||
|
self.assertEqual(Back.LIGHTGREEN_EX, '\033[102m')
|
||||||
|
self.assertEqual(Back.LIGHTYELLOW_EX, '\033[103m')
|
||||||
|
self.assertEqual(Back.LIGHTBLUE_EX, '\033[104m')
|
||||||
|
self.assertEqual(Back.LIGHTMAGENTA_EX, '\033[105m')
|
||||||
|
self.assertEqual(Back.LIGHTCYAN_EX, '\033[106m')
|
||||||
|
self.assertEqual(Back.LIGHTWHITE_EX, '\033[107m')
|
||||||
|
|
||||||
|
|
||||||
|
def testStyleAttributes(self):
|
||||||
|
self.assertEqual(Style.DIM, '\033[2m')
|
||||||
|
self.assertEqual(Style.NORMAL, '\033[22m')
|
||||||
|
self.assertEqual(Style.BRIGHT, '\033[1m')
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
294
venv/Lib/site-packages/colorama/tests/ansitowin32_test.py
Normal file
294
venv/Lib/site-packages/colorama/tests/ansitowin32_test.py
Normal file
@@ -0,0 +1,294 @@
|
|||||||
|
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
|
||||||
|
from io import StringIO, TextIOWrapper
|
||||||
|
from unittest import TestCase, main
|
||||||
|
try:
|
||||||
|
from contextlib import ExitStack
|
||||||
|
except ImportError:
|
||||||
|
# python 2
|
||||||
|
from contextlib2 import ExitStack
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import MagicMock, Mock, patch
|
||||||
|
except ImportError:
|
||||||
|
from mock import MagicMock, Mock, patch
|
||||||
|
|
||||||
|
from ..ansitowin32 import AnsiToWin32, StreamWrapper
|
||||||
|
from ..win32 import ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
||||||
|
from .utils import osname
|
||||||
|
|
||||||
|
|
||||||
|
class StreamWrapperTest(TestCase):
|
||||||
|
|
||||||
|
def testIsAProxy(self):
|
||||||
|
mockStream = Mock()
|
||||||
|
wrapper = StreamWrapper(mockStream, None)
|
||||||
|
self.assertTrue( wrapper.random_attr is mockStream.random_attr )
|
||||||
|
|
||||||
|
def testDelegatesWrite(self):
|
||||||
|
mockStream = Mock()
|
||||||
|
mockConverter = Mock()
|
||||||
|
wrapper = StreamWrapper(mockStream, mockConverter)
|
||||||
|
wrapper.write('hello')
|
||||||
|
self.assertTrue(mockConverter.write.call_args, (('hello',), {}))
|
||||||
|
|
||||||
|
def testDelegatesContext(self):
|
||||||
|
mockConverter = Mock()
|
||||||
|
s = StringIO()
|
||||||
|
with StreamWrapper(s, mockConverter) as fp:
|
||||||
|
fp.write(u'hello')
|
||||||
|
self.assertTrue(s.closed)
|
||||||
|
|
||||||
|
def testProxyNoContextManager(self):
|
||||||
|
mockStream = MagicMock()
|
||||||
|
mockStream.__enter__.side_effect = AttributeError()
|
||||||
|
mockConverter = Mock()
|
||||||
|
with self.assertRaises(AttributeError) as excinfo:
|
||||||
|
with StreamWrapper(mockStream, mockConverter) as wrapper:
|
||||||
|
wrapper.write('hello')
|
||||||
|
|
||||||
|
def test_closed_shouldnt_raise_on_closed_stream(self):
|
||||||
|
stream = StringIO()
|
||||||
|
stream.close()
|
||||||
|
wrapper = StreamWrapper(stream, None)
|
||||||
|
self.assertEqual(wrapper.closed, True)
|
||||||
|
|
||||||
|
def test_closed_shouldnt_raise_on_detached_stream(self):
|
||||||
|
stream = TextIOWrapper(StringIO())
|
||||||
|
stream.detach()
|
||||||
|
wrapper = StreamWrapper(stream, None)
|
||||||
|
self.assertEqual(wrapper.closed, True)
|
||||||
|
|
||||||
|
class AnsiToWin32Test(TestCase):
|
||||||
|
|
||||||
|
def testInit(self):
|
||||||
|
mockStdout = Mock()
|
||||||
|
auto = Mock()
|
||||||
|
stream = AnsiToWin32(mockStdout, autoreset=auto)
|
||||||
|
self.assertEqual(stream.wrapped, mockStdout)
|
||||||
|
self.assertEqual(stream.autoreset, auto)
|
||||||
|
|
||||||
|
@patch('colorama.ansitowin32.winterm', None)
|
||||||
|
@patch('colorama.ansitowin32.winapi_test', lambda *_: True)
|
||||||
|
def testStripIsTrueOnWindows(self):
|
||||||
|
with osname('nt'):
|
||||||
|
mockStdout = Mock()
|
||||||
|
stream = AnsiToWin32(mockStdout)
|
||||||
|
self.assertTrue(stream.strip)
|
||||||
|
|
||||||
|
def testStripIsFalseOffWindows(self):
|
||||||
|
with osname('posix'):
|
||||||
|
mockStdout = Mock(closed=False)
|
||||||
|
stream = AnsiToWin32(mockStdout)
|
||||||
|
self.assertFalse(stream.strip)
|
||||||
|
|
||||||
|
def testWriteStripsAnsi(self):
|
||||||
|
mockStdout = Mock()
|
||||||
|
stream = AnsiToWin32(mockStdout)
|
||||||
|
stream.wrapped = Mock()
|
||||||
|
stream.write_and_convert = Mock()
|
||||||
|
stream.strip = True
|
||||||
|
|
||||||
|
stream.write('abc')
|
||||||
|
|
||||||
|
self.assertFalse(stream.wrapped.write.called)
|
||||||
|
self.assertEqual(stream.write_and_convert.call_args, (('abc',), {}))
|
||||||
|
|
||||||
|
def testWriteDoesNotStripAnsi(self):
|
||||||
|
mockStdout = Mock()
|
||||||
|
stream = AnsiToWin32(mockStdout)
|
||||||
|
stream.wrapped = Mock()
|
||||||
|
stream.write_and_convert = Mock()
|
||||||
|
stream.strip = False
|
||||||
|
stream.convert = False
|
||||||
|
|
||||||
|
stream.write('abc')
|
||||||
|
|
||||||
|
self.assertFalse(stream.write_and_convert.called)
|
||||||
|
self.assertEqual(stream.wrapped.write.call_args, (('abc',), {}))
|
||||||
|
|
||||||
|
def assert_autoresets(self, convert, autoreset=True):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
stream.convert = convert
|
||||||
|
stream.reset_all = Mock()
|
||||||
|
stream.autoreset = autoreset
|
||||||
|
stream.winterm = Mock()
|
||||||
|
|
||||||
|
stream.write('abc')
|
||||||
|
|
||||||
|
self.assertEqual(stream.reset_all.called, autoreset)
|
||||||
|
|
||||||
|
def testWriteAutoresets(self):
|
||||||
|
self.assert_autoresets(convert=True)
|
||||||
|
self.assert_autoresets(convert=False)
|
||||||
|
self.assert_autoresets(convert=True, autoreset=False)
|
||||||
|
self.assert_autoresets(convert=False, autoreset=False)
|
||||||
|
|
||||||
|
def testWriteAndConvertWritesPlainText(self):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
stream.write_and_convert( 'abc' )
|
||||||
|
self.assertEqual( stream.wrapped.write.call_args, (('abc',), {}) )
|
||||||
|
|
||||||
|
def testWriteAndConvertStripsAllValidAnsi(self):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
stream.call_win32 = Mock()
|
||||||
|
data = [
|
||||||
|
'abc\033[mdef',
|
||||||
|
'abc\033[0mdef',
|
||||||
|
'abc\033[2mdef',
|
||||||
|
'abc\033[02mdef',
|
||||||
|
'abc\033[002mdef',
|
||||||
|
'abc\033[40mdef',
|
||||||
|
'abc\033[040mdef',
|
||||||
|
'abc\033[0;1mdef',
|
||||||
|
'abc\033[40;50mdef',
|
||||||
|
'abc\033[50;30;40mdef',
|
||||||
|
'abc\033[Adef',
|
||||||
|
'abc\033[0Gdef',
|
||||||
|
'abc\033[1;20;128Hdef',
|
||||||
|
]
|
||||||
|
for datum in data:
|
||||||
|
stream.wrapped.write.reset_mock()
|
||||||
|
stream.write_and_convert( datum )
|
||||||
|
self.assertEqual(
|
||||||
|
[args[0] for args in stream.wrapped.write.call_args_list],
|
||||||
|
[ ('abc',), ('def',) ]
|
||||||
|
)
|
||||||
|
|
||||||
|
def testWriteAndConvertSkipsEmptySnippets(self):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
stream.call_win32 = Mock()
|
||||||
|
stream.write_and_convert( '\033[40m\033[41m' )
|
||||||
|
self.assertFalse( stream.wrapped.write.called )
|
||||||
|
|
||||||
|
def testWriteAndConvertCallsWin32WithParamsAndCommand(self):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
stream.convert = True
|
||||||
|
stream.call_win32 = Mock()
|
||||||
|
stream.extract_params = Mock(return_value='params')
|
||||||
|
data = {
|
||||||
|
'abc\033[adef': ('a', 'params'),
|
||||||
|
'abc\033[;;bdef': ('b', 'params'),
|
||||||
|
'abc\033[0cdef': ('c', 'params'),
|
||||||
|
'abc\033[;;0;;Gdef': ('G', 'params'),
|
||||||
|
'abc\033[1;20;128Hdef': ('H', 'params'),
|
||||||
|
}
|
||||||
|
for datum, expected in data.items():
|
||||||
|
stream.call_win32.reset_mock()
|
||||||
|
stream.write_and_convert( datum )
|
||||||
|
self.assertEqual( stream.call_win32.call_args[0], expected )
|
||||||
|
|
||||||
|
def test_reset_all_shouldnt_raise_on_closed_orig_stdout(self):
|
||||||
|
stream = StringIO()
|
||||||
|
converter = AnsiToWin32(stream)
|
||||||
|
stream.close()
|
||||||
|
|
||||||
|
converter.reset_all()
|
||||||
|
|
||||||
|
def test_wrap_shouldnt_raise_on_closed_orig_stdout(self):
|
||||||
|
stream = StringIO()
|
||||||
|
stream.close()
|
||||||
|
with \
|
||||||
|
patch("colorama.ansitowin32.os.name", "nt"), \
|
||||||
|
patch("colorama.ansitowin32.winapi_test", lambda: True):
|
||||||
|
converter = AnsiToWin32(stream)
|
||||||
|
self.assertTrue(converter.strip)
|
||||||
|
self.assertFalse(converter.convert)
|
||||||
|
|
||||||
|
def test_wrap_shouldnt_raise_on_missing_closed_attr(self):
|
||||||
|
with \
|
||||||
|
patch("colorama.ansitowin32.os.name", "nt"), \
|
||||||
|
patch("colorama.ansitowin32.winapi_test", lambda: True):
|
||||||
|
converter = AnsiToWin32(object())
|
||||||
|
self.assertTrue(converter.strip)
|
||||||
|
self.assertFalse(converter.convert)
|
||||||
|
|
||||||
|
def testExtractParams(self):
|
||||||
|
stream = AnsiToWin32(Mock())
|
||||||
|
data = {
|
||||||
|
'': (0,),
|
||||||
|
';;': (0,),
|
||||||
|
'2': (2,),
|
||||||
|
';;002;;': (2,),
|
||||||
|
'0;1': (0, 1),
|
||||||
|
';;003;;456;;': (3, 456),
|
||||||
|
'11;22;33;44;55': (11, 22, 33, 44, 55),
|
||||||
|
}
|
||||||
|
for datum, expected in data.items():
|
||||||
|
self.assertEqual(stream.extract_params('m', datum), expected)
|
||||||
|
|
||||||
|
def testCallWin32UsesLookup(self):
|
||||||
|
listener = Mock()
|
||||||
|
stream = AnsiToWin32(listener)
|
||||||
|
stream.win32_calls = {
|
||||||
|
1: (lambda *_, **__: listener(11),),
|
||||||
|
2: (lambda *_, **__: listener(22),),
|
||||||
|
3: (lambda *_, **__: listener(33),),
|
||||||
|
}
|
||||||
|
stream.call_win32('m', (3, 1, 99, 2))
|
||||||
|
self.assertEqual(
|
||||||
|
[a[0][0] for a in listener.call_args_list],
|
||||||
|
[33, 11, 22] )
|
||||||
|
|
||||||
|
def test_osc_codes(self):
|
||||||
|
mockStdout = Mock()
|
||||||
|
stream = AnsiToWin32(mockStdout, convert=True)
|
||||||
|
with patch('colorama.ansitowin32.winterm') as winterm:
|
||||||
|
data = [
|
||||||
|
'\033]0\x07', # missing arguments
|
||||||
|
'\033]0;foo\x08', # wrong OSC command
|
||||||
|
'\033]0;colorama_test_title\x07', # should work
|
||||||
|
'\033]1;colorama_test_title\x07', # wrong set command
|
||||||
|
'\033]2;colorama_test_title\x07', # should work
|
||||||
|
'\033]' + ';' * 64 + '\x08', # see issue #247
|
||||||
|
]
|
||||||
|
for code in data:
|
||||||
|
stream.write(code)
|
||||||
|
self.assertEqual(winterm.set_title.call_count, 2)
|
||||||
|
|
||||||
|
def test_native_windows_ansi(self):
|
||||||
|
with ExitStack() as stack:
|
||||||
|
def p(a, b):
|
||||||
|
stack.enter_context(patch(a, b, create=True))
|
||||||
|
# Pretend to be on Windows
|
||||||
|
p("colorama.ansitowin32.os.name", "nt")
|
||||||
|
p("colorama.ansitowin32.winapi_test", lambda: True)
|
||||||
|
p("colorama.win32.winapi_test", lambda: True)
|
||||||
|
p("colorama.winterm.win32.windll", "non-None")
|
||||||
|
p("colorama.winterm.get_osfhandle", lambda _: 1234)
|
||||||
|
|
||||||
|
# Pretend that our mock stream has native ANSI support
|
||||||
|
p(
|
||||||
|
"colorama.winterm.win32.GetConsoleMode",
|
||||||
|
lambda _: ENABLE_VIRTUAL_TERMINAL_PROCESSING,
|
||||||
|
)
|
||||||
|
SetConsoleMode = Mock()
|
||||||
|
p("colorama.winterm.win32.SetConsoleMode", SetConsoleMode)
|
||||||
|
|
||||||
|
stdout = Mock()
|
||||||
|
stdout.closed = False
|
||||||
|
stdout.isatty.return_value = True
|
||||||
|
stdout.fileno.return_value = 1
|
||||||
|
|
||||||
|
# Our fake console says it has native vt support, so AnsiToWin32 should
|
||||||
|
# enable that support and do nothing else.
|
||||||
|
stream = AnsiToWin32(stdout)
|
||||||
|
SetConsoleMode.assert_called_with(1234, ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
||||||
|
self.assertFalse(stream.strip)
|
||||||
|
self.assertFalse(stream.convert)
|
||||||
|
self.assertFalse(stream.should_wrap())
|
||||||
|
|
||||||
|
# Now let's pretend we're on an old Windows console, that doesn't have
|
||||||
|
# native ANSI support.
|
||||||
|
p("colorama.winterm.win32.GetConsoleMode", lambda _: 0)
|
||||||
|
SetConsoleMode = Mock()
|
||||||
|
p("colorama.winterm.win32.SetConsoleMode", SetConsoleMode)
|
||||||
|
|
||||||
|
stream = AnsiToWin32(stdout)
|
||||||
|
SetConsoleMode.assert_called_with(1234, ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
||||||
|
self.assertTrue(stream.strip)
|
||||||
|
self.assertTrue(stream.convert)
|
||||||
|
self.assertTrue(stream.should_wrap())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
189
venv/Lib/site-packages/colorama/tests/initialise_test.py
Normal file
189
venv/Lib/site-packages/colorama/tests/initialise_test.py
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
|
||||||
|
import sys
|
||||||
|
from unittest import TestCase, main, skipUnless
|
||||||
|
|
||||||
|
try:
|
||||||
|
from unittest.mock import patch, Mock
|
||||||
|
except ImportError:
|
||||||
|
from mock import patch, Mock
|
||||||
|
|
||||||
|
from ..ansitowin32 import StreamWrapper
|
||||||
|
from ..initialise import init, just_fix_windows_console, _wipe_internal_state_for_tests
|
||||||
|
from .utils import osname, replace_by
|
||||||
|
|
||||||
|
orig_stdout = sys.stdout
|
||||||
|
orig_stderr = sys.stderr
|
||||||
|
|
||||||
|
|
||||||
|
class InitTest(TestCase):
|
||||||
|
|
||||||
|
@skipUnless(sys.stdout.isatty(), "sys.stdout is not a tty")
|
||||||
|
def setUp(self):
|
||||||
|
# sanity check
|
||||||
|
self.assertNotWrapped()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
_wipe_internal_state_for_tests()
|
||||||
|
sys.stdout = orig_stdout
|
||||||
|
sys.stderr = orig_stderr
|
||||||
|
|
||||||
|
def assertWrapped(self):
|
||||||
|
self.assertIsNot(sys.stdout, orig_stdout, 'stdout should be wrapped')
|
||||||
|
self.assertIsNot(sys.stderr, orig_stderr, 'stderr should be wrapped')
|
||||||
|
self.assertTrue(isinstance(sys.stdout, StreamWrapper),
|
||||||
|
'bad stdout wrapper')
|
||||||
|
self.assertTrue(isinstance(sys.stderr, StreamWrapper),
|
||||||
|
'bad stderr wrapper')
|
||||||
|
|
||||||
|
def assertNotWrapped(self):
|
||||||
|
self.assertIs(sys.stdout, orig_stdout, 'stdout should not be wrapped')
|
||||||
|
self.assertIs(sys.stderr, orig_stderr, 'stderr should not be wrapped')
|
||||||
|
|
||||||
|
@patch('colorama.initialise.reset_all')
|
||||||
|
@patch('colorama.ansitowin32.winapi_test', lambda *_: True)
|
||||||
|
@patch('colorama.ansitowin32.enable_vt_processing', lambda *_: False)
|
||||||
|
def testInitWrapsOnWindows(self, _):
|
||||||
|
with osname("nt"):
|
||||||
|
init()
|
||||||
|
self.assertWrapped()
|
||||||
|
|
||||||
|
@patch('colorama.initialise.reset_all')
|
||||||
|
@patch('colorama.ansitowin32.winapi_test', lambda *_: False)
|
||||||
|
def testInitDoesntWrapOnEmulatedWindows(self, _):
|
||||||
|
with osname("nt"):
|
||||||
|
init()
|
||||||
|
self.assertNotWrapped()
|
||||||
|
|
||||||
|
def testInitDoesntWrapOnNonWindows(self):
|
||||||
|
with osname("posix"):
|
||||||
|
init()
|
||||||
|
self.assertNotWrapped()
|
||||||
|
|
||||||
|
def testInitDoesntWrapIfNone(self):
|
||||||
|
with replace_by(None):
|
||||||
|
init()
|
||||||
|
# We can't use assertNotWrapped here because replace_by(None)
|
||||||
|
# changes stdout/stderr already.
|
||||||
|
self.assertIsNone(sys.stdout)
|
||||||
|
self.assertIsNone(sys.stderr)
|
||||||
|
|
||||||
|
def testInitAutoresetOnWrapsOnAllPlatforms(self):
|
||||||
|
with osname("posix"):
|
||||||
|
init(autoreset=True)
|
||||||
|
self.assertWrapped()
|
||||||
|
|
||||||
|
def testInitWrapOffDoesntWrapOnWindows(self):
|
||||||
|
with osname("nt"):
|
||||||
|
init(wrap=False)
|
||||||
|
self.assertNotWrapped()
|
||||||
|
|
||||||
|
def testInitWrapOffIncompatibleWithAutoresetOn(self):
|
||||||
|
self.assertRaises(ValueError, lambda: init(autoreset=True, wrap=False))
|
||||||
|
|
||||||
|
@patch('colorama.win32.SetConsoleTextAttribute')
|
||||||
|
@patch('colorama.initialise.AnsiToWin32')
|
||||||
|
def testAutoResetPassedOn(self, mockATW32, _):
|
||||||
|
with osname("nt"):
|
||||||
|
init(autoreset=True)
|
||||||
|
self.assertEqual(len(mockATW32.call_args_list), 2)
|
||||||
|
self.assertEqual(mockATW32.call_args_list[1][1]['autoreset'], True)
|
||||||
|
self.assertEqual(mockATW32.call_args_list[0][1]['autoreset'], True)
|
||||||
|
|
||||||
|
@patch('colorama.initialise.AnsiToWin32')
|
||||||
|
def testAutoResetChangeable(self, mockATW32):
|
||||||
|
with osname("nt"):
|
||||||
|
init()
|
||||||
|
|
||||||
|
init(autoreset=True)
|
||||||
|
self.assertEqual(len(mockATW32.call_args_list), 4)
|
||||||
|
self.assertEqual(mockATW32.call_args_list[2][1]['autoreset'], True)
|
||||||
|
self.assertEqual(mockATW32.call_args_list[3][1]['autoreset'], True)
|
||||||
|
|
||||||
|
init()
|
||||||
|
self.assertEqual(len(mockATW32.call_args_list), 6)
|
||||||
|
self.assertEqual(
|
||||||
|
mockATW32.call_args_list[4][1]['autoreset'], False)
|
||||||
|
self.assertEqual(
|
||||||
|
mockATW32.call_args_list[5][1]['autoreset'], False)
|
||||||
|
|
||||||
|
|
||||||
|
@patch('colorama.initialise.atexit.register')
|
||||||
|
def testAtexitRegisteredOnlyOnce(self, mockRegister):
|
||||||
|
init()
|
||||||
|
self.assertTrue(mockRegister.called)
|
||||||
|
mockRegister.reset_mock()
|
||||||
|
init()
|
||||||
|
self.assertFalse(mockRegister.called)
|
||||||
|
|
||||||
|
|
||||||
|
class JustFixWindowsConsoleTest(TestCase):
|
||||||
|
def _reset(self):
|
||||||
|
_wipe_internal_state_for_tests()
|
||||||
|
sys.stdout = orig_stdout
|
||||||
|
sys.stderr = orig_stderr
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self._reset()
|
||||||
|
|
||||||
|
@patch("colorama.ansitowin32.winapi_test", lambda: True)
|
||||||
|
def testJustFixWindowsConsole(self):
|
||||||
|
if sys.platform != "win32":
|
||||||
|
# just_fix_windows_console should be a no-op
|
||||||
|
just_fix_windows_console()
|
||||||
|
self.assertIs(sys.stdout, orig_stdout)
|
||||||
|
self.assertIs(sys.stderr, orig_stderr)
|
||||||
|
else:
|
||||||
|
def fake_std():
|
||||||
|
# Emulate stdout=not a tty, stderr=tty
|
||||||
|
# to check that we handle both cases correctly
|
||||||
|
stdout = Mock()
|
||||||
|
stdout.closed = False
|
||||||
|
stdout.isatty.return_value = False
|
||||||
|
stdout.fileno.return_value = 1
|
||||||
|
sys.stdout = stdout
|
||||||
|
|
||||||
|
stderr = Mock()
|
||||||
|
stderr.closed = False
|
||||||
|
stderr.isatty.return_value = True
|
||||||
|
stderr.fileno.return_value = 2
|
||||||
|
sys.stderr = stderr
|
||||||
|
|
||||||
|
for native_ansi in [False, True]:
|
||||||
|
with patch(
|
||||||
|
'colorama.ansitowin32.enable_vt_processing',
|
||||||
|
lambda *_: native_ansi
|
||||||
|
):
|
||||||
|
self._reset()
|
||||||
|
fake_std()
|
||||||
|
|
||||||
|
# Regular single-call test
|
||||||
|
prev_stdout = sys.stdout
|
||||||
|
prev_stderr = sys.stderr
|
||||||
|
just_fix_windows_console()
|
||||||
|
self.assertIs(sys.stdout, prev_stdout)
|
||||||
|
if native_ansi:
|
||||||
|
self.assertIs(sys.stderr, prev_stderr)
|
||||||
|
else:
|
||||||
|
self.assertIsNot(sys.stderr, prev_stderr)
|
||||||
|
|
||||||
|
# second call without resetting is always a no-op
|
||||||
|
prev_stdout = sys.stdout
|
||||||
|
prev_stderr = sys.stderr
|
||||||
|
just_fix_windows_console()
|
||||||
|
self.assertIs(sys.stdout, prev_stdout)
|
||||||
|
self.assertIs(sys.stderr, prev_stderr)
|
||||||
|
|
||||||
|
self._reset()
|
||||||
|
fake_std()
|
||||||
|
|
||||||
|
# If init() runs first, just_fix_windows_console should be a no-op
|
||||||
|
init()
|
||||||
|
prev_stdout = sys.stdout
|
||||||
|
prev_stderr = sys.stderr
|
||||||
|
just_fix_windows_console()
|
||||||
|
self.assertIs(prev_stdout, sys.stdout)
|
||||||
|
self.assertIs(prev_stderr, sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
57
venv/Lib/site-packages/colorama/tests/isatty_test.py
Normal file
57
venv/Lib/site-packages/colorama/tests/isatty_test.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
|
||||||
|
import sys
|
||||||
|
from unittest import TestCase, main
|
||||||
|
|
||||||
|
from ..ansitowin32 import StreamWrapper, AnsiToWin32
|
||||||
|
from .utils import pycharm, replace_by, replace_original_by, StreamTTY, StreamNonTTY
|
||||||
|
|
||||||
|
|
||||||
|
def is_a_tty(stream):
|
||||||
|
return StreamWrapper(stream, None).isatty()
|
||||||
|
|
||||||
|
class IsattyTest(TestCase):
|
||||||
|
|
||||||
|
def test_TTY(self):
|
||||||
|
tty = StreamTTY()
|
||||||
|
self.assertTrue(is_a_tty(tty))
|
||||||
|
with pycharm():
|
||||||
|
self.assertTrue(is_a_tty(tty))
|
||||||
|
|
||||||
|
def test_nonTTY(self):
|
||||||
|
non_tty = StreamNonTTY()
|
||||||
|
self.assertFalse(is_a_tty(non_tty))
|
||||||
|
with pycharm():
|
||||||
|
self.assertFalse(is_a_tty(non_tty))
|
||||||
|
|
||||||
|
def test_withPycharm(self):
|
||||||
|
with pycharm():
|
||||||
|
self.assertTrue(is_a_tty(sys.stderr))
|
||||||
|
self.assertTrue(is_a_tty(sys.stdout))
|
||||||
|
|
||||||
|
def test_withPycharmTTYOverride(self):
|
||||||
|
tty = StreamTTY()
|
||||||
|
with pycharm(), replace_by(tty):
|
||||||
|
self.assertTrue(is_a_tty(tty))
|
||||||
|
|
||||||
|
def test_withPycharmNonTTYOverride(self):
|
||||||
|
non_tty = StreamNonTTY()
|
||||||
|
with pycharm(), replace_by(non_tty):
|
||||||
|
self.assertFalse(is_a_tty(non_tty))
|
||||||
|
|
||||||
|
def test_withPycharmNoneOverride(self):
|
||||||
|
with pycharm():
|
||||||
|
with replace_by(None), replace_original_by(None):
|
||||||
|
self.assertFalse(is_a_tty(None))
|
||||||
|
self.assertFalse(is_a_tty(StreamNonTTY()))
|
||||||
|
self.assertTrue(is_a_tty(StreamTTY()))
|
||||||
|
|
||||||
|
def test_withPycharmStreamWrapped(self):
|
||||||
|
with pycharm():
|
||||||
|
self.assertTrue(AnsiToWin32(StreamTTY()).stream.isatty())
|
||||||
|
self.assertFalse(AnsiToWin32(StreamNonTTY()).stream.isatty())
|
||||||
|
self.assertTrue(AnsiToWin32(sys.stdout).stream.isatty())
|
||||||
|
self.assertTrue(AnsiToWin32(sys.stderr).stream.isatty())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user