Merge branch 'master' of ssh://tonks/qutebrowser

This commit is contained in:
Florian Bruhin 2015-02-20 17:35:31 +01:00
commit 0ccb104f48
8 changed files with 112 additions and 43 deletions

View File

@ -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)

View File

@ -24,3 +24,4 @@ from PyQt5.QtWidgets import QApplication
# We create a singleton QApplication here.
qApp = QApplication([])
qApp.setApplicationName('qutebrowser')

View File

@ -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
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()

View File

@ -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"))

View File

@ -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=''):

View File

@ -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__':

View File

@ -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)

View File

@ -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,18 +38,18 @@ 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):
"""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,14 +92,14 @@ 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'))
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):
@ -136,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()