Skip to content

Commit

Permalink
Refactor test and make it work in CI (no console)
Browse files Browse the repository at this point in the history
  • Loading branch information
njsmith committed Oct 17, 2022
1 parent 1a82924 commit 78bce18
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions colorama/tests/initialise_test.py
Expand Up @@ -125,14 +125,15 @@ def _reset(self):
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:
for native_ansi in [False, True]:
def fake_std():
# Emulate stdout=not a tty, stderr=tty
# to check that we handle both cases correctly
stdout = Mock()
Expand All @@ -147,17 +148,23 @@ def testJustFixWindowsConsole(self):
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, stdout)
self.assertIs(sys.stdout, prev_stdout)
if native_ansi:
self.assertIs(sys.stderr, stderr)
self.assertIs(sys.stderr, prev_stderr)
else:
self.assertIsNot(sys.stderr, stderr)
self.assertIsNot(sys.stderr, prev_stderr)

# second call without resetting is always a no-op
prev_stdout = sys.stdout
Expand All @@ -167,6 +174,7 @@ def testJustFixWindowsConsole(self):
self.assertIs(sys.stderr, prev_stderr)

self._reset()
fake_std()

# If init() runs first, just_fix_windows_console should be a no-op
init()
Expand All @@ -176,8 +184,6 @@ def testJustFixWindowsConsole(self):
self.assertIs(prev_stdout, sys.stdout)
self.assertIs(prev_stderr, sys.stderr)

self._reset()


if __name__ == '__main__':
main()

0 comments on commit 78bce18

Please sign in to comment.