Fix standarddir tests when XDG_*_HOME is set.

This commit is contained in:
Florian Bruhin 2015-02-18 13:47:15 +01:00
parent 83b636a0a7
commit 7a90d7fca8
3 changed files with 30 additions and 5 deletions

View File

@ -33,11 +33,20 @@ def environ_set_temp(name, value):
oldval = os.environ[name] oldval = os.environ[name]
except KeyError: except KeyError:
oldval = None 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 yield
if oldval is not None: if oldval is not None:
os.environ[name] = oldval os.environ[name] = oldval
else: elif value is not None:
del os.environ[name] del os.environ[name]

View File

@ -43,6 +43,19 @@ class TestEnvironSetTemp(unittest.TestCase):
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_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): def tearDown(self):
if 'QUTEBROWSER_ENVIRON_TEST' in os.environ: if 'QUTEBROWSER_ENVIRON_TEST' in os.environ:
# if some test failed # if some test failed

View File

@ -73,7 +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): with helpers.environ_set_temp('HOME', self.temp_dir), \
helpers.environ_set_temp('XDG_DATA_HOME', None):
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'))
@ -81,7 +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): with helpers.environ_set_temp('HOME', self.temp_dir), \
helpers.environ_set_temp('XDG_CONFIG_HOME', None):
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',
@ -90,7 +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): with helpers.environ_set_temp('HOME', self.temp_dir), \
helpers.environ_set_temp('XDG_CACHE_HOME', None):
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'))