From 99e4f623f76d479f0c89dd4a0b3f20555c97938b Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 13:48:02 +0200 Subject: [PATCH 01/13] Allow user to force terminal size used by the st2 CLI formattes. This comes handy in scenarios where terminal size can't be obtained (e.g. when running tests outside tty). --- st2client/st2client/utils/terminal.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index d53a342fc5..f9c622e85e 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -14,6 +14,7 @@ # limitations under the License. from __future__ import absolute_import + import os import struct import subprocess @@ -30,6 +31,16 @@ def get_terminal_size(default=(80, 20)): """ :return: (lines, cols) """ + # Allow user to force terminal size using a environment variables + # E.g. ST2_CLI_FORCE_TERMINAL_SIZE=80,200 # lines, columns + force_terminal_size = os.environ.get('ST2_CLI_FORCE_TERMINAL_SIZE', None) + if force_terminal_size: + split = force_terminal_size.split(',') + lines = split[0] + columns = split[1] if len(split) >= 2 else default[1] + + return lines, columns + def ioctl_GWINSZ(fd): import fcntl import termios From df5afde341c47c3c4bb2d4abf806a5341abea0ea Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 13:48:59 +0200 Subject: [PATCH 02/13] Also use a more reasonable default terminal size. --- st2client/st2client/utils/terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index f9c622e85e..1c1224867e 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -27,7 +27,7 @@ ] -def get_terminal_size(default=(80, 20)): +def get_terminal_size(default=(80, 200)): """ :return: (lines, cols) """ From c9ad392b0f93d214503af35b85283a14398a42f0 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 13:51:19 +0200 Subject: [PATCH 03/13] 200 -> 150.. --- st2client/st2client/utils/terminal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index 1c1224867e..84dff78e3f 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -27,7 +27,7 @@ ] -def get_terminal_size(default=(80, 200)): +def get_terminal_size(default=(80, 150)): """ :return: (lines, cols) """ From e59291dc097c2665c65e4510be42d8e52e167a33 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 13:57:33 +0200 Subject: [PATCH 04/13] Make sure we cast it to int. --- st2client/st2client/utils/terminal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index 84dff78e3f..cfc5ae3e55 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -36,8 +36,8 @@ def get_terminal_size(default=(80, 150)): force_terminal_size = os.environ.get('ST2_CLI_FORCE_TERMINAL_SIZE', None) if force_terminal_size: split = force_terminal_size.split(',') - lines = split[0] - columns = split[1] if len(split) >= 2 else default[1] + lines = int(split[0]) + columns = int(split[1] if len(split) >= 2 else default[1]) return lines, columns From 921fe11db839f7e6548e12068c50250aa88f25b6 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 14:01:40 +0200 Subject: [PATCH 05/13] Truncate extra whitespace. --- CHANGELOG.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 54ff8b3382..789f4d15dd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -11,16 +11,16 @@ Added The ``winrm-cmd`` runner executes Command Prompt commands remotely on Windows hosts using the WinRM protocol. The ``winrm-ps-cmd`` and ``winrm-ps-script`` runners execute PowerShell commands and scripts on remote Windows hosts using the WinRM protocol. - + To accompany these new runners, there are two new actions ``core.winrm_cmd`` that executes remote Command Prompt commands along with ``core.winrm_ps_cmd`` that executes remote PowerShell commands. (new feature) #1636 - + Contributed by Nick Maludy (Encore Technologies). * Add new ``?tags``, query param filter to the ``/v1/actions`` API endpoint. This query parameter allows users to filter out actions based on the tag name . By default, when no filter values are provided, all actions are returned. (new feature) #4219 - + Changed ~~~~~~~ @@ -30,10 +30,9 @@ Changed * Migrated runners to using the ``in-requirements.txt`` pattern for "components" in the build system, so the ``Makefile`` correctly generates and installs runner dependencies during testing and packaging. (improvement) (bugfix) #4169 - + Contributed by Nick Maludy (Encore Technologies). - Fixed ~~~~~ @@ -41,7 +40,7 @@ Fixed Reported by @jjm Contributed by Nick Maludy (Encore Technologies). - + 2.8.0 - July 10, 2018 --------------------- From 208b5938829c99b16bf43ee8fa54767c6397b297 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 14:05:09 +0200 Subject: [PATCH 06/13] Add changelog entry. --- CHANGELOG.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 789f4d15dd..333abd364b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,6 +20,9 @@ Added * Add new ``?tags``, query param filter to the ``/v1/actions`` API endpoint. This query parameter allows users to filter out actions based on the tag name . By default, when no filter values are provided, all actions are returned. (new feature) #4219 +* Allow user to force terminal size to use by ``st2`` CLI for table formatting purposes by setting + ``ST2_CLI_FORCE_TERMINAL_SIZE=`` environment variable. For example, + ``ST2_CLI_FORCE_TERMINAL_SIZE=80,200 st2 action list``. (improvement) #4242 Changed ~~~~~~~ @@ -32,6 +35,12 @@ Changed testing and packaging. (improvement) (bugfix) #4169 Contributed by Nick Maludy (Encore Technologies). +* Update ``st2`` CLI to use a more sensible default terminal size for table formatting purposes if + we are unable to retrieve a terminal size using various system-specific approaches. + + Previously we would fall back to a very unfriendly default of 20 columns for a total terminal + width which would cause every table column to wrap and make output impossible / hard to read. + (improvement) #4242 Fixed ~~~~~ From 56f9cec9b9cfc973d4e03c062b9c9905d6fd34db Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 14:05:58 +0200 Subject: [PATCH 07/13] Rewording. --- CHANGELOG.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 333abd364b..672d8e9e80 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -36,10 +36,10 @@ Changed Contributed by Nick Maludy (Encore Technologies). * Update ``st2`` CLI to use a more sensible default terminal size for table formatting purposes if - we are unable to retrieve a terminal size using various system-specific approaches. + we are unable to retrieve terminal size using various system-specific approaches. Previously we would fall back to a very unfriendly default of 20 columns for a total terminal - width which would cause every table column to wrap and make output impossible / hard to read. + width. This would cause every table column to wrap and make output impossible / hard to read. (improvement) #4242 Fixed From 471b5ef87f08978b46c96f99fd38dcc59e7145f4 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 15:38:22 +0200 Subject: [PATCH 08/13] Update get_terminal_size method to check LINES and COLUMNS environment variables first. This way it's more performant and consistent with upstream Python 3 implementation - https://github.com/python/cpython/blob/master/Lib/shutil.py#L1203. This way there is also no need for additional environment variables since those values are checked first. --- CHANGELOG.rst | 11 +++++-- st2client/st2client/utils/terminal.py | 41 +++++++++++++++++---------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 672d8e9e80..23e44d2d56 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,9 +20,14 @@ Added * Add new ``?tags``, query param filter to the ``/v1/actions`` API endpoint. This query parameter allows users to filter out actions based on the tag name . By default, when no filter values are provided, all actions are returned. (new feature) #4219 -* Allow user to force terminal size to use by ``st2`` CLI for table formatting purposes by setting - ``ST2_CLI_FORCE_TERMINAL_SIZE=`` environment variable. For example, - ``ST2_CLI_FORCE_TERMINAL_SIZE=80,200 st2 action list``. (improvement) #4242 +* Update ``st2`` CLI to it inspect ``LINES`` and ``COLUMNS`` environment variables first when + determining the terminal size. Previously those environment variables were checked second last + (after trying to retrieve terminal size using various OS specific methods and before falling back + to the default values). + + This approach is more performant and allows user to easily overwrite the default values or values + returned by the operating system checks - e.g. by running + ``LINES=80 COLUMNS=200 st2 action list``. (improvement) #4242 Changed ~~~~~~~ diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index cfc5ae3e55..817a9393ac 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -22,35 +22,49 @@ from st2client.utils.color import format_status +DEFAULT_TERMINAL_SIZE_LINES = 80 +DEFAULT_TERMINAL_SIZE_COLUMNS = 150 + __all__ = [ 'get_terminal_size' ] -def get_terminal_size(default=(80, 150)): +def get_terminal_size(default=(DEFAULT_TERMINAL_SIZE_LINES, DEFAULT_TERMINAL_SIZE_COLUMNS)): """ + Try to retrieve a default terminal size using various system specific approaches. + + If terminal size can't be retrieved, default value is returned. + + NOTE: LINES and COLUMNS environment variables are checked first, if those values are not set / + available, other methods are tried. + :return: (lines, cols) """ - # Allow user to force terminal size using a environment variables - # E.g. ST2_CLI_FORCE_TERMINAL_SIZE=80,200 # lines, columns - force_terminal_size = os.environ.get('ST2_CLI_FORCE_TERMINAL_SIZE', None) - if force_terminal_size: - split = force_terminal_size.split(',') - lines = int(split[0]) - columns = int(split[1] if len(split) >= 2 else default[1]) + # Try LINES and COLUMNS environment variables first like in upstream Python 3 method - + # https://github.com/python/cpython/blob/master/Lib/shutil.py#L1203 + # This way it's consistent with upstream implementation. In the past, our implementation + # checked those variables at the end as a fall back. + try: + lines = os.environ['LINES'] + columns = os.environ['COLUMNS'] - return lines, columns + return int(lines), int(columns) + except: + pass def ioctl_GWINSZ(fd): import fcntl import termios return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) + # try stdin, stdout, stderr for fd in (0, 1, 2): try: return ioctl_GWINSZ(fd) except: pass + # try os.ctermid() try: fd = os.open(os.ctermid(), os.O_RDONLY) @@ -60,6 +74,7 @@ def ioctl_GWINSZ(fd): os.close(fd) except: pass + # try `stty size` try: process = subprocess.Popen(['stty', 'size'], @@ -71,12 +86,8 @@ def ioctl_GWINSZ(fd): return tuple(int(x) for x in result[0].split()) except: pass - # try environment variables - try: - return tuple(int(os.getenv(var)) for var in ('LINES', 'COLUMNS')) - except: - pass - # return default. + + # return default value return default From a034c06b4244bb4dfcd4498b657bda511196c81e Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 17:01:31 +0200 Subject: [PATCH 09/13] Add a note. --- st2client/st2client/utils/terminal.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index 817a9393ac..9107f8534e 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -36,9 +36,11 @@ def get_terminal_size(default=(DEFAULT_TERMINAL_SIZE_LINES, DEFAULT_TERMINAL_SIZ If terminal size can't be retrieved, default value is returned. - NOTE: LINES and COLUMNS environment variables are checked first, if those values are not set / + NOTE 1: LINES and COLUMNS environment variables are checked first, if those values are not set / available, other methods are tried. + NOTE 2: This method requires both environment variables to be specified together. + :return: (lines, cols) """ # Try LINES and COLUMNS environment variables first like in upstream Python 3 method - From cf50187b5d9c203f109601d8052ddcbbeb784759 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 17:12:37 +0200 Subject: [PATCH 10/13] Add tests for get_terminal_size. --- st2client/tests/unit/test_util_terminal.py | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 st2client/tests/unit/test_util_terminal.py diff --git a/st2client/tests/unit/test_util_terminal.py b/st2client/tests/unit/test_util_terminal.py new file mode 100644 index 0000000000..95f3e243a5 --- /dev/null +++ b/st2client/tests/unit/test_util_terminal.py @@ -0,0 +1,62 @@ +# Licensed to the StackStorm, Inc ('StackStorm') under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import os + +import unittest2 +import mock + +from st2client.utils.terminal import DEFAULT_TERMINAL_SIZE_LINES +from st2client.utils.terminal import DEFAULT_TERMINAL_SIZE_COLUMNS +from st2client.utils.terminal import get_terminal_size + +__all__ = [ + 'TerminalUtilsTestCase' +] + + +class TerminalUtilsTestCase(unittest2.TestCase): + @mock.patch.dict(os.environ, {'LINES': '111', 'COLUMNS': '222'}) + def test_get_terminal_size_lines_columns_environment_variable_have_precedence(self): + # Verify that LINES and COLUMNS environment variables have precedence over other approaches + lines, columns = get_terminal_size() + + self.assertEqual(lines, 111) + self.assertEqual(columns, 222) + + @mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) + @mock.patch('subprocess.Popen') + def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): + mock_communicate = mock.Mock(return_value=['555 666']) + + mock_process = mock.Mock() + mock_process.returncode = 0 + mock_process.communicate = mock_communicate + + mock_popen.return_value = mock_process + + lines, columns = get_terminal_size() + self.assertEqual(lines, 555) + self.assertEqual(columns, 666) + + @mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) + @mock.patch('subprocess.Popen', mock.Mock(side_effect=Exception('b'))) + def test_get_terminal_size_default_values_are_used(self): + lines, columns = get_terminal_size() + + self.assertEqual(lines, DEFAULT_TERMINAL_SIZE_LINES) + self.assertEqual(columns, DEFAULT_TERMINAL_SIZE_COLUMNS) From 62c61ccba95bfa9e151ed9a2eda2d5d25e80d260 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 17:18:31 +0200 Subject: [PATCH 11/13] Number the various fallbacks. --- st2client/st2client/utils/terminal.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index 9107f8534e..3de14dc9b3 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -43,7 +43,7 @@ def get_terminal_size(default=(DEFAULT_TERMINAL_SIZE_LINES, DEFAULT_TERMINAL_SIZ :return: (lines, cols) """ - # Try LINES and COLUMNS environment variables first like in upstream Python 3 method - + # 1. Try LINES and COLUMNS environment variables first like in upstream Python 3 method - # https://github.com/python/cpython/blob/master/Lib/shutil.py#L1203 # This way it's consistent with upstream implementation. In the past, our implementation # checked those variables at the end as a fall back. @@ -60,14 +60,14 @@ def ioctl_GWINSZ(fd): import termios return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) - # try stdin, stdout, stderr + # 2. try stdin, stdout, stderr for fd in (0, 1, 2): try: return ioctl_GWINSZ(fd) except: pass - # try os.ctermid() + # 3. try os.ctermid() try: fd = os.open(os.ctermid(), os.O_RDONLY) try: @@ -77,7 +77,7 @@ def ioctl_GWINSZ(fd): except: pass - # try `stty size` + # 4. try `stty size` try: process = subprocess.Popen(['stty', 'size'], shell=False, @@ -89,7 +89,7 @@ def ioctl_GWINSZ(fd): except: pass - # return default value + # 5. return default value return default From 95308893da067eb00659f35cc522f7bfcbc60208 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 17:45:22 +0200 Subject: [PATCH 12/13] Replace get_terminal_size with get_terminal_size_columns. We only use columns value of terminal size so this allows us to simplify code for retrieving terminal size. --- CHANGELOG.rst | 16 ++++----- st2client/st2client/formatters/table.py | 4 +-- st2client/st2client/utils/terminal.py | 38 ++++++++++------------ st2client/tests/unit/test_util_terminal.py | 21 ++++++------ 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 23e44d2d56..89bc2178d4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -20,14 +20,14 @@ Added * Add new ``?tags``, query param filter to the ``/v1/actions`` API endpoint. This query parameter allows users to filter out actions based on the tag name . By default, when no filter values are provided, all actions are returned. (new feature) #4219 -* Update ``st2`` CLI to it inspect ``LINES`` and ``COLUMNS`` environment variables first when - determining the terminal size. Previously those environment variables were checked second last - (after trying to retrieve terminal size using various OS specific methods and before falling back - to the default values). - - This approach is more performant and allows user to easily overwrite the default values or values - returned by the operating system checks - e.g. by running - ``LINES=80 COLUMNS=200 st2 action list``. (improvement) #4242 +* Update ``st2`` CLI to inspect ``COLUMNS`` environment variable first when determining the + terminal size. Previously this environment variable was checked second last (after trying to + retrieve terminal size using various OS specific methods and before falling back to the default + value). + + This approach is more performant and allows user to easily overwrite the default value or value + returned by the operating system checks - e.g. by running ``COLUMNS=200 st2 action list``. + (improvement) #4242 Changed ~~~~~~~ diff --git a/st2client/st2client/formatters/table.py b/st2client/st2client/formatters/table.py index ff783349fc..c29c583426 100644 --- a/st2client/st2client/formatters/table.py +++ b/st2client/st2client/formatters/table.py @@ -27,7 +27,7 @@ from st2client import formatters from st2client.utils import strutil -from st2client.utils.terminal import get_terminal_size +from st2client.utils.terminal import get_terminal_size_columns LOG = logging.getLogger(__name__) @@ -65,7 +65,7 @@ def format(cls, entries, *args, **kwargs): if not widths and attributes: # Dynamically calculate column size based on the terminal size - lines, cols = get_terminal_size() + cols = get_terminal_size_columns() if attributes[0] == 'id': # consume iterator and save as entries so collection is accessible later. diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index 3de14dc9b3..bd463fcb9f 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -26,55 +26,53 @@ DEFAULT_TERMINAL_SIZE_COLUMNS = 150 __all__ = [ - 'get_terminal_size' + 'get_terminal_size_columns' ] -def get_terminal_size(default=(DEFAULT_TERMINAL_SIZE_LINES, DEFAULT_TERMINAL_SIZE_COLUMNS)): +def get_terminal_size_columns(default=DEFAULT_TERMINAL_SIZE_COLUMNS): """ - Try to retrieve a default terminal size using various system specific approaches. + Try to retrieve COLUMNS value of terminal size using various system specific approaches. If terminal size can't be retrieved, default value is returned. - NOTE 1: LINES and COLUMNS environment variables are checked first, if those values are not set / - available, other methods are tried. + NOTE 1: COLUMNS environment variable is checked first, if the value is not set / available, + other methods are tried. - NOTE 2: This method requires both environment variables to be specified together. - - :return: (lines, cols) + :rtype: ``int`` + :return: columns """ - # 1. Try LINES and COLUMNS environment variables first like in upstream Python 3 method - + # 1. Try COLUMNS environment variable first like in upstream Python 3 method - # https://github.com/python/cpython/blob/master/Lib/shutil.py#L1203 # This way it's consistent with upstream implementation. In the past, our implementation # checked those variables at the end as a fall back. try: - lines = os.environ['LINES'] columns = os.environ['COLUMNS'] - - return int(lines), int(columns) - except: + return int(columns) + except (KeyError, ValueError): pass def ioctl_GWINSZ(fd): import fcntl import termios + # Return a tuple (lines, columns) return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) # 2. try stdin, stdout, stderr for fd in (0, 1, 2): try: - return ioctl_GWINSZ(fd) - except: + return ioctl_GWINSZ(fd)[1] + except Exception: pass # 3. try os.ctermid() try: fd = os.open(os.ctermid(), os.O_RDONLY) try: - return ioctl_GWINSZ(fd) + return ioctl_GWINSZ(fd)[1] finally: os.close(fd) - except: + except Exception: pass # 4. try `stty size` @@ -85,11 +83,11 @@ def ioctl_GWINSZ(fd): stderr=open(os.devnull, 'w')) result = process.communicate() if process.returncode == 0: - return tuple(int(x) for x in result[0].split()) - except: + return tuple(int(x) for x in result[0].split())[1] + except Exception: pass - # 5. return default value + # 5. return default fallback value return default diff --git a/st2client/tests/unit/test_util_terminal.py b/st2client/tests/unit/test_util_terminal.py index 95f3e243a5..b7635f2494 100644 --- a/st2client/tests/unit/test_util_terminal.py +++ b/st2client/tests/unit/test_util_terminal.py @@ -20,9 +20,8 @@ import unittest2 import mock -from st2client.utils.terminal import DEFAULT_TERMINAL_SIZE_LINES from st2client.utils.terminal import DEFAULT_TERMINAL_SIZE_COLUMNS -from st2client.utils.terminal import get_terminal_size +from st2client.utils.terminal import get_terminal_size_columns __all__ = [ 'TerminalUtilsTestCase' @@ -31,13 +30,17 @@ class TerminalUtilsTestCase(unittest2.TestCase): @mock.patch.dict(os.environ, {'LINES': '111', 'COLUMNS': '222'}) - def test_get_terminal_size_lines_columns_environment_variable_have_precedence(self): - # Verify that LINES and COLUMNS environment variables have precedence over other approaches - lines, columns = get_terminal_size() + def test_get_terminal_size_columns_columns_environment_variable_has_precedence(self): + # Verify that COLUMNS environment variables has precedence over other approaches + columns = get_terminal_size_columns() - self.assertEqual(lines, 111) self.assertEqual(columns, 222) + @mock.patch('struct.unpack', mock.Mock(return_value=(333, 444))) + def test_get_terminal_size_columns_stdout_is_used(self): + columns = get_terminal_size_columns() + self.assertEqual(columns, 444) + @mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) @mock.patch('subprocess.Popen') def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): @@ -49,14 +52,12 @@ def test_get_terminal_size_subprocess_popen_is_used(self, mock_popen): mock_popen.return_value = mock_process - lines, columns = get_terminal_size() - self.assertEqual(lines, 555) + columns = get_terminal_size_columns() self.assertEqual(columns, 666) @mock.patch('struct.unpack', mock.Mock(side_effect=Exception('a'))) @mock.patch('subprocess.Popen', mock.Mock(side_effect=Exception('b'))) def test_get_terminal_size_default_values_are_used(self): - lines, columns = get_terminal_size() + columns = get_terminal_size_columns() - self.assertEqual(lines, DEFAULT_TERMINAL_SIZE_LINES) self.assertEqual(columns, DEFAULT_TERMINAL_SIZE_COLUMNS) From e2d2c8ad35b101c574646fa692d4a1598a960557 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Fri, 13 Jul 2018 17:56:24 +0200 Subject: [PATCH 13/13] Remove unused variable. --- st2client/st2client/utils/terminal.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/st2client/st2client/utils/terminal.py b/st2client/st2client/utils/terminal.py index bd463fcb9f..b7ca3da4cd 100644 --- a/st2client/st2client/utils/terminal.py +++ b/st2client/st2client/utils/terminal.py @@ -22,10 +22,11 @@ from st2client.utils.color import format_status -DEFAULT_TERMINAL_SIZE_LINES = 80 DEFAULT_TERMINAL_SIZE_COLUMNS = 150 __all__ = [ + 'DEFAULT_TERMINAL_SIZE_COLUMNS', + 'get_terminal_size_columns' ]