Skip to content

Commit f6c8749

Browse files
authored
Merge pull request #238 from boegel/fix_set_columns
also take into account possible IndexError in get_columns
2 parents 282758f + 2d71dd7 commit f6c8749

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

lib/vsc/utils/generaloption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def set_columns(cols=None):
6767
if os.path.exists(stty):
6868
try:
6969
cols = int(os.popen('%s size 2>/dev/null' % stty).read().strip().split(' ')[1])
70-
except (ValueError, OSError, AttributeError):
70+
except (AttributeError, IndexError, OSError, ValueError):
7171
# do nothing
7272
pass
7373

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
]
4545

4646
PACKAGE = {
47-
'version': '2.5.3',
47+
'version': '2.5.4',
4848
'author': [sdw, jt, ag, kh],
4949
'maintainer': [sdw, jt, ag, kh],
5050
# as long as 1.0.0 is not out, vsc-base should still provide vsc.fancylogger

test/generaloption.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
@author: Stijn De Weirdt (Ghent University)
3030
"""
31+
import copy
3132
import datetime
3233
import logging
3334
import os
@@ -37,7 +38,7 @@
3738
from tempfile import NamedTemporaryFile
3839

3940
from vsc.utils import fancylogger
40-
from vsc.utils.generaloption import GeneralOption, HELP_OUTPUT_FORMATS
41+
from vsc.utils.generaloption import GeneralOption, HELP_OUTPUT_FORMATS, set_columns
4142
from vsc.utils.missing import shell_quote, shell_unquote
4243
from vsc.utils.optcomplete import gen_cmdline
4344
from vsc.utils.run import run_simple
@@ -118,8 +119,14 @@ class GeneralOptionTest(TestCase):
118119
"""Tests for general option"""
119120

120121
def setUp(self):
122+
"""Prepare for running test."""
121123
super(GeneralOptionTest, self).setUp()
122124
self.setup = vsc_setup()
125+
self.orig_environ = copy.deepcopy(os.environ)
126+
127+
def tearDown(self):
128+
"""Clean up after running test."""
129+
os.environ = self.orig_environ
123130

124131
def test_help_short(self):
125132
"""Generate short help message"""
@@ -908,3 +915,26 @@ def test_option_as_value(self):
908915
# but first error still wins
909916
self._match_testoption1_sysexit(['--store', '--foo', '--nosuchoptiondefinedfoobar'],
910917
"Value '--foo' starts with a '-'")
918+
919+
def test_set_columns(self):
920+
"""Test set_columns function."""
921+
def reset_columns():
922+
"""Reset environment to run another test case for set_columns."""
923+
if 'COLUMNS' in os.environ:
924+
del os.environ['COLUMNS']
925+
926+
reset_columns()
927+
set_columns()
928+
cols = os.environ.get('COLUMNS')
929+
self.assertTrue(cols is None or isinstance(cols, basestring))
930+
931+
set_columns(cols=10)
932+
self.assertEqual(os.environ['COLUMNS'], '10')
933+
934+
# $COLUMNS wins
935+
set_columns(cols=99)
936+
self.assertEqual(os.environ['COLUMNS'], '10')
937+
938+
del os.environ['COLUMNS']
939+
set_columns(cols=99)
940+
self.assertEqual(os.environ['COLUMNS'], '99')

0 commit comments

Comments
 (0)