Merging upstream version 2.2.0.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
ab1302c465
commit
95bca6b33d
42 changed files with 1085 additions and 840 deletions
|
@ -8,56 +8,61 @@ from unittest.mock import MagicMock
|
|||
import pytest
|
||||
|
||||
from cli_helpers.compat import MAC, text_type, WIN
|
||||
from cli_helpers.config import (Config, DefaultConfigValidationError,
|
||||
get_system_config_dirs, get_user_config_dir,
|
||||
_pathify)
|
||||
from cli_helpers.config import (
|
||||
Config,
|
||||
DefaultConfigValidationError,
|
||||
get_system_config_dirs,
|
||||
get_user_config_dir,
|
||||
_pathify,
|
||||
)
|
||||
from .utils import with_temp_dir
|
||||
|
||||
APP_NAME, APP_AUTHOR = 'Test', 'Acme'
|
||||
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'config_data')
|
||||
APP_NAME, APP_AUTHOR = "Test", "Acme"
|
||||
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "config_data")
|
||||
DEFAULT_CONFIG = {
|
||||
'section': {
|
||||
'test_boolean_default': 'True',
|
||||
'test_string_file': '~/myfile',
|
||||
'test_option': 'foobar'
|
||||
"section": {
|
||||
"test_boolean_default": "True",
|
||||
"test_string_file": "~/myfile",
|
||||
"test_option": "foobar✔",
|
||||
},
|
||||
'section2': {}
|
||||
"section2": {},
|
||||
}
|
||||
DEFAULT_VALID_CONFIG = {
|
||||
'section': {
|
||||
'test_boolean_default': True,
|
||||
'test_string_file': '~/myfile',
|
||||
'test_option': 'foobar'
|
||||
"section": {
|
||||
"test_boolean_default": True,
|
||||
"test_string_file": "~/myfile",
|
||||
"test_option": "foobar✔",
|
||||
},
|
||||
'section2': {}
|
||||
"section2": {},
|
||||
}
|
||||
|
||||
|
||||
def _mocked_user_config(temp_dir, *args, **kwargs):
|
||||
config = Config(*args, **kwargs)
|
||||
config.user_config_file = MagicMock(return_value=os.path.join(
|
||||
temp_dir, config.filename))
|
||||
config.user_config_file = MagicMock(
|
||||
return_value=os.path.join(temp_dir, config.filename)
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def test_user_config_dir():
|
||||
"""Test that the config directory is a string with the app name in it."""
|
||||
if 'XDG_CONFIG_HOME' in os.environ:
|
||||
del os.environ['XDG_CONFIG_HOME']
|
||||
if "XDG_CONFIG_HOME" in os.environ:
|
||||
del os.environ["XDG_CONFIG_HOME"]
|
||||
config_dir = get_user_config_dir(APP_NAME, APP_AUTHOR)
|
||||
assert isinstance(config_dir, text_type)
|
||||
assert (config_dir.endswith(APP_NAME) or
|
||||
config_dir.endswith(_pathify(APP_NAME)))
|
||||
assert config_dir.endswith(APP_NAME) or config_dir.endswith(_pathify(APP_NAME))
|
||||
|
||||
|
||||
def test_sys_config_dirs():
|
||||
"""Test that the sys config directories are returned correctly."""
|
||||
if 'XDG_CONFIG_DIRS' in os.environ:
|
||||
del os.environ['XDG_CONFIG_DIRS']
|
||||
if "XDG_CONFIG_DIRS" in os.environ:
|
||||
del os.environ["XDG_CONFIG_DIRS"]
|
||||
config_dirs = get_system_config_dirs(APP_NAME, APP_AUTHOR)
|
||||
assert isinstance(config_dirs, list)
|
||||
assert (config_dirs[0].endswith(APP_NAME) or
|
||||
config_dirs[0].endswith(_pathify(APP_NAME)))
|
||||
assert config_dirs[0].endswith(APP_NAME) or config_dirs[0].endswith(
|
||||
_pathify(APP_NAME)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(not WIN, reason="requires Windows")
|
||||
|
@ -66,7 +71,7 @@ def test_windows_user_config_dir_no_roaming():
|
|||
config_dir = get_user_config_dir(APP_NAME, APP_AUTHOR, roaming=False)
|
||||
assert isinstance(config_dir, text_type)
|
||||
assert config_dir.endswith(APP_NAME)
|
||||
assert 'Local' in config_dir
|
||||
assert "Local" in config_dir
|
||||
|
||||
|
||||
@pytest.mark.skipif(not MAC, reason="requires macOS")
|
||||
|
@ -75,7 +80,7 @@ def test_mac_user_config_dir_no_xdg():
|
|||
config_dir = get_user_config_dir(APP_NAME, APP_AUTHOR, force_xdg=False)
|
||||
assert isinstance(config_dir, text_type)
|
||||
assert config_dir.endswith(APP_NAME)
|
||||
assert 'Library' in config_dir
|
||||
assert "Library" in config_dir
|
||||
|
||||
|
||||
@pytest.mark.skipif(not MAC, reason="requires macOS")
|
||||
|
@ -84,53 +89,61 @@ def test_mac_system_config_dirs_no_xdg():
|
|||
config_dirs = get_system_config_dirs(APP_NAME, APP_AUTHOR, force_xdg=False)
|
||||
assert isinstance(config_dirs, list)
|
||||
assert config_dirs[0].endswith(APP_NAME)
|
||||
assert 'Library' in config_dirs[0]
|
||||
assert "Library" in config_dirs[0]
|
||||
|
||||
|
||||
def test_config_reading_raise_errors():
|
||||
"""Test that instantiating Config will raise errors when appropriate."""
|
||||
with pytest.raises(ValueError):
|
||||
Config(APP_NAME, APP_AUTHOR, 'test_config', write_default=True)
|
||||
Config(APP_NAME, APP_AUTHOR, "test_config", write_default=True)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Config(APP_NAME, APP_AUTHOR, 'test_config', validate=True)
|
||||
Config(APP_NAME, APP_AUTHOR, "test_config", validate=True)
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
Config(APP_NAME, APP_AUTHOR, 'test_config', default=b'test')
|
||||
Config(APP_NAME, APP_AUTHOR, "test_config", default=b"test")
|
||||
|
||||
|
||||
def test_config_user_file():
|
||||
"""Test that the Config user_config_file is appropriate."""
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config')
|
||||
assert (get_user_config_dir(APP_NAME, APP_AUTHOR) in
|
||||
config.user_config_file())
|
||||
config = Config(APP_NAME, APP_AUTHOR, "test_config")
|
||||
assert get_user_config_dir(APP_NAME, APP_AUTHOR) in config.user_config_file()
|
||||
|
||||
|
||||
def test_config_reading_default_dict():
|
||||
"""Test that the Config constructor will read in defaults from a dict."""
|
||||
default = {'main': {'foo': 'bar'}}
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config', default=default)
|
||||
default = {"main": {"foo": "bar"}}
|
||||
config = Config(APP_NAME, APP_AUTHOR, "test_config", default=default)
|
||||
assert config.data == default
|
||||
|
||||
|
||||
def test_config_reading_no_default():
|
||||
"""Test that the Config constructor will work without any defaults."""
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config')
|
||||
config = Config(APP_NAME, APP_AUTHOR, "test_config")
|
||||
assert config.data == {}
|
||||
|
||||
|
||||
def test_config_reading_default_file():
|
||||
"""Test that the Config will work with a default file."""
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config',
|
||||
default=os.path.join(TEST_DATA_DIR, 'configrc'))
|
||||
config = Config(
|
||||
APP_NAME,
|
||||
APP_AUTHOR,
|
||||
"test_config",
|
||||
default=os.path.join(TEST_DATA_DIR, "configrc"),
|
||||
)
|
||||
config.read_default_config()
|
||||
assert config.data == DEFAULT_CONFIG
|
||||
|
||||
|
||||
def test_config_reading_configspec():
|
||||
"""Test that the Config default file will work with a configspec."""
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config', validate=True,
|
||||
default=os.path.join(TEST_DATA_DIR, 'configspecrc'))
|
||||
config = Config(
|
||||
APP_NAME,
|
||||
APP_AUTHOR,
|
||||
"test_config",
|
||||
validate=True,
|
||||
default=os.path.join(TEST_DATA_DIR, "configspecrc"),
|
||||
)
|
||||
config.read_default_config()
|
||||
assert config.data == DEFAULT_VALID_CONFIG
|
||||
|
||||
|
@ -138,134 +151,143 @@ def test_config_reading_configspec():
|
|||
def test_config_reading_configspec_with_error():
|
||||
"""Test that reading an invalid configspec raises and exception."""
|
||||
with pytest.raises(DefaultConfigValidationError):
|
||||
config = Config(APP_NAME, APP_AUTHOR, 'test_config', validate=True,
|
||||
default=os.path.join(TEST_DATA_DIR,
|
||||
'invalid_configspecrc'))
|
||||
config = Config(
|
||||
APP_NAME,
|
||||
APP_AUTHOR,
|
||||
"test_config",
|
||||
validate=True,
|
||||
default=os.path.join(TEST_DATA_DIR, "invalid_configspecrc"),
|
||||
)
|
||||
config.read_default_config()
|
||||
|
||||
|
||||
@with_temp_dir
|
||||
def test_write_and_read_default_config(temp_dir=None):
|
||||
config_file = 'test_config'
|
||||
default_file = os.path.join(TEST_DATA_DIR, 'configrc')
|
||||
config_file = "test_config"
|
||||
default_file = os.path.join(TEST_DATA_DIR, "configrc")
|
||||
temp_config_file = os.path.join(temp_dir, config_file)
|
||||
|
||||
config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR, config_file,
|
||||
default=default_file)
|
||||
config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file
|
||||
)
|
||||
config.read_default_config()
|
||||
config.write_default_config()
|
||||
|
||||
user_config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR,
|
||||
config_file, default=default_file)
|
||||
user_config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file
|
||||
)
|
||||
user_config.read()
|
||||
assert temp_config_file in user_config.config_filenames
|
||||
assert user_config == config
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
contents = f.read()
|
||||
assert '# Test file comment' in contents
|
||||
assert '# Test section comment' in contents
|
||||
assert '# Test field comment' in contents
|
||||
assert '# Test field commented out' in contents
|
||||
assert "# Test file comment" in contents
|
||||
assert "# Test section comment" in contents
|
||||
assert "# Test field comment" in contents
|
||||
assert "# Test field commented out" in contents
|
||||
|
||||
|
||||
@with_temp_dir
|
||||
def test_write_and_read_default_config_from_configspec(temp_dir=None):
|
||||
config_file = 'test_config'
|
||||
default_file = os.path.join(TEST_DATA_DIR, 'configspecrc')
|
||||
config_file = "test_config"
|
||||
default_file = os.path.join(TEST_DATA_DIR, "configspecrc")
|
||||
temp_config_file = os.path.join(temp_dir, config_file)
|
||||
|
||||
config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR, config_file,
|
||||
default=default_file, validate=True)
|
||||
config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file, validate=True
|
||||
)
|
||||
config.read_default_config()
|
||||
config.write_default_config()
|
||||
|
||||
user_config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR,
|
||||
config_file, default=default_file,
|
||||
validate=True)
|
||||
user_config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file, validate=True
|
||||
)
|
||||
user_config.read()
|
||||
assert temp_config_file in user_config.config_filenames
|
||||
assert user_config == config
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
contents = f.read()
|
||||
assert '# Test file comment' in contents
|
||||
assert '# Test section comment' in contents
|
||||
assert '# Test field comment' in contents
|
||||
assert '# Test field commented out' in contents
|
||||
assert "# Test file comment" in contents
|
||||
assert "# Test section comment" in contents
|
||||
assert "# Test field comment" in contents
|
||||
assert "# Test field commented out" in contents
|
||||
|
||||
|
||||
@with_temp_dir
|
||||
def test_overwrite_default_config_from_configspec(temp_dir=None):
|
||||
config_file = 'test_config'
|
||||
default_file = os.path.join(TEST_DATA_DIR, 'configspecrc')
|
||||
config_file = "test_config"
|
||||
default_file = os.path.join(TEST_DATA_DIR, "configspecrc")
|
||||
temp_config_file = os.path.join(temp_dir, config_file)
|
||||
|
||||
config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR, config_file,
|
||||
default=default_file, validate=True)
|
||||
config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file, validate=True
|
||||
)
|
||||
config.read_default_config()
|
||||
config.write_default_config()
|
||||
|
||||
with open(temp_config_file, 'a') as f:
|
||||
f.write('--APPEND--')
|
||||
with open(temp_config_file, "a") as f:
|
||||
f.write("--APPEND--")
|
||||
|
||||
config.write_default_config()
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
assert '--APPEND--' in f.read()
|
||||
assert "--APPEND--" in f.read()
|
||||
|
||||
config.write_default_config(overwrite=True)
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
assert '--APPEND--' not in f.read()
|
||||
assert "--APPEND--" not in f.read()
|
||||
|
||||
|
||||
def test_read_invalid_config_file():
|
||||
config_file = 'invalid_configrc'
|
||||
config_file = "invalid_configrc"
|
||||
|
||||
config = _mocked_user_config(TEST_DATA_DIR, APP_NAME, APP_AUTHOR,
|
||||
config_file)
|
||||
config = _mocked_user_config(TEST_DATA_DIR, APP_NAME, APP_AUTHOR, config_file)
|
||||
config.read()
|
||||
assert 'section' in config
|
||||
assert 'test_string_file' in config['section']
|
||||
assert 'test_boolean_default' not in config['section']
|
||||
assert 'section2' in config
|
||||
assert "section" in config
|
||||
assert "test_string_file" in config["section"]
|
||||
assert "test_boolean_default" not in config["section"]
|
||||
assert "section2" in config
|
||||
|
||||
|
||||
@with_temp_dir
|
||||
def test_write_to_user_config(temp_dir=None):
|
||||
config_file = 'test_config'
|
||||
default_file = os.path.join(TEST_DATA_DIR, 'configrc')
|
||||
config_file = "test_config"
|
||||
default_file = os.path.join(TEST_DATA_DIR, "configrc")
|
||||
temp_config_file = os.path.join(temp_dir, config_file)
|
||||
|
||||
config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR, config_file,
|
||||
default=default_file)
|
||||
config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file
|
||||
)
|
||||
config.read_default_config()
|
||||
config.write_default_config()
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
assert 'test_boolean_default = True' in f.read()
|
||||
assert "test_boolean_default = True" in f.read()
|
||||
|
||||
config['section']['test_boolean_default'] = False
|
||||
config["section"]["test_boolean_default"] = False
|
||||
config.write()
|
||||
|
||||
with open(temp_config_file) as f:
|
||||
assert 'test_boolean_default = False' in f.read()
|
||||
assert "test_boolean_default = False" in f.read()
|
||||
|
||||
|
||||
@with_temp_dir
|
||||
def test_write_to_outfile(temp_dir=None):
|
||||
config_file = 'test_config'
|
||||
outfile = os.path.join(temp_dir, 'foo')
|
||||
default_file = os.path.join(TEST_DATA_DIR, 'configrc')
|
||||
config_file = "test_config"
|
||||
outfile = os.path.join(temp_dir, "foo")
|
||||
default_file = os.path.join(TEST_DATA_DIR, "configrc")
|
||||
|
||||
config = _mocked_user_config(temp_dir, APP_NAME, APP_AUTHOR, config_file,
|
||||
default=default_file)
|
||||
config = _mocked_user_config(
|
||||
temp_dir, APP_NAME, APP_AUTHOR, config_file, default=default_file
|
||||
)
|
||||
config.read_default_config()
|
||||
config.write_default_config()
|
||||
|
||||
config['section']['test_boolean_default'] = False
|
||||
config["section"]["test_boolean_default"] = False
|
||||
config.write(outfile=outfile)
|
||||
|
||||
with open(outfile) as f:
|
||||
assert 'test_boolean_default = False' in f.read()
|
||||
assert "test_boolean_default = False" in f.read()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue