Skip to content

Commit 2e29441

Browse files
committed
Vendorize EnvironmentVarGuard, attempt to fix #121
Anaconda 2.x doesn't bundle test module, vendorize the decorator used from it. Slightly modified to change ``iteritems()`` to ``items()`` for python 3 compatibility.
1 parent afc31f7 commit 2e29441

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

tmuxp/_compat.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
from string import lower as ascii_lowercase
3434
import urlparse
3535

36-
from test import test_support as support
37-
3836
exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
3937

4038
def implements_to_string(cls):
@@ -73,8 +71,6 @@ def console_to_str(s):
7371
import urllib.parse as urlparse
7472
from urllib.request import urlretrieve
7573

76-
from test import support
77-
7874
console_encoding = sys.__stdout__.encoding
7975

8076
implements_to_string = _identity

tmuxp/testsuite/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import kaptan
1919

2020
from .helpers import TestCase
21+
from .util import EnvironmentVarGuard
2122
from .. import config, exc
22-
from .._compat import support
23-
2423

2524

2625
logger = logging.getLogger(__name__)
@@ -1090,7 +1089,7 @@ def test_replaces_start_directory(self):
10901089
sconfig = kaptan.Kaptan(handler='yaml')
10911090
sconfig = sconfig.import_config(yaml_config).get()
10921091

1093-
with support.EnvironmentVarGuard() as env:
1092+
with EnvironmentVarGuard() as env:
10941093
env.set(env_key, env_value)
10951094
sconfig = config.expand(sconfig)
10961095
self.assertEqual("%s/test" % env_value, sconfig['start_directory'])

tmuxp/testsuite/util.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,40 @@
2424
fixtures_dir = os.path.realpath(os.path.join(current_dir, 'fixtures'))
2525

2626

27+
class EnvironmentVarGuard(object):
28+
29+
"""Class to help protect the environment variable properly. Can be used as
30+
a context manager.
31+
Vendorize to fix issue with Anaconda Python 2 not
32+
including test module, see #121.
33+
"""
34+
35+
def __init__(self):
36+
self._environ = os.environ
37+
self._unset = set()
38+
self._reset = dict()
39+
40+
def set(self, envvar, value):
41+
if envvar not in self._environ:
42+
self._unset.add(envvar)
43+
else:
44+
self._reset[envvar] = self._environ[envvar]
45+
self._environ[envvar] = value
46+
47+
def unset(self, envvar):
48+
if envvar in self._environ:
49+
self._reset[envvar] = self._environ[envvar]
50+
del self._environ[envvar]
51+
52+
def __enter__(self):
53+
return self
54+
55+
def __exit__(self, *ignore_exc):
56+
for envvar, value in self._reset.items():
57+
self._environ[envvar] = value
58+
for unset in self._unset:
59+
del self._environ[unset]
60+
2761
class TmuxVersionTest(TmuxTestCase):
2862

2963
"""Test the :meth:`has_required_tmux_version`."""

0 commit comments

Comments
 (0)