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):
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)
self.values = {}
@ -113,6 +114,6 @@ def init(config):
# This fixes one of the corruption issues here:
# 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]:
QSettings.setPath(fmt, QSettings.UserScope, path)

View File

@ -56,7 +56,13 @@ def _init_config(args):
_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]

View File

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

View File

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

View File

@ -89,6 +89,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, varname', [
(standarddir.data, 'XDG_DATA_HOME'),
(standarddir.config, 'XDG_CONFIG_HOME'),
(lambda: standarddir.config(auto=True), 'XDG_CONFIG_HOME'),
(standarddir.cache, 'XDG_CACHE_HOME'),
(standarddir.runtime, 'XDG_RUNTIME_DIR'),
])
@ -107,6 +108,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, subdirs', [
(standarddir.data, ['.local', 'share', 'qute_test']),
(standarddir.config, ['.config', 'qute_test']),
(lambda: standarddir.config(auto=True), ['.config', 'qute_test']),
(standarddir.cache, ['.cache', 'qute_test']),
(standarddir.download, ['Downloads']),
])
@ -139,6 +141,7 @@ class TestStandardDir:
@pytest.mark.parametrize('func, elems, expected', [
(standarddir.data, 2, ['qute_test', 'data']),
(standarddir.config, 1, ['qute_test']),
(lambda: standarddir.config(auto=True), 1, ['qute_test']),
(standarddir.cache, 2, ['qute_test', 'cache']),
(standarddir.download, 1, ['Downloads']),
])
@ -150,6 +153,8 @@ class TestStandardDir:
@pytest.mark.parametrize('func, elems, expected', [
(standarddir.data, 2, ['Application Support', '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.download, 1, ['Downloads']),
])
@ -166,16 +171,20 @@ class TestArguments:
"""Tests the --basedir argument."""
@pytest.mark.parametrize('typ', [
'config', 'data', 'cache', 'download',
pytest.param('runtime', marks=pytest.mark.linux)])
def test_basedir(self, tmpdir, typ):
@pytest.mark.parametrize('typ, args', [
('config', []),
('config', [True]), # user config
('data', []),
('cache', []),
('download', []),
pytest.param('runtime', [], marks=pytest.mark.linux)])
def test_basedir(self, tmpdir, typ, args):
"""Test --basedir."""
expected = str(tmpdir / typ)
args = types.SimpleNamespace(basedir=str(tmpdir))
standarddir._init_dirs(args)
init_args = types.SimpleNamespace(basedir=str(tmpdir))
standarddir._init_dirs(init_args)
func = getattr(standarddir, typ)
assert func() == expected
assert func(*args) == expected
def test_basedir_relative(self, tmpdir):
"""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."
def test_path_info(monkeypatch):
"""Test _path_info()."""
@pytest.mark.parametrize('equal', [True, False])
def test_path_info(monkeypatch, equal):
"""Test _path_info().
Args:
equal: Whether system data / data and system config / config are equal.
"""
patches = {
'config': lambda: 'CONFIG PATH',
'data': lambda system=False: ('SYSTEM DATA PATH' if system
else 'DATA PATH'),
'config': lambda auto=False:
'AUTO CONFIG PATH' if auto and not equal
else 'CONFIG PATH',
'data': lambda system=False:
'SYSTEM DATA PATH' if system and not equal
else 'DATA PATH',
'cache': lambda: 'CACHE PATH',
'runtime': lambda: 'RUNTIME PATH',
}
@ -473,10 +481,16 @@ def test_path_info(monkeypatch):
assert pathinfo['config'] == 'CONFIG PATH'
assert pathinfo['data'] == 'DATA PATH'
assert pathinfo['system data'] == 'SYSTEM DATA PATH'
assert pathinfo['cache'] == 'CACHE 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: