| 1 |
import os |
| 2 |
from base64 import b64encode |
| 3 |
from tempfile import NamedTemporaryFile |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import t.skip |
| 9 |
from celery.utils import term |
| 10 |
from celery.utils.term import _read_as_base64, colored, fg, supports_images |
| 11 |
|
| 12 |
|
| 13 |
@t.skip.if_win32 |
| 14 |
class test_colored: |
| 15 |
|
| 16 |
@pytest.fixture(autouse=True) |
| 17 |
def preserve_encoding(self, patching): |
| 18 |
patching('sys.getdefaultencoding', 'utf-8') |
| 19 |
|
| 20 |
@pytest.mark.parametrize('name,color', [ |
| 21 |
('black', term.BLACK), |
| 22 |
('red', term.RED), |
| 23 |
('green', term.GREEN), |
| 24 |
('yellow', term.YELLOW), |
| 25 |
('blue', term.BLUE), |
| 26 |
('magenta', term.MAGENTA), |
| 27 |
('cyan', term.CYAN), |
| 28 |
('white', term.WHITE), |
| 29 |
]) |
| 30 |
def test_colors(self, name, color): |
| 31 |
assert fg(30 + color) in str(colored().names[name]('foo')) |
| 32 |
|
| 33 |
@pytest.mark.parametrize('name', [ |
| 34 |
'bold', 'underline', 'blink', 'reverse', 'bright', |
| 35 |
'ired', 'igreen', 'iyellow', 'iblue', 'imagenta', |
| 36 |
'icyan', 'iwhite', 'reset', |
| 37 |
]) |
| 38 |
def test_modifiers(self, name): |
| 39 |
assert str(getattr(colored(), name)('f')) |
| 40 |
|
| 41 |
def test_unicode(self): |
| 42 |
assert str(colored().green('∂bar')) |
| 43 |
assert colored().red('éefoo') + colored().green('∂bar') |
| 44 |
assert colored().red('foo').no_color() == 'foo' |
| 45 |
|
| 46 |
def test_repr(self): |
| 47 |
assert repr(colored().blue('åfoo')) |
| 48 |
assert "''" in repr(colored()) |
| 49 |
|
| 50 |
def test_more_unicode(self): |
| 51 |
c = colored() |
| 52 |
s = c.red('foo', c.blue('bar'), c.green('baz')) |
| 53 |
assert s.no_color() |
| 54 |
c._fold_no_color(s, 'øfoo') |
| 55 |
c._fold_no_color('fooå', s) |
| 56 |
|
| 57 |
c = colored().red('åfoo') |
| 58 |
assert c._add(c, 'baræ') == '\x1b[1;31m\xe5foo\x1b[0mbar\xe6' |
| 59 |
|
| 60 |
c2 = colored().blue('ƒƒz') |
| 61 |
c3 = c._add(c, c2) |
| 62 |
assert c3 == '\x1b[1;31m\xe5foo\x1b[0m\x1b[1;34m\u0192\u0192z\x1b[0m' |
| 63 |
|
| 64 |
def test_read_as_base64(self): |
| 65 |
test_data = b"The quick brown fox jumps over the lazy dog" |
| 66 |
with NamedTemporaryFile(mode='wb') as temp_file: |
| 67 |
temp_file.write(test_data) |
| 68 |
temp_file.seek(0) |
| 69 |
temp_file_path = temp_file.name |
| 70 |
|
| 71 |
result = _read_as_base64(temp_file_path) |
| 72 |
expected_result = b64encode(test_data).decode('ascii') |
| 73 |
|
| 74 |
assert result == expected_result |
| 75 |
|
| 76 |
@pytest.mark.parametrize('is_tty, iterm_profile, expected', [ |
| 77 |
(True, 'test_profile', True), |
| 78 |
(False, 'test_profile', False), |
| 79 |
(True, None, False), |
| 80 |
]) |
| 81 |
@patch('sys.stdin.isatty') |
| 82 |
@patch.dict(os.environ, {'ITERM_PROFILE': 'test_profile'}, clear=True) |
| 83 |
def test_supports_images(self, mock_isatty, is_tty, iterm_profile, expected): |
| 84 |
mock_isatty.return_value = is_tty |
| 85 |
if iterm_profile is None: |
| 86 |
del os.environ['ITERM_PROFILE'] |
| 87 |
assert supports_images() == expected |