colors_test.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import os
  2. import pytest
  3. from compose.cli.colors import AnsiMode
  4. from tests import mock
  5. @pytest.fixture
  6. def tty_stream():
  7. stream = mock.Mock()
  8. stream.isatty.return_value = True
  9. return stream
  10. @pytest.fixture
  11. def non_tty_stream():
  12. stream = mock.Mock()
  13. stream.isatty.return_value = False
  14. return stream
  15. class TestAnsiModeTestCase:
  16. @mock.patch.dict(os.environ)
  17. def test_ansi_mode_never(self, tty_stream, non_tty_stream):
  18. if "CLICOLOR" in os.environ:
  19. del os.environ["CLICOLOR"]
  20. assert not AnsiMode.NEVER.use_ansi_codes(tty_stream)
  21. assert not AnsiMode.NEVER.use_ansi_codes(non_tty_stream)
  22. os.environ["CLICOLOR"] = "0"
  23. assert not AnsiMode.NEVER.use_ansi_codes(tty_stream)
  24. assert not AnsiMode.NEVER.use_ansi_codes(non_tty_stream)
  25. @mock.patch.dict(os.environ)
  26. def test_ansi_mode_always(self, tty_stream, non_tty_stream):
  27. if "CLICOLOR" in os.environ:
  28. del os.environ["CLICOLOR"]
  29. assert AnsiMode.ALWAYS.use_ansi_codes(tty_stream)
  30. assert AnsiMode.ALWAYS.use_ansi_codes(non_tty_stream)
  31. os.environ["CLICOLOR"] = "0"
  32. assert AnsiMode.ALWAYS.use_ansi_codes(tty_stream)
  33. assert AnsiMode.ALWAYS.use_ansi_codes(non_tty_stream)
  34. @mock.patch.dict(os.environ)
  35. def test_ansi_mode_auto(self, tty_stream, non_tty_stream):
  36. if "CLICOLOR" in os.environ:
  37. del os.environ["CLICOLOR"]
  38. assert AnsiMode.AUTO.use_ansi_codes(tty_stream)
  39. assert not AnsiMode.AUTO.use_ansi_codes(non_tty_stream)
  40. os.environ["CLICOLOR"] = "0"
  41. assert not AnsiMode.AUTO.use_ansi_codes(tty_stream)
  42. assert not AnsiMode.AUTO.use_ansi_codes(non_tty_stream)