Add initial support for standarddir.config(auto=True)

This doesn't actually migrate things yet.
See #2791, #383.
This commit is contained in:
Florian Bruhin 2017-09-13 14:19:36 +02:00
parent a2f16dbecd
commit ad2598b475
6 changed files with 55 additions and 21 deletions

View File

@ -64,7 +64,8 @@ class YamlConfig:
def __init__(self): def __init__(self):
save_manager = objreg.get('save-manager') save_manager = objreg.get('save-manager')
self._filename = os.path.join(standarddir.config(), 'autoconfig.yml') self._filename = os.path.join(standarddir.config(auto=True),
'autoconfig.yml')
save_manager.add_saveable('yaml-config', self._save) save_manager.add_saveable('yaml-config', self._save)
self.values = {} self.values = {}
@ -113,6 +114,6 @@ def init(config):
# This fixes one of the corruption issues here: # This fixes one of the corruption issues here:
# https://github.com/qutebrowser/qutebrowser/issues/515 # https://github.com/qutebrowser/qutebrowser/issues/515
path = os.path.join(standarddir.config(), 'qsettings') path = os.path.join(standarddir.config(auto=True), 'qsettings')
for fmt in [QSettings.NativeFormat, QSettings.IniFormat]: for fmt in [QSettings.NativeFormat, QSettings.IniFormat]:
QSettings.setPath(fmt, QSettings.UserScope, path) QSettings.setPath(fmt, QSettings.UserScope, path)

View File

@ -56,7 +56,13 @@ def _init_config(args):
_locations[Location.config] = path _locations[Location.config] = path
def config(): def config(auto=False):
"""Get the location for the config directory.
If auto=True is given, get the location for the autoconfig.yml directory,
which is different on macOS.
"""
# FIXME:conf handle auto=True
return _locations[Location.config] return _locations[Location.config]

View File

@ -219,13 +219,17 @@ def _path_info():
Return: Return:
A dictionary of descriptive to actual path names. A dictionary of descriptive to actual path names.
""" """
return { info = {
'config': standarddir.config(), 'config': standarddir.config(),
'data': standarddir.data(), 'data': standarddir.data(),
'system data': standarddir.data(system=True),
'cache': standarddir.cache(), 'cache': standarddir.cache(),
'runtime': standarddir.runtime(), 'runtime': standarddir.runtime(),
} }
if standarddir.config() != standarddir.config(auto=True):
info['auto config'] = standarddir.config(auto=True)
if standarddir.data() != standarddir.data(system=True):
info['system data'] = standarddir.data(system=True)
return info
def _os_info(): def _os_info():
@ -369,7 +373,7 @@ def version():
'', '',
'Paths:', 'Paths:',
] ]
for name, path in _path_info().items(): for name, path in sorted(_path_info().items()):
lines += ['{}: {}'.format(name, path)] lines += ['{}: {}'.format(name, path)]
return '\n'.join(lines) return '\n'.join(lines)

View File

@ -449,7 +449,7 @@ def config_tmpdir(monkeypatch, tmpdir):
confdir = tmpdir / 'config' confdir = tmpdir / 'config'
path = str(confdir) path = str(confdir)
os.mkdir(path) os.mkdir(path)
monkeypatch.setattr(standarddir, 'config', lambda: path) monkeypatch.setattr(standarddir, 'config', lambda auto=False: path)
return confdir return confdir
@ -462,7 +462,7 @@ def data_tmpdir(monkeypatch, tmpdir):
datadir = tmpdir / 'data' datadir = tmpdir / 'data'
path = str(datadir) path = str(datadir)
os.mkdir(path) os.mkdir(path)
monkeypatch.setattr(standarddir, 'data', lambda: path) monkeypatch.setattr(standarddir, 'data', lambda system=False: path)
return datadir return datadir

View File

@ -89,6 +89,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, varname', [ @pytest.mark.parametrize('func, varname', [
(standarddir.data, 'XDG_DATA_HOME'), (standarddir.data, 'XDG_DATA_HOME'),
(standarddir.config, 'XDG_CONFIG_HOME'), (standarddir.config, 'XDG_CONFIG_HOME'),
(lambda: standarddir.config(auto=True), 'XDG_CONFIG_HOME'),
(standarddir.cache, 'XDG_CACHE_HOME'), (standarddir.cache, 'XDG_CACHE_HOME'),
(standarddir.runtime, 'XDG_RUNTIME_DIR'), (standarddir.runtime, 'XDG_RUNTIME_DIR'),
]) ])
@ -107,6 +108,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, subdirs', [ @pytest.mark.parametrize('func, subdirs', [
(standarddir.data, ['.local', 'share', 'qute_test']), (standarddir.data, ['.local', 'share', 'qute_test']),
(standarddir.config, ['.config', 'qute_test']), (standarddir.config, ['.config', 'qute_test']),
(lambda: standarddir.config(auto=True), ['.config', 'qute_test']),
(standarddir.cache, ['.cache', 'qute_test']), (standarddir.cache, ['.cache', 'qute_test']),
(standarddir.download, ['Downloads']), (standarddir.download, ['Downloads']),
]) ])
@ -139,6 +141,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, elems, expected', [ @pytest.mark.parametrize('func, elems, expected', [
(standarddir.data, 2, ['qute_test', 'data']), (standarddir.data, 2, ['qute_test', 'data']),
(standarddir.config, 1, ['qute_test']), (standarddir.config, 1, ['qute_test']),
(lambda: standarddir.config(auto=True), 1, ['qute_test']),
(standarddir.cache, 2, ['qute_test', 'cache']), (standarddir.cache, 2, ['qute_test', 'cache']),
(standarddir.download, 1, ['Downloads']), (standarddir.download, 1, ['Downloads']),
]) ])
@ -150,6 +153,8 @@ class TestStandardDir:
@pytest.mark.parametrize('func, elems, expected', [ @pytest.mark.parametrize('func, elems, expected', [
(standarddir.data, 2, ['Application Support', 'qute_test']), (standarddir.data, 2, ['Application Support', 'qute_test']),
(standarddir.config, 1, ['qute_test']), (standarddir.config, 1, ['qute_test']),
# FIXME:conf Actually support auto=True
(lambda: standarddir.config(auto=True), 1, ['qute_test']),
(standarddir.cache, 2, ['Caches', 'qute_test']), (standarddir.cache, 2, ['Caches', 'qute_test']),
(standarddir.download, 1, ['Downloads']), (standarddir.download, 1, ['Downloads']),
]) ])
@ -166,16 +171,20 @@ class TestArguments:
"""Tests the --basedir argument.""" """Tests the --basedir argument."""
@pytest.mark.parametrize('typ', [ @pytest.mark.parametrize('typ, args', [
'config', 'data', 'cache', 'download', ('config', []),
pytest.param('runtime', marks=pytest.mark.linux)]) ('config', [True]), # user config
def test_basedir(self, tmpdir, typ): ('data', []),
('cache', []),
('download', []),
pytest.param('runtime', [], marks=pytest.mark.linux)])
def test_basedir(self, tmpdir, typ, args):
"""Test --basedir.""" """Test --basedir."""
expected = str(tmpdir / typ) expected = str(tmpdir / typ)
args = types.SimpleNamespace(basedir=str(tmpdir)) init_args = types.SimpleNamespace(basedir=str(tmpdir))
standarddir._init_dirs(args) standarddir._init_dirs(init_args)
func = getattr(standarddir, typ) func = getattr(standarddir, typ)
assert func() == expected assert func(*args) == expected
def test_basedir_relative(self, tmpdir): def test_basedir_relative(self, tmpdir):
"""Test --basedir with a relative path.""" """Test --basedir with a relative path."""

View File

@ -456,12 +456,20 @@ def test_release_info(files, expected, caplog, monkeypatch):
assert caplog.records[0].message == "Error while reading fake-file." assert caplog.records[0].message == "Error while reading fake-file."
def test_path_info(monkeypatch): @pytest.mark.parametrize('equal', [True, False])
"""Test _path_info().""" def test_path_info(monkeypatch, equal):
"""Test _path_info().
Args:
equal: Whether system data / data and system config / config are equal.
"""
patches = { patches = {
'config': lambda: 'CONFIG PATH', 'config': lambda auto=False:
'data': lambda system=False: ('SYSTEM DATA PATH' if system 'AUTO CONFIG PATH' if auto and not equal
else 'DATA PATH'), else 'CONFIG PATH',
'data': lambda system=False:
'SYSTEM DATA PATH' if system and not equal
else 'DATA PATH',
'cache': lambda: 'CACHE PATH', 'cache': lambda: 'CACHE PATH',
'runtime': lambda: 'RUNTIME PATH', 'runtime': lambda: 'RUNTIME PATH',
} }
@ -473,10 +481,16 @@ def test_path_info(monkeypatch):
assert pathinfo['config'] == 'CONFIG PATH' assert pathinfo['config'] == 'CONFIG PATH'
assert pathinfo['data'] == 'DATA PATH' assert pathinfo['data'] == 'DATA PATH'
assert pathinfo['system data'] == 'SYSTEM DATA PATH'
assert pathinfo['cache'] == 'CACHE PATH' assert pathinfo['cache'] == 'CACHE PATH'
assert pathinfo['runtime'] == 'RUNTIME PATH' assert pathinfo['runtime'] == 'RUNTIME PATH'
if equal:
assert 'auto config' not in pathinfo
assert 'system data' not in pathinfo
else:
assert pathinfo['auto config'] == 'AUTO CONFIG PATH'
assert pathinfo['system data'] == 'SYSTEM DATA PATH'
class ImportFake: class ImportFake: