|
| 1 | +import io |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import devtools.prettier |
| 6 | +from devtools import Debug, pformat, pprint, sprint |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize('printer', ['debug', 'pprint']) |
| 10 | +@pytest.mark.parametrize('tty', [False, True]) |
| 11 | +@pytest.mark.parametrize('no_color', [None, '', '1', '0', 'false']) |
| 12 | +def test_no_color_default(monkeypatch: pytest.MonkeyPatch, printer: str, tty: bool, no_color: str | None): |
| 13 | + monkeypatch.delenv('PY_DEVTOOLS_HIGHLIGHT', raising=False) |
| 14 | + monkeypatch.setattr(devtools.prettier, 'force_highlight', None) |
| 15 | + if no_color is None: |
| 16 | + monkeypatch.delenv('NO_COLOR', raising=False) |
| 17 | + else: |
| 18 | + monkeypatch.setenv('NO_COLOR', no_color) |
| 19 | + output = io.StringIO() |
| 20 | + monkeypatch.setattr(output, 'isatty', lambda: tty) |
| 21 | + monkeypatch.setattr('devtools.utils.activate_win_color', lambda: True) |
| 22 | + |
| 23 | + if printer == 'debug': |
| 24 | + Debug()(1, file_=output) |
| 25 | + else: |
| 26 | + pprint(1, file=output) |
| 27 | + |
| 28 | + assert ('\x1b[' in output.getvalue()) is (tty and not no_color) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize('printer', ['debug', 'pprint']) |
| 32 | +@pytest.mark.parametrize('tty', [False, True]) |
| 33 | +@pytest.mark.parametrize('highlight', [False, True]) |
| 34 | +def test_no_color_config_override(monkeypatch: pytest.MonkeyPatch, printer: str, tty: bool, highlight: bool): |
| 35 | + monkeypatch.setenv('NO_COLOR', '1') |
| 36 | + monkeypatch.setenv('PY_DEVTOOLS_HIGHLIGHT', str(highlight)) |
| 37 | + monkeypatch.setattr(devtools.prettier, 'force_highlight', highlight) |
| 38 | + output = io.StringIO() |
| 39 | + monkeypatch.setattr(output, 'isatty', lambda: tty) |
| 40 | + |
| 41 | + if printer == 'debug': |
| 42 | + Debug()(1, file_=output) |
| 43 | + else: |
| 44 | + pprint(1, file=output) |
| 45 | + |
| 46 | + assert ('\x1b[' in output.getvalue()) is highlight |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize('highlight', [False, True]) |
| 50 | +@pytest.mark.parametrize('tty', [False, True]) |
| 51 | +def test_no_color_explicit_debug_override(monkeypatch: pytest.MonkeyPatch, highlight: bool, tty: bool): |
| 52 | + monkeypatch.setenv('NO_COLOR', '1') |
| 53 | + monkeypatch.setenv('PY_DEVTOOLS_HIGHLIGHT', str(not highlight)) |
| 54 | + output = io.StringIO() |
| 55 | + monkeypatch.setattr(output, 'isatty', lambda: tty) |
| 56 | + |
| 57 | + Debug(highlight=highlight)(1, file_=output) |
| 58 | + |
| 59 | + assert ('\x1b[' in output.getvalue()) is highlight |
| 60 | + |
| 61 | + |
| 62 | +def test_no_color_explicit_pformat_override(monkeypatch: pytest.MonkeyPatch): |
| 63 | + monkeypatch.setenv('NO_COLOR', '1') |
| 64 | + assert '\x1b[' not in pformat(1) |
| 65 | + assert '\x1b[' in pformat(1, highlight=True) |
| 66 | + |
| 67 | + |
| 68 | +def test_pprint_highlight_config_snapshot(monkeypatch: pytest.MonkeyPatch): |
| 69 | + monkeypatch.delenv('NO_COLOR', raising=False) |
| 70 | + monkeypatch.setenv('PY_DEVTOOLS_HIGHLIGHT', 'True') |
| 71 | + monkeypatch.setattr(devtools.prettier, 'force_highlight', None) |
| 72 | + output = io.StringIO() |
| 73 | + monkeypatch.setattr(output, 'isatty', lambda: False) |
| 74 | + |
| 75 | + pprint(1, file=output) |
| 76 | + |
| 77 | + assert output.getvalue() == '1\n' |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize('no_color', [None, '', '1']) |
| 81 | +def test_sprint_no_color(monkeypatch: pytest.MonkeyPatch, no_color: str | None): |
| 82 | + if no_color is None: |
| 83 | + monkeypatch.delenv('NO_COLOR', raising=False) |
| 84 | + else: |
| 85 | + monkeypatch.setenv('NO_COLOR', no_color) |
| 86 | + output = io.StringIO() |
| 87 | + monkeypatch.setattr(output, 'isatty', lambda: True) |
| 88 | + |
| 89 | + sprint('hello', sprint.green, file=output) |
| 90 | + |
| 91 | + assert ('\x1b[' in output.getvalue()) is (not no_color) |
0 commit comments