Skip to content

Commit b13c84b

Browse files
author
Alistair Coles
committed
Fix unit tests failing when OS_ env vars are set
tests/unit/test_shell.py:TestParsing tests can fail if there are OS_* variables set in the environment. There is already code in the test setUp to remove ST_* variables, so we should do the same for OS_* variables. This patch also changes the mechanism used to remove and then restore any unwanted variables found in os.environ. The existing setUp() takes a copy of os.environ and then deletes any ST_* variables in the original. In tearDown() it sets os.environ as the copy. However, the environ imported into shell.py remains pointing to the original os.environ object. So after the first call to tearDown, subsequent mocking of os.environ has no effect on shell.environ. This renders some of the tests ineffective e.g. test_insufficient_env_vars_v3 is not actually setting any vars in shell.environ. The issue can be provoked by repeating a test: nosetests -w tests/unit/ test_shell.py:TestParsing.test_args_v3 \ test_shell.py:TestParsing.test_args_v3 The test will pass first time and fail second time. Change-Id: I5d100f81115e74878d510326acb5777e6a3626c8
1 parent 8f1b394 commit b13c84b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

tests/unit/test_shell.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,15 @@ class TestParsing(unittest.TestCase):
399399

400400
def setUp(self):
401401
super(TestParsing, self).setUp()
402-
self._orig_environ = os.environ.copy()
402+
self._environ_vars = {}
403403
keys = os.environ.keys()
404404
for k in keys:
405-
if k in ('ST_KEY', 'ST_USER', 'ST_AUTH'):
406-
del os.environ[k]
405+
if (k in ('ST_KEY', 'ST_USER', 'ST_AUTH')
406+
or k.startswith('OS_')):
407+
self._environ_vars[k] = os.environ.pop(k)
407408

408409
def tearDown(self):
409-
os.environ = self._orig_environ
410+
os.environ.update(self._environ_vars)
410411

411412
def _make_fake_command(self, result):
412413
def fake_command(parser, args, thread_manager):

0 commit comments

Comments
 (0)