tests: Adjust environ_set_temp to take a dict.

This commit is contained in:
Florian Bruhin 2015-02-20 08:34:24 +01:00
parent 14f2420500
commit 60d4305cc4
4 changed files with 53 additions and 29 deletions

View File

@ -1391,7 +1391,7 @@ class DirectoryTests(unittest.TestCase):
os_path.expanduser.side_effect = lambda x: x os_path.expanduser.side_effect = lambda x: x
os_path.isdir.side_effect = lambda path: path == '/home/foo/bar/foobar' os_path.isdir.side_effect = lambda path: path == '/home/foo/bar/foobar'
os_path.isabs.return_value = True os_path.isabs.return_value = True
with helpers.environ_set_temp('bar', '/home/foo/bar'): with helpers.environ_set_temp({'BAR': '/home/foo/bar'}):
self.t.validate('$BAR/foobar') self.t.validate('$BAR/foobar')
os_path.expandvars.assert_called_once_with('$BAR/foobar') os_path.expandvars.assert_called_once_with('$BAR/foobar')
@ -1795,7 +1795,7 @@ class UserStyleSheetTests(unittest.TestCase):
def test_transform_file_expandvars(self): def test_transform_file_expandvars(self):
"""Test transform with a filename (expandvars).""" """Test transform with a filename (expandvars)."""
with helpers.environ_set_temp('FOO', 'foo'): with helpers.environ_set_temp({'FOO': 'foo'}):
path = os.path.join(os.path.sep, '$FOO', 'bar') path = os.path.join(os.path.sep, '$FOO', 'bar')
self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar")) self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar"))

View File

@ -27,27 +27,32 @@ from PyQt5.QtGui import QKeyEvent
@contextlib.contextmanager @contextlib.contextmanager
def environ_set_temp(name, value): def environ_set_temp(env):
"""Set a temporary environment variable.""" """Set temporary environment variables.
try:
oldval = os.environ[name]
except KeyError:
oldval = None
if value is not None: Args:
os.environ[name] = value env: A dictionary with name: value pairs.
else: If value is None, the variable is temporarily deleted.
"""
old_env = {}
for name, value in env.items():
try: try:
del os.environ[name] old_env[name] = os.environ[name]
except KeyError: except KeyError:
pass pass
if value is None:
os.environ.pop(name, None)
else:
os.environ[name] = value
yield yield
if oldval is not None: for name, value in env.items():
os.environ[name] = oldval if name in old_env:
elif value is not None: os.environ[name] = old_env[name]
del os.environ[name] elif value is not None:
del os.environ[name]
def fake_keyevent(key, modifiers=0, text=''): def fake_keyevent(key, modifiers=0, text=''):

View File

@ -33,26 +33,45 @@ class TestEnvironSetTemp(unittest.TestCase):
def test_environ_set(self): def test_environ_set(self):
"""Test environ_set_temp with something which was set already.""" """Test environ_set_temp with something which was set already."""
os.environ['QUTEBROWSER_ENVIRON_TEST'] = 'oldval' os.environ['QUTEBROWSER_ENVIRON_TEST'] = 'oldval'
with helpers.environ_set_temp('QUTEBROWSER_ENVIRON_TEST', 'newval'): with helpers.environ_set_temp({'QUTEBROWSER_ENVIRON_TEST': 'newval'}):
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval') self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval')
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval') self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval')
def test_environ_unset(self): def test_environ_unset(self):
"""Test environ_set_temp with something which wasn't set yet.""" """Test environ_set_temp with something which wasn't set yet."""
with helpers.environ_set_temp('QUTEBROWSER_ENVIRON_TEST', 'newval'): with helpers.environ_set_temp({'QUTEBROWSER_ENVIRON_TEST': 'newval'}):
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval') self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval')
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ)
def test_environ_multiple(self):
"""Test environ_set_temp with multiple values."""
os.environ['QUTEBROWSER_ENVIRON_TEST_1'] = 'oldval_1'
os.environ['QUTEBROWSER_ENVIRON_TEST_3'] = 'oldval_3'
env = {
'QUTEBROWSER_ENVIRON_TEST_1': 'newval_1',
'QUTEBROWSER_ENVIRON_TEST_2': 'newval_2',
'QUTEBROWSER_ENVIRON_TEST_3': None,
}
with helpers.environ_set_temp(env):
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST_1'],
'newval_1')
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST_2'],
'newval_2')
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST_3', os.environ)
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST_1'], 'oldval_1')
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST_2', os.environ)
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST_3'], 'oldval_3')
def test_environ_none_set(self): def test_environ_none_set(self):
"""Test environ_set_temp with something which was set already.""" """Test environ_set_temp with something which was set already."""
os.environ['QUTEBROWSER_ENVIRON_TEST'] = 'oldval' os.environ['QUTEBROWSER_ENVIRON_TEST'] = 'oldval'
with helpers.environ_set_temp('QUTEBROWSER_ENVIRON_TEST', None): with helpers.environ_set_temp({'QUTEBROWSER_ENVIRON_TEST': None}):
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ)
self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval') self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval')
def test_environ_none_unset(self): def test_environ_none_unset(self):
"""Test environ_set_temp with something which wasn't set yet.""" """Test environ_set_temp with something which wasn't set yet."""
with helpers.environ_set_temp('QUTEBROWSER_ENVIRON_TEST', None): with helpers.environ_set_temp({'QUTEBROWSER_ENVIRON_TEST': None}):
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ)
self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ)

View File

@ -49,7 +49,7 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_data_explicit(self): def test_data_explicit(self):
"""Test data dir with XDG_DATA_HOME explicitely set.""" """Test data dir with XDG_DATA_HOME explicitely set."""
with helpers.environ_set_temp('XDG_DATA_HOME', self.temp_dir): with helpers.environ_set_temp({'XDG_DATA_HOME': self.temp_dir}):
cur_dir = standarddir.get(QStandardPaths.DataLocation) cur_dir = standarddir.get(QStandardPaths.DataLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, self.assertEqual(cur_dir, os.path.join(self.temp_dir,
'qutebrowser')) 'qutebrowser'))
@ -57,7 +57,7 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_config_explicit(self): def test_config_explicit(self):
"""Test config dir with XDG_CONFIG_HOME explicitely set.""" """Test config dir with XDG_CONFIG_HOME explicitely set."""
with helpers.environ_set_temp('XDG_CONFIG_HOME', self.temp_dir): with helpers.environ_set_temp({'XDG_CONFIG_HOME': self.temp_dir}):
cur_dir = standarddir.get(QStandardPaths.ConfigLocation) cur_dir = standarddir.get(QStandardPaths.ConfigLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, self.assertEqual(cur_dir, os.path.join(self.temp_dir,
'qutebrowser')) 'qutebrowser'))
@ -65,7 +65,7 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_cache_explicit(self): def test_cache_explicit(self):
"""Test cache dir with XDG_CACHE_HOME explicitely set.""" """Test cache dir with XDG_CACHE_HOME explicitely set."""
with helpers.environ_set_temp('XDG_CACHE_HOME', self.temp_dir): with helpers.environ_set_temp({'XDG_CACHE_HOME': self.temp_dir}):
cur_dir = standarddir.get(QStandardPaths.CacheLocation) cur_dir = standarddir.get(QStandardPaths.CacheLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, self.assertEqual(cur_dir, os.path.join(self.temp_dir,
'qutebrowser')) 'qutebrowser'))
@ -73,8 +73,8 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_data(self): def test_data(self):
"""Test data dir with XDG_DATA_HOME not set.""" """Test data dir with XDG_DATA_HOME not set."""
with helpers.environ_set_temp('HOME', self.temp_dir), \ env = {'HOME': self.temp_dir, 'XDG_DATA_HOME': None}
helpers.environ_set_temp('XDG_DATA_HOME', None): with helpers.environ_set_temp(env):
cur_dir = standarddir.get(QStandardPaths.DataLocation) cur_dir = standarddir.get(QStandardPaths.DataLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local', self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local',
'share', 'qutebrowser')) 'share', 'qutebrowser'))
@ -82,8 +82,8 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_config(self): def test_config(self):
"""Test config dir with XDG_CONFIG_HOME not set.""" """Test config dir with XDG_CONFIG_HOME not set."""
with helpers.environ_set_temp('HOME', self.temp_dir), \ env = {'HOME': self.temp_dir, 'XDG_CONFIG_HOME': None}
helpers.environ_set_temp('XDG_CONFIG_HOME', None): with helpers.environ_set_temp(env):
cur_dir = standarddir.get( cur_dir = standarddir.get(
QStandardPaths.ConfigLocation) QStandardPaths.ConfigLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.config', self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.config',
@ -92,8 +92,8 @@ class GetStandardDirLinuxTests(unittest.TestCase):
@unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux")
def test_cache(self): def test_cache(self):
"""Test cache dir with XDG_CACHE_HOME not set.""" """Test cache dir with XDG_CACHE_HOME not set."""
with helpers.environ_set_temp('HOME', self.temp_dir), \ env = {'HOME': self.temp_dir, 'XDG_CACHE_HOME': None}
helpers.environ_set_temp('XDG_CACHE_HOME', None): with helpers.environ_set_temp(env):
cur_dir = standarddir.get(QStandardPaths.CacheLocation) cur_dir = standarddir.get(QStandardPaths.CacheLocation)
self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache', self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache',
'qutebrowser')) 'qutebrowser'))