From 7a90d7fca89f126928bca4912709219d74e8fd82 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 18 Feb 2015 13:47:15 +0100 Subject: [PATCH] Fix standarddir tests when XDG_*_HOME is set. --- qutebrowser/test/helpers.py | 13 +++++++++++-- qutebrowser/test/test_helpers.py | 13 +++++++++++++ qutebrowser/test/utils/test_standarddir.py | 9 ++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/qutebrowser/test/helpers.py b/qutebrowser/test/helpers.py index fbb6c0ee7..d3ae76686 100644 --- a/qutebrowser/test/helpers.py +++ b/qutebrowser/test/helpers.py @@ -33,11 +33,20 @@ def environ_set_temp(name, value): oldval = os.environ[name] except KeyError: oldval = None - os.environ[name] = value + + if value is not None: + os.environ[name] = value + else: + try: + del os.environ[name] + except KeyError: + pass + yield + if oldval is not None: os.environ[name] = oldval - else: + elif value is not None: del os.environ[name] diff --git a/qutebrowser/test/test_helpers.py b/qutebrowser/test/test_helpers.py index 9174d8d9f..2ad23dcbd 100644 --- a/qutebrowser/test/test_helpers.py +++ b/qutebrowser/test/test_helpers.py @@ -43,6 +43,19 @@ class TestEnvironSetTemp(unittest.TestCase): self.assertEqual(os.environ['QUTEBROWSER_ENVIRON_TEST'], 'newval') self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) + 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): + 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): + self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) + self.assertNotIn('QUTEBROWSER_ENVIRON_TEST', os.environ) + def tearDown(self): if 'QUTEBROWSER_ENVIRON_TEST' in os.environ: # if some test failed diff --git a/qutebrowser/test/utils/test_standarddir.py b/qutebrowser/test/utils/test_standarddir.py index 845a610d1..a676b9be2 100644 --- a/qutebrowser/test/utils/test_standarddir.py +++ b/qutebrowser/test/utils/test_standarddir.py @@ -73,7 +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): + with helpers.environ_set_temp('HOME', self.temp_dir), \ + helpers.environ_set_temp('XDG_DATA_HOME', None): cur_dir = standarddir.get(QStandardPaths.DataLocation) self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.local', 'share', 'qutebrowser')) @@ -81,7 +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): + with helpers.environ_set_temp('HOME', self.temp_dir), \ + helpers.environ_set_temp('XDG_CONFIG_HOME', None): cur_dir = standarddir.get( QStandardPaths.ConfigLocation) self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.config', @@ -90,7 +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): + with helpers.environ_set_temp('HOME', self.temp_dir), \ + helpers.environ_set_temp('XDG_CACHE_HOME', None): cur_dir = standarddir.get(QStandardPaths.CacheLocation) self.assertEqual(cur_dir, os.path.join(self.temp_dir, '.cache', 'qutebrowser'))