From 60d4305cc4a80285810d469d03fb5cdee0ad6ed7 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 08:34:24 +0100 Subject: [PATCH] tests: Adjust environ_set_temp to take a dict. --- qutebrowser/test/config/test_configtypes.py | 4 +-- qutebrowser/test/helpers.py | 33 ++++++++++++--------- qutebrowser/test/test_helpers.py | 27 ++++++++++++++--- qutebrowser/test/utils/test_standarddir.py | 18 +++++------ 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index f07035cb1..dc656a718 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -1391,7 +1391,7 @@ class DirectoryTests(unittest.TestCase): os_path.expanduser.side_effect = lambda x: x os_path.isdir.side_effect = lambda path: path == '/home/foo/bar/foobar' 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') os_path.expandvars.assert_called_once_with('$BAR/foobar') @@ -1795,7 +1795,7 @@ class UserStyleSheetTests(unittest.TestCase): def test_transform_file_expandvars(self): """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') self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar")) diff --git a/qutebrowser/test/helpers.py b/qutebrowser/test/helpers.py index d3ae76686..b5c6b0dd7 100644 --- a/qutebrowser/test/helpers.py +++ b/qutebrowser/test/helpers.py @@ -27,27 +27,32 @@ from PyQt5.QtGui import QKeyEvent @contextlib.contextmanager -def environ_set_temp(name, value): - """Set a temporary environment variable.""" - try: - oldval = os.environ[name] - except KeyError: - oldval = None +def environ_set_temp(env): + """Set temporary environment variables. - if value is not None: - os.environ[name] = value - else: + Args: + env: A dictionary with name: value pairs. + If value is None, the variable is temporarily deleted. + """ + old_env = {} + + for name, value in env.items(): try: - del os.environ[name] + old_env[name] = os.environ[name] except KeyError: pass + if value is None: + os.environ.pop(name, None) + else: + os.environ[name] = value yield - if oldval is not None: - os.environ[name] = oldval - elif value is not None: - del os.environ[name] + for name, value in env.items(): + if name in old_env: + os.environ[name] = old_env[name] + elif value is not None: + del os.environ[name] def fake_keyevent(key, modifiers=0, text=''): diff --git a/qutebrowser/test/test_helpers.py b/qutebrowser/test/test_helpers.py index 2ad23dcbd..13e224374 100644 --- a/qutebrowser/test/test_helpers.py +++ b/qutebrowser/test/test_helpers.py @@ -33,26 +33,45 @@ class TestEnvironSetTemp(unittest.TestCase): def test_environ_set(self): """Test environ_set_temp with something which was set already.""" 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'], 'oldval') def test_environ_unset(self): """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.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): """Test environ_set_temp with something which was set already.""" 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.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'oldval') def test_environ_none_unset(self): """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) diff --git a/qutebrowser/test/utils/test_standarddir.py b/qutebrowser/test/utils/test_standarddir.py index a676b9be2..5e717c672 100644 --- a/qutebrowser/test/utils/test_standarddir.py +++ b/qutebrowser/test/utils/test_standarddir.py @@ -49,7 +49,7 @@ class GetStandardDirLinuxTests(unittest.TestCase): @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_data_explicit(self): """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) self.assertEqual(cur_dir, os.path.join(self.temp_dir, 'qutebrowser')) @@ -57,7 +57,7 @@ class GetStandardDirLinuxTests(unittest.TestCase): @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_config_explicit(self): """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) self.assertEqual(cur_dir, os.path.join(self.temp_dir, 'qutebrowser')) @@ -65,7 +65,7 @@ class GetStandardDirLinuxTests(unittest.TestCase): @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_cache_explicit(self): """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) self.assertEqual(cur_dir, os.path.join(self.temp_dir, 'qutebrowser')) @@ -73,8 +73,8 @@ class GetStandardDirLinuxTests(unittest.TestCase): @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_data(self): """Test data dir with XDG_DATA_HOME not set.""" - with helpers.environ_set_temp('HOME', self.temp_dir), \ - helpers.environ_set_temp('XDG_DATA_HOME', None): + env = {'HOME': self.temp_dir, 'XDG_DATA_HOME': None} + with helpers.environ_set_temp(env): cur_dir = standarddir.get(QStandardPaths.DataLocation) self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local', 'share', 'qutebrowser')) @@ -82,8 +82,8 @@ class GetStandardDirLinuxTests(unittest.TestCase): @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_config(self): """Test config dir with XDG_CONFIG_HOME not set.""" - with helpers.environ_set_temp('HOME', self.temp_dir), \ - helpers.environ_set_temp('XDG_CONFIG_HOME', None): + env = {'HOME': self.temp_dir, 'XDG_CONFIG_HOME': None} + with helpers.environ_set_temp(env): cur_dir = standarddir.get( QStandardPaths.ConfigLocation) 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") def test_cache(self): """Test cache dir with XDG_CACHE_HOME not set.""" - with helpers.environ_set_temp('HOME', self.temp_dir), \ - helpers.environ_set_temp('XDG_CACHE_HOME', None): + env = {'HOME': self.temp_dir, 'XDG_CACHE_HOME': None} + with helpers.environ_set_temp(env): cur_dir = standarddir.get(QStandardPaths.CacheLocation) self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache', 'qutebrowser'))