Skip to content

Commit

Permalink
Merge branch 'windows-term-width' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ljcooke committed Apr 24, 2016
2 parents fbb371a + be36e4d commit cd47b05
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include see.py setup.py README.rst CHANGELOG.rst AUTHORS.rst LICENSE
include see.py setup.py tests/test_*.py README.rst CHANGELOG.rst AUTHORS.rst LICENSE
24 changes: 19 additions & 5 deletions see.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,28 @@
"""
import fnmatch
import inspect
import platform
import re
import struct
import sys
import textwrap

try:
import fcntl
import termios
if platform.system() == 'Windows':
from ctypes import windll, create_string_buffer
fcntl, termios = None, None
else:
import fcntl, termios
windll, create_string_buffer = None, None
except ImportError:
fcntl = None
termios = None
fcntl, termios = None, None
windll, create_string_buffer = None, None

__all__ = ['see']

__author__ = 'Liam Cooke'
__contributors__ = 'See AUTHORS.rst'
__version__ = '1.2.0'
__version__ = '1.3.0-alpha.1'
__copyright__ = 'Copyright (c) 2009-2016 Liam Cooke'
__license__ = 'BSD License'

Expand All @@ -73,6 +78,15 @@ def term_width():
return width
except IOError:
pass
elif windll and create_string_buffer:
stderr_handle, struct_size = -12, 22
handle = windll.kernel32.GetStdHandle(stderr_handle)
csbi = create_string_buffer(struct_size)
res = windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
if res:
(_, _, _, _, _, left, _, right, _,
_, _) = struct.unpack('hhhhHhhhhhh', csbi.raw)
return right - left + 1


def line_width(default_width=DEFAULT_LINE_WIDTH, max_width=MAX_LINE_WIDTH):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
]

setup(name='see',
version='1.2.0',
version='1.3.0-alpha.1',
description='dir for humans',
author='Liam Cooke',
author_email='[email protected]',
Expand Down
56 changes: 47 additions & 9 deletions tests/test_term_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"""
Unit tests for getting information about the terminal.
There are separate test cases to simulate non-Unixlike environments, such as
Windows, where information about the terminal is not easily available. To
do this, we prevent Python from importing some modules while it loads see.
There are separate test cases to simulate unsupported environments (not
Unixlike or Windows), where information about the terminal is not easily
available. To do this, we prevent Python from importing some modules while it
loads see.
"""
import platform
import sys
from imp import reload

Expand All @@ -28,7 +30,8 @@
import see


UNIXLIKE_MODULES = (
MOCK_EXCLUDE_MODULES = (
'ctypes',
'fcntl',
'termios',
)
Expand All @@ -43,18 +46,42 @@ def mock_import(name,
locals=None if PY3 else {},
fromlist=None if PY3 else [],
level=0 if PY3 else -1):
if name in UNIXLIKE_MODULES:
if name in MOCK_EXCLUDE_MODULES:
raise ImportError
return REAL_IMPORT(name, globals, locals, fromlist, level)


class TestUnixlike(unittest.TestCase):
class TestSupportedTerminal(unittest.TestCase):

def setUp(self):
self.system = platform.system()
self.windows = (self.system == 'Windows')

def test_system(self):
self.assertTrue(self.system, 'System/OS name could not be determined')

def test_import_success(self):
self.assertIsNotNone(see.fcntl)
self.assertIsNotNone(see.termios)
if self.windows:
self.assertIsNone(see.fcntl)
self.assertIsNone(see.termios)
self.assertIsNotNone(see.windll)
self.assertIsNotNone(see.create_string_buffer)
else:
self.assertIsNotNone(see.fcntl)
self.assertIsNotNone(see.termios)
self.assertIsNone(see.windll)
self.assertIsNone(see.create_string_buffer)

def test_term_width(self):
width = see.term_width()

self.assertIsNotNone(width)
self.assertGreater(width, 0)

def test_ioctl_fail(self):
if self.windows:
return

with mock.patch('see.fcntl.ioctl', side_effect=IOError('')) as patch:
width = see.term_width()

Expand All @@ -75,7 +102,18 @@ def test_indent_to_prompt(self):
self.assertEqual(indent, len(sys.ps1))


class TestNonUnix(unittest.TestCase):
class TestMockWindowsTerminal(unittest.TestCase):

def setUp(self):
builtins.__import__ = mock_import
reload(see)

def tearDown(self):
builtins.__import__ = REAL_IMPORT
reload(see)


class TestMockUnsupportedTerminal(unittest.TestCase):

def setUp(self):
builtins.__import__ = mock_import
Expand Down

0 comments on commit cd47b05

Please sign in to comment.