Skip to content

Commit

Permalink
Merge pull request #238 from boegel/fix_set_columns
Browse files Browse the repository at this point in the history
also take into account possible IndexError in get_columns
  • Loading branch information
wdpypere authored Sep 5, 2016
2 parents 282758f + 2d71dd7 commit f6c8749
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/vsc/utils/generaloption.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def set_columns(cols=None):
if os.path.exists(stty):
try:
cols = int(os.popen('%s size 2>/dev/null' % stty).read().strip().split(' ')[1])
except (ValueError, OSError, AttributeError):
except (AttributeError, IndexError, OSError, ValueError):
# do nothing
pass

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
]

PACKAGE = {
'version': '2.5.3',
'version': '2.5.4',
'author': [sdw, jt, ag, kh],
'maintainer': [sdw, jt, ag, kh],
# as long as 1.0.0 is not out, vsc-base should still provide vsc.fancylogger
Expand Down
32 changes: 31 additions & 1 deletion test/generaloption.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@author: Stijn De Weirdt (Ghent University)
"""
import copy
import datetime
import logging
import os
Expand All @@ -37,7 +38,7 @@
from tempfile import NamedTemporaryFile

from vsc.utils import fancylogger
from vsc.utils.generaloption import GeneralOption, HELP_OUTPUT_FORMATS
from vsc.utils.generaloption import GeneralOption, HELP_OUTPUT_FORMATS, set_columns
from vsc.utils.missing import shell_quote, shell_unquote
from vsc.utils.optcomplete import gen_cmdline
from vsc.utils.run import run_simple
Expand Down Expand Up @@ -118,8 +119,14 @@ class GeneralOptionTest(TestCase):
"""Tests for general option"""

def setUp(self):
"""Prepare for running test."""
super(GeneralOptionTest, self).setUp()
self.setup = vsc_setup()
self.orig_environ = copy.deepcopy(os.environ)

def tearDown(self):
"""Clean up after running test."""
os.environ = self.orig_environ

def test_help_short(self):
"""Generate short help message"""
Expand Down Expand Up @@ -908,3 +915,26 @@ def test_option_as_value(self):
# but first error still wins
self._match_testoption1_sysexit(['--store', '--foo', '--nosuchoptiondefinedfoobar'],
"Value '--foo' starts with a '-'")

def test_set_columns(self):
"""Test set_columns function."""
def reset_columns():
"""Reset environment to run another test case for set_columns."""
if 'COLUMNS' in os.environ:
del os.environ['COLUMNS']

reset_columns()
set_columns()
cols = os.environ.get('COLUMNS')
self.assertTrue(cols is None or isinstance(cols, basestring))

set_columns(cols=10)
self.assertEqual(os.environ['COLUMNS'], '10')

# $COLUMNS wins
set_columns(cols=99)
self.assertEqual(os.environ['COLUMNS'], '10')

del os.environ['COLUMNS']
set_columns(cols=99)
self.assertEqual(os.environ['COLUMNS'], '99')

0 comments on commit f6c8749

Please sign in to comment.