Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions fire/formatting_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

"""Tests for formatting.py."""

from unittest import mock

from fire import formatting
from fire import formatting_windows
from fire import testutils

LINE_LENGTH = 80
Expand Down Expand Up @@ -74,5 +77,44 @@ def test_ellipsis_middle_truncate_not_enough_space(self):
self.assertEqual('1000000000L', truncated_text)


class FormattingWindowsTest(testutils.BaseTestCase):

def test_std_output_handle_constant(self):
self.assertEqual(formatting_windows.STD_OUTPUT_HANDLE, -11)

@mock.patch.object(formatting_windows, 'HAS_COLORAMA', True)
@mock.patch.object(formatting_windows, 'colorama', create=True)
@mock.patch('platform.release', return_value='10')
@mock.patch('sys.stdout.isatty', return_value=True)
def test_initialize_or_disable_windows_10(
self, mock_isatty, mock_release, mock_colorama
):
fake_kernel32 = mock.MagicMock()
fake_kernel32.GetStdHandle.return_value = 42
fake_kernel32.GetConsoleMode.return_value = 1
fake_kernel32.SetConsoleMode.return_value = 1

with mock.patch.object(
formatting_windows.ctypes, 'windll', create=True
) as mock_windll:
mock_windll.kernel32 = fake_kernel32
with mock.patch.object(
formatting_windows.ctypes, 'wintypes', create=True
) as mock_wintypes:
with mock.patch.object(
formatting_windows.ctypes, 'byref', return_value=mock.MagicMock()
):
mock_dword = mock.MagicMock()
mock_dword.value = 0
mock_wintypes.DWORD.return_value = mock_dword

formatting_windows.initialize_or_disable()

fake_kernel32.GetStdHandle.assert_called_once_with(
formatting_windows.STD_OUTPUT_HANDLE
)
mock_colorama.init.assert_called_once_with(wrap=False)


if __name__ == '__main__':
testutils.main()
5 changes: 3 additions & 2 deletions fire/formatting_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import ctypes
import os
import platform
import subprocess
import sys

try:
Expand All @@ -26,6 +25,8 @@
except ImportError:
HAS_COLORAMA = False

STD_OUTPUT_HANDLE = -11


def initialize_or_disable():
"""Enables ANSI processing on Windows or disables it as needed."""
Expand All @@ -40,7 +41,7 @@ def initialize_or_disable():
wrap = False
kernel32 = ctypes.windll.kernel32
enable_virtual_terminal_processing = 0x04
out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE) # pylint: disable=line-too-long,
out_handle = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
# GetConsoleMode fails if the terminal isn't native.
mode = ctypes.wintypes.DWORD()
if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0:
Expand Down
Loading