From 0ec05e071f55080816035b668dac9330bda1ffee Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Wed, 18 Feb 2015 13:05:11 +0000 Subject: [PATCH] expand environment vars in settings accepting paths --- qutebrowser/config/configdata.py | 9 +++++++-- qutebrowser/config/configtypes.py | 6 +++++- qutebrowser/test/config/test_configtypes.py | 21 ++++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index a54937309..248d9b1a8 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -51,6 +51,10 @@ FIRST_COMMENT = r""" # Interpolation looks like ${value} or ${section:value} and will be # replaced by the respective value. # +# Some settings will expand environment variables. Note that, since +# interpolation is run first, you will need to escape the $ char as +# described below. +# # This is the default config, so if you want to remove anything from # here (as opposed to change/add), for example a keybinding, set it to # an empty value. @@ -235,7 +239,8 @@ DATA = collections.OrderedDict([ ('user-stylesheet', SettingValue(typ.UserStyleSheet(), '::-webkit-scrollbar { width: 0px; height: 0px; }'), - "User stylesheet to use (absolute filename or CSS string)."), + "User stylesheet to use (absolute filename or CSS string). Will " + "expand environment variables."), ('css-media-type', SettingValue(typ.String(none_ok=True), ''), @@ -444,7 +449,7 @@ DATA = collections.OrderedDict([ ('download-directory', SettingValue(typ.Directory(none_ok=True), ''), "The directory to save downloads to. An empty value selects a " - "sensible os-specific default."), + "sensible os-specific default. Will expand environment variables."), ('maximum-pages-in-cache', SettingValue( diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 69f22f08f..659769054 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -837,6 +837,7 @@ class Directory(BaseType): return else: raise configexc.ValidationError(value, "may not be empty!") + value = os.path.expandvars(value) value = os.path.expanduser(value) if not os.path.isdir(value): raise configexc.ValidationError(value, "must be a valid " @@ -847,6 +848,7 @@ class Directory(BaseType): def transform(self, value): if not value: return None + value = os.path.expandvars(value) return os.path.expanduser(value) @@ -1132,6 +1134,7 @@ class UserStyleSheet(File): return else: raise configexc.ValidationError(value, "may not be empty!") + value = os.path.expandvars(value) value = os.path.expanduser(value) if not os.path.isabs(value): # probably a CSS, so we don't handle it as filename. @@ -1147,7 +1150,8 @@ class UserStyleSheet(File): raise configexc.ValidationError(value, "must be a valid file!") def transform(self, value): - path = os.path.expanduser(value) + path = os.path.expandvars(value) + path = os.path.expanduser(path) if not value: return None elif os.path.isabs(path): diff --git a/qutebrowser/test/config/test_configtypes.py b/qutebrowser/test/config/test_configtypes.py index adc4ada9a..44b7733a6 100644 --- a/qutebrowser/test/config/test_configtypes.py +++ b/qutebrowser/test/config/test_configtypes.py @@ -26,7 +26,7 @@ import base64 from unittest import mock from qutebrowser.config import configtypes, configexc -from qutebrowser.test import stubs +from qutebrowser.test import stubs, helpers from qutebrowser.utils import debug, utils from PyQt5.QtCore import QUrl @@ -1377,14 +1377,27 @@ class DirectoryTests(unittest.TestCase): def test_validate_expanduser(self, os_path): """Test if validate expands the user correctly.""" + os_path.expandvars.side_effect = lambda x: x os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo') os_path.isdir.side_effect = lambda path: path == '/home/foo/foobar' os_path.isabs.return_value = True self.t.validate('~/foobar') os_path.expanduser.assert_called_once_with('~/foobar') + def test_validate_expandvars(self, os_path): + """Test if validate expands the user correctly.""" + os_path.expandvars.side_effect = lambda x: x.replace('$BAR', + '/home/foo/bar') + 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'): + self.t.validate('$BAR/foobar') + os_path.expandvars.assert_called_once_with('$BAR/foobar') + def test_transform(self, os_path): """Test transform.""" + os_path.expandvars.side_effect = lambda x: x os_path.expanduser.side_effect = lambda x: x.replace('~', '/home/foo') self.assertEqual(self.t.transform('~/foobar'), '/home/foo/foobar') os_path.expanduser.assert_called_once_with('~/foobar') @@ -1780,6 +1793,12 @@ class UserStyleSheetTests(unittest.TestCase): path = os.path.join(os.path.sep, 'foo', 'bar') self.assertEqual(self.t.transform(path), QUrl("file:///foo/bar")) + def test_transform_file_expandvars(self): + """Test transform with a filename (expandvars).""" + 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")) + def test_transform_base64(self): """Test transform with a data string.""" b64 = base64.b64encode(b"test").decode('ascii')