From 60d4305cc4a80285810d469d03fb5cdee0ad6ed7 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 08:34:24 +0100 Subject: [PATCH 1/6] 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')) From 5fe85d0ddefeaf2de69cc031f42685a45310383d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 09:09:35 +0100 Subject: [PATCH 2/6] Add test for starting with -c ''. --- qutebrowser/test/config/test_config.py | 41 +++++++++++++++++++ .../test/keyinput/test_basekeyparser.py | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/qutebrowser/test/config/test_config.py b/qutebrowser/test/config/test_config.py index e27c49063..2f228e802 100644 --- a/qutebrowser/test/config/test_config.py +++ b/qutebrowser/test/config/test_config.py @@ -20,12 +20,21 @@ """Tests for qutebrowser.config.config.""" +import os +import os.path import unittest import configparser +import tempfile +import types +import shutil +from unittest import mock +from PyQt5.QtCore import QObject from PyQt5.QtGui import QColor from qutebrowser.config import config, configexc +from qutebrowser.test import helpers, stubs +from qutebrowser.utils import objreg class ConfigParserTests(unittest.TestCase): @@ -147,5 +156,37 @@ class DefaultConfigTests(unittest.TestCase): conf._validate_all() +class ConfigInitTests(unittest.TestCase): + + """Test initializing of the config.""" + + def setUp(self): + self.temp_dir = tempfile.mkdtemp() + self.conf_path = os.path.join(self.temp_dir, 'config') + self.data_path = os.path.join(self.temp_dir, 'data') + self.cache_path = os.path.join(self.temp_dir, 'cache') + os.mkdir(self.conf_path) + os.mkdir(self.data_path) + os.mkdir(self.cache_path) + self.env = { + 'XDG_CONFIG_HOME': self.conf_path, + 'XDG_DATA_HOME': self.data_path, + 'XDG_CACHE_HOME': self.cache_path, + } + objreg.register('app', QObject()) + objreg.register('save-manager', mock.MagicMock()) + + def tearDown(self): + shutil.rmtree(self.temp_dir) + objreg.global_registry.clear() + + def test_config_none(self): + """Test initializing with config path set to None.""" + args = types.SimpleNamespace(confdir='') + with helpers.environ_set_temp(self.env): + config.init(args) + self.assertFalse(os.listdir(self.conf_path)) + + if __name__ == '__main__': unittest.main() diff --git a/qutebrowser/test/keyinput/test_basekeyparser.py b/qutebrowser/test/keyinput/test_basekeyparser.py index a9c937637..5f5eb30af 100644 --- a/qutebrowser/test/keyinput/test_basekeyparser.py +++ b/qutebrowser/test/keyinput/test_basekeyparser.py @@ -303,7 +303,7 @@ class CountTests(unittest.TestCase): self.assertEqual(self.kp._keystring, '') def tearDown(self): - objreg.delete('key-config') + objreg.global_registry.clear() if __name__ == '__main__': From 684f0d3df5738d96ef1140b81cb952f372328f35 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 09:12:56 +0100 Subject: [PATCH 3/6] Fix starting with -c '' again. --- qutebrowser/config/config.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index fac5cd386..ee6bcb08a 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -214,7 +214,10 @@ def _init_misc(args): # https://github.com/The-Compiler/qutebrowser/issues/515 config_path = standarddir.get(QStandardPaths.ConfigLocation, args) - path = os.path.join(config_path, 'qsettings') + if config_path is None: + path = os.devnull + else: + path = os.path.join(config_path, 'qsettings') for fmt in (QSettings.NativeFormat, QSettings.IniFormat): QSettings.setPath(fmt, QSettings.UserScope, path) From f33bc7bf313069efc94e1a8e9db19cef7b427a2d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 09:21:59 +0100 Subject: [PATCH 4/6] tests: Get rid of second QCoreApplication. --- qutebrowser/test/__init__.py | 1 + qutebrowser/test/utils/test_standarddir.py | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/qutebrowser/test/__init__.py b/qutebrowser/test/__init__.py index 8c1a54b99..793f6e0d2 100644 --- a/qutebrowser/test/__init__.py +++ b/qutebrowser/test/__init__.py @@ -24,3 +24,4 @@ from PyQt5.QtWidgets import QApplication # We create a singleton QApplication here. qApp = QApplication([]) +qApp.setApplicationName('qutebrowser') diff --git a/qutebrowser/test/utils/test_standarddir.py b/qutebrowser/test/utils/test_standarddir.py index 5e717c672..0ccd73b87 100644 --- a/qutebrowser/test/utils/test_standarddir.py +++ b/qutebrowser/test/utils/test_standarddir.py @@ -26,10 +26,10 @@ import shutil import unittest import tempfile -from PyQt5.QtCore import QStandardPaths, QCoreApplication +from PyQt5.QtCore import QStandardPaths from qutebrowser.utils import standarddir -from qutebrowser.test import helpers +from qutebrowser.test import helpers, qApp class GetStandardDirLinuxTests(unittest.TestCase): @@ -38,13 +38,13 @@ class GetStandardDirLinuxTests(unittest.TestCase): Attributes: temp_dir: A temporary directory. - app: The QCoreApplication used. + old_name: The old applicationName. """ def setUp(self): self.temp_dir = tempfile.mkdtemp() - self.app = QCoreApplication([]) - self.app.setApplicationName('qutebrowser') + self.old_name = qApp.applicationName() + qApp.setApplicationName('qutebrowser') @unittest.skipUnless(sys.platform.startswith("linux"), "requires Linux") def test_data_explicit(self): @@ -99,7 +99,7 @@ class GetStandardDirLinuxTests(unittest.TestCase): 'qutebrowser')) def tearDown(self): - self.app.quit() + qApp.setApplicationName(self.old_name) shutil.rmtree(self.temp_dir) @@ -108,13 +108,16 @@ class GetStandardDirWindowsTests(unittest.TestCase): """Tests for standarddir.get under Windows. Attributes: - app: The QCoreApplication used. + old_name: The old applicationName. """ def setUp(self): - self.app = QCoreApplication([]) + self.old_name = qApp.applicationName() # We can't store the files in a temp dir, so we don't chose qutebrowser - self.app.setApplicationName('qutebrowser_test') + qApp.setApplicationName('qutebrowser_test') + + def tearDown(self): + qApp.setApplicationName(self.old_name) @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows") def test_data(self): From 42c8acc7aabab1c4f7ad28c93eaefa590ef42b50 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 09:23:06 +0100 Subject: [PATCH 5/6] Fix lint --- qutebrowser/test/config/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/test/config/test_config.py b/qutebrowser/test/config/test_config.py index 2f228e802..1c76f6209 100644 --- a/qutebrowser/test/config/test_config.py +++ b/qutebrowser/test/config/test_config.py @@ -33,7 +33,7 @@ from PyQt5.QtCore import QObject from PyQt5.QtGui import QColor from qutebrowser.config import config, configexc -from qutebrowser.test import helpers, stubs +from qutebrowser.test import helpers from qutebrowser.utils import objreg From 05d8a2429b21abe27c0b17944b655ad4372e5554 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 20 Feb 2015 09:27:41 +0100 Subject: [PATCH 6/6] tests: Fix double tearDown. --- qutebrowser/test/utils/test_standarddir.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/qutebrowser/test/utils/test_standarddir.py b/qutebrowser/test/utils/test_standarddir.py index 0ccd73b87..477b56e82 100644 --- a/qutebrowser/test/utils/test_standarddir.py +++ b/qutebrowser/test/utils/test_standarddir.py @@ -139,6 +139,3 @@ class GetStandardDirWindowsTests(unittest.TestCase): cur_dir = standarddir.get(QStandardPaths.CacheLocation) self.assertEqual(cur_dir.split(os.sep)[-2:], ['qutebrowser_test', 'cache'], cur_dir) - - def tearDown(self): - self.app.quit()