Simplify and reorganize configfile tests

Also make save/load of sys.path a little more robust
This commit is contained in:
Jay Kamat 2017-09-21 10:17:25 -04:00
parent 4e22b4666d
commit 43ce10efc3
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 30 additions and 29 deletions

View File

@ -247,7 +247,7 @@ def read_config_py(filename=None):
raise configexc.ConfigFileErrors(basename, [desc])
except SyntaxError as e:
desc = configexc.ConfigErrorDesc("Syntax Error", e,
traceback=traceback.format_exc())
traceback=traceback.format_exc())
raise configexc.ConfigFileErrors(basename, [desc])
try:
@ -257,13 +257,14 @@ def read_config_py(filename=None):
# other files in logical places
config_dir = os.path.dirname(filename)
if config_dir not in sys.path:
sys.path = [config_dir] + sys.path
sys.path.insert(0, config_dir)
exec(code, module.__dict__)
except Exception as e:
api.errors.append(configexc.ConfigErrorDesc(
"Unhandled exception",
exception=e, traceback=traceback.format_exc()))
api.finalize()
return api
@ -271,13 +272,11 @@ def read_config_py(filename=None):
@contextlib.contextmanager
def saved_sys_properties():
"""Save various sys properties such as sys.path and sys.modules."""
old_path = sys.path
old_path = sys.path.copy()
old_modules = sys.modules.copy()
try:
yield
except:
raise
finally:
sys.path = old_path
for module in set(sys.modules).difference(old_modules):

View File

@ -19,6 +19,7 @@
"""Tests for qutebrowser.config.configfiles."""
import os
import sys
import pytest
@ -207,17 +208,39 @@ class TestYaml:
assert error.traceback is None
class ConfPy:
"""Helper class to get a confpy fixture."""
def __init__(self, tmpdir, filename: str = "config.py"):
self._file = tmpdir / filename
self.filename = str(self._file)
def write(self, *lines):
text = '\n'.join(lines)
self._file.write_text(text, 'utf-8', ensure=True)
def read(self):
"""Read the config.py via configfiles and check for errors."""
api = configfiles.read_config_py(self.filename)
assert not api.errors
def write_qbmodule(self):
self.write('import qbmodule',
'qbmodule.run(config)')
class TestConfigPyModules:
pytestmark = pytest.mark.usefixtures('config_stub', 'key_config_stub')
@pytest.fixture
def confpy(self, tmpdir):
return TestConfigPy.ConfPy(tmpdir)
return ConfPy(tmpdir)
@pytest.fixture
def qbmodulepy(self, tmpdir):
return TestConfigPy.ConfPy(tmpdir, filename="qbmodule.py")
return ConfPy(tmpdir, filename="qbmodule.py")
@pytest.fixture(autouse=True)
def restore_sys_path(self):
@ -279,30 +302,9 @@ class TestConfigPy:
pytestmark = pytest.mark.usefixtures('config_stub', 'key_config_stub')
class ConfPy:
"""Helper class to get a confpy fixture."""
def __init__(self, tmpdir, filename: str = "config.py"):
self._confpy = tmpdir / filename
self.filename = str(self._confpy)
def write(self, *lines):
text = '\n'.join(lines)
self._confpy.write_text(text, 'utf-8', ensure=True)
def read(self):
"""Read the config.py via configfiles and check for errors."""
api = configfiles.read_config_py(self.filename)
assert not api.errors
def write_qbmodule(self):
self.write('import qbmodule',
'qbmodule.run(config)')
@pytest.fixture
def confpy(self, tmpdir):
return self.ConfPy(tmpdir)
return ConfPy(tmpdir)
@pytest.mark.parametrize('line', [
'c.colors.hints.bg = "red"',