Merge branch 'relapaths' of https://github.com/lamarpavel/qutebrowser into lamarpavel-relapaths
This commit is contained in:
commit
171a0f201b
@ -34,6 +34,7 @@ from PyQt5.QtWidgets import QTabWidget, QTabBar
|
|||||||
|
|
||||||
from qutebrowser.commands import cmdutils
|
from qutebrowser.commands import cmdutils
|
||||||
from qutebrowser.config import configexc
|
from qutebrowser.config import configexc
|
||||||
|
from qutebrowser.utils import standarddir
|
||||||
|
|
||||||
|
|
||||||
SYSTEM_PROXY = object() # Return value for Proxy type
|
SYSTEM_PROXY = object() # Return value for Proxy type
|
||||||
@ -798,6 +799,17 @@ class File(BaseType):
|
|||||||
|
|
||||||
typestr = 'file'
|
typestr = 'file'
|
||||||
|
|
||||||
|
def transform(self, value):
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
value = os.path.expanduser(value)
|
||||||
|
value = os.path.expandvars(value)
|
||||||
|
if not os.path.isabs(value):
|
||||||
|
cfgdir = standarddir.config()
|
||||||
|
if cfgdir is not None:
|
||||||
|
return os.path.join(cfgdir, value)
|
||||||
|
return value
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
if self._none_ok:
|
if self._none_ok:
|
||||||
@ -805,20 +817,26 @@ class File(BaseType):
|
|||||||
else:
|
else:
|
||||||
raise configexc.ValidationError(value, "may not be empty!")
|
raise configexc.ValidationError(value, "may not be empty!")
|
||||||
value = os.path.expanduser(value)
|
value = os.path.expanduser(value)
|
||||||
|
value = os.path.expandvars(value)
|
||||||
try:
|
try:
|
||||||
if not os.path.isfile(value):
|
|
||||||
raise configexc.ValidationError(value, "must be a valid file!")
|
|
||||||
if not os.path.isabs(value):
|
if not os.path.isabs(value):
|
||||||
|
cfgdir = standarddir.config()
|
||||||
|
if cfgdir is None:
|
||||||
|
raise configexc.ValidationError(
|
||||||
|
value, "must be an absolute path when not using a "
|
||||||
|
"config directory!")
|
||||||
|
elif not os.path.isfile(os.path.join(cfgdir, value)):
|
||||||
|
raise configexc.ValidationError(
|
||||||
|
value, "must be a valid path relative to the config "
|
||||||
|
"directory!")
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
elif not os.path.isfile(value):
|
||||||
raise configexc.ValidationError(
|
raise configexc.ValidationError(
|
||||||
value, "must be an absolute path!")
|
value, "must be a valid file!")
|
||||||
except UnicodeEncodeError as e:
|
except UnicodeEncodeError as e:
|
||||||
raise configexc.ValidationError(value, e)
|
raise configexc.ValidationError(value, e)
|
||||||
|
|
||||||
def transform(self, value):
|
|
||||||
if not value:
|
|
||||||
return None
|
|
||||||
return os.path.expanduser(value)
|
|
||||||
|
|
||||||
|
|
||||||
class Directory(BaseType):
|
class Directory(BaseType):
|
||||||
|
|
||||||
@ -1151,6 +1169,16 @@ class UserStyleSheet(File):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(none_ok=True)
|
super().__init__(none_ok=True)
|
||||||
|
|
||||||
|
def transform(self, value):
|
||||||
|
if not value:
|
||||||
|
return None
|
||||||
|
path = super().transform(value)
|
||||||
|
if os.path.exists(path):
|
||||||
|
return QUrl.fromLocalFile(path)
|
||||||
|
else:
|
||||||
|
data = base64.b64encode(value.encode('utf-8')).decode('ascii')
|
||||||
|
return QUrl("data:text/css;charset=utf-8;base64,{}".format(data))
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
if self._none_ok:
|
if self._none_ok:
|
||||||
@ -1160,31 +1188,17 @@ class UserStyleSheet(File):
|
|||||||
value = os.path.expandvars(value)
|
value = os.path.expandvars(value)
|
||||||
value = os.path.expanduser(value)
|
value = os.path.expanduser(value)
|
||||||
try:
|
try:
|
||||||
if not os.path.isabs(value):
|
super().validate(value)
|
||||||
# probably a CSS, so we don't handle it as filename.
|
except configexc.ValidationError:
|
||||||
# FIXME We just try if it is encodable, maybe we should
|
try:
|
||||||
# validate CSS?
|
if not os.path.isabs(value):
|
||||||
# https://github.com/The-Compiler/qutebrowser/issues/115
|
# probably a CSS, so we don't handle it as filename.
|
||||||
try:
|
# FIXME We just try if it is encodable, maybe we should
|
||||||
|
# validate CSS?
|
||||||
|
# https://github.com/The-Compiler/qutebrowser/issues/115
|
||||||
value.encode('utf-8')
|
value.encode('utf-8')
|
||||||
except UnicodeEncodeError as e:
|
except UnicodeEncodeError as e:
|
||||||
raise configexc.ValidationError(value, str(e))
|
raise configexc.ValidationError(value, str(e))
|
||||||
return
|
|
||||||
elif not os.path.isfile(value):
|
|
||||||
raise configexc.ValidationError(value, "must be a valid file!")
|
|
||||||
except UnicodeEncodeError as e:
|
|
||||||
raise configexc.ValidationError(value, e)
|
|
||||||
|
|
||||||
def transform(self, value):
|
|
||||||
path = os.path.expandvars(value)
|
|
||||||
path = os.path.expanduser(path)
|
|
||||||
if not value:
|
|
||||||
return None
|
|
||||||
elif os.path.isabs(path):
|
|
||||||
return QUrl.fromLocalFile(path)
|
|
||||||
else:
|
|
||||||
data = base64.b64encode(value.encode('utf-8')).decode('ascii')
|
|
||||||
return QUrl("data:text/css;charset=utf-8;base64,{}".format(data))
|
|
||||||
|
|
||||||
|
|
||||||
class AutoSearch(BaseType):
|
class AutoSearch(BaseType):
|
||||||
|
@ -1362,6 +1362,7 @@ class TestFile:
|
|||||||
def test_validate_does_not_exist(self, os_path):
|
def test_validate_does_not_exist(self, os_path):
|
||||||
"""Test validate with a file which does not exist."""
|
"""Test validate with a file which does not exist."""
|
||||||
os_path.expanduser.side_effect = lambda x: x
|
os_path.expanduser.side_effect = lambda x: x
|
||||||
|
os_path.expandvars.side_effect = lambda x: x
|
||||||
os_path.isfile.return_value = False
|
os_path.isfile.return_value = False
|
||||||
with pytest.raises(configexc.ValidationError):
|
with pytest.raises(configexc.ValidationError):
|
||||||
self.t.validate('foobar')
|
self.t.validate('foobar')
|
||||||
@ -1369,26 +1370,50 @@ class TestFile:
|
|||||||
def test_validate_exists_abs(self, os_path):
|
def test_validate_exists_abs(self, os_path):
|
||||||
"""Test validate with a file which does exist."""
|
"""Test validate with a file which does exist."""
|
||||||
os_path.expanduser.side_effect = lambda x: x
|
os_path.expanduser.side_effect = lambda x: x
|
||||||
|
os_path.expandvars.side_effect = lambda x: x
|
||||||
os_path.isfile.return_value = True
|
os_path.isfile.return_value = True
|
||||||
os_path.isabs.return_value = True
|
os_path.isabs.return_value = True
|
||||||
self.t.validate('foobar')
|
self.t.validate('foobar')
|
||||||
|
|
||||||
def test_validate_exists_not_abs(self, os_path):
|
def test_validate_exists_rel(self, os_path, monkeypatch):
|
||||||
"""Test validate with a file which does exist but is not absolute."""
|
"""Test validate with a relative path to an existing file."""
|
||||||
|
monkeypatch.setattr(
|
||||||
|
'qutebrowser.config.configtypes.standarddir.config',
|
||||||
|
lambda: '/home/foo/.config/')
|
||||||
os_path.expanduser.side_effect = lambda x: x
|
os_path.expanduser.side_effect = lambda x: x
|
||||||
|
os_path.expandvars.side_effect = lambda x: x
|
||||||
os_path.isfile.return_value = True
|
os_path.isfile.return_value = True
|
||||||
os_path.isabs.return_value = False
|
os_path.isabs.return_value = False
|
||||||
|
self.t.validate('foobar')
|
||||||
|
os_path.join.assert_called_once_with('/home/foo/.config/', 'foobar')
|
||||||
|
|
||||||
|
def test_validate_rel_config_none(self, os_path, monkeypatch):
|
||||||
|
"""Test with a relative path and standarddir.config returning None."""
|
||||||
|
monkeypatch.setattr(
|
||||||
|
'qutebrowser.config.configtypes.standarddir.config', lambda: None)
|
||||||
|
os_path.isabs.return_value = False
|
||||||
with pytest.raises(configexc.ValidationError):
|
with pytest.raises(configexc.ValidationError):
|
||||||
self.t.validate('foobar')
|
self.t.validate('foobar')
|
||||||
|
|
||||||
def test_validate_expanduser(self, os_path):
|
def test_validate_expanduser(self, os_path):
|
||||||
"""Test if validate expands the user correctly."""
|
"""Test if validate expands the user correctly."""
|
||||||
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
|
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
|
||||||
|
os_path.expandvars.side_effect = lambda x: x
|
||||||
os_path.isfile.side_effect = lambda path: path == '/home/foo/foobar'
|
os_path.isfile.side_effect = lambda path: path == '/home/foo/foobar'
|
||||||
os_path.isabs.return_value = True
|
os_path.isabs.return_value = True
|
||||||
self.t.validate('~/foobar')
|
self.t.validate('~/foobar')
|
||||||
os_path.expanduser.assert_called_once_with('~/foobar')
|
os_path.expanduser.assert_called_once_with('~/foobar')
|
||||||
|
|
||||||
|
def test_validate_expandvars(self, os_path):
|
||||||
|
"""Test if validate expands the environment vars correctly."""
|
||||||
|
os_path.expanduser.side_effect = lambda x: x
|
||||||
|
os_path.expandvars.side_effect = lambda x: x.replace(
|
||||||
|
'$HOME', '/home/foo')
|
||||||
|
os_path.isfile.side_effect = lambda path: path == '/home/foo/foobar'
|
||||||
|
os_path.isabs.return_value = True
|
||||||
|
self.t.validate('$HOME/foobar')
|
||||||
|
os_path.expandvars.assert_called_once_with('$HOME/foobar')
|
||||||
|
|
||||||
def test_validate_invalid_encoding(self, os_path, unicode_encode_err):
|
def test_validate_invalid_encoding(self, os_path, unicode_encode_err):
|
||||||
"""Test validate with an invalid encoding, e.g. LC_ALL=C."""
|
"""Test validate with an invalid encoding, e.g. LC_ALL=C."""
|
||||||
os_path.isfile.side_effect = unicode_encode_err
|
os_path.isfile.side_effect = unicode_encode_err
|
||||||
@ -1399,6 +1424,7 @@ class TestFile:
|
|||||||
def test_transform(self, os_path):
|
def test_transform(self, os_path):
|
||||||
"""Test transform."""
|
"""Test transform."""
|
||||||
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
|
os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo')
|
||||||
|
os_path.expandvars.side_effect = lambda x: x
|
||||||
assert self.t.transform('~/foobar') == '/home/foo/foobar'
|
assert self.t.transform('~/foobar') == '/home/foo/foobar'
|
||||||
os_path.expanduser.assert_called_once_with('~/foobar')
|
os_path.expanduser.assert_called_once_with('~/foobar')
|
||||||
|
|
||||||
@ -1930,13 +1956,21 @@ class TestUserStyleSheet:
|
|||||||
"""Test transform with an empty value."""
|
"""Test transform with an empty value."""
|
||||||
assert self.t.transform('') is None
|
assert self.t.transform('') is None
|
||||||
|
|
||||||
def test_transform_file(self):
|
def test_transform_file(self, os_path, mocker):
|
||||||
"""Test transform with a filename."""
|
"""Test transform with a filename."""
|
||||||
|
qurl = mocker.patch('qutebrowser.config.configtypes.QUrl',
|
||||||
|
autospec=True)
|
||||||
|
qurl.fromLocalFile.return_value = QUrl("file:///foo/bar")
|
||||||
|
os_path.exists.return_value = True
|
||||||
path = os.path.join(os.path.sep, 'foo', 'bar')
|
path = os.path.join(os.path.sep, 'foo', 'bar')
|
||||||
assert self.t.transform(path) == QUrl("file:///foo/bar")
|
assert self.t.transform(path) == QUrl("file:///foo/bar")
|
||||||
|
|
||||||
def test_transform_file_expandvars(self, monkeypatch):
|
def test_transform_file_expandvars(self, os_path, monkeypatch, mocker):
|
||||||
"""Test transform with a filename (expandvars)."""
|
"""Test transform with a filename (expandvars)."""
|
||||||
|
qurl = mocker.patch('qutebrowser.config.configtypes.QUrl',
|
||||||
|
autospec=True)
|
||||||
|
qurl.fromLocalFile.return_value = QUrl("file:///foo/bar")
|
||||||
|
os_path.exists.return_value = True
|
||||||
monkeypatch.setenv('FOO', 'foo')
|
monkeypatch.setenv('FOO', 'foo')
|
||||||
path = os.path.join(os.path.sep, '$FOO', 'bar')
|
path = os.path.join(os.path.sep, '$FOO', 'bar')
|
||||||
assert self.t.transform(path) == QUrl("file:///foo/bar")
|
assert self.t.transform(path) == QUrl("file:///foo/bar")
|
||||||
|
Loading…
Reference in New Issue
Block a user