1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# -*- coding: utf-8 -*-
"""Test the cli_helpers.config module."""
from __future__ import unicode_literals
import os
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 .utils import with_temp_dir
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✔",
},
"section2": {},
}
DEFAULT_VALID_CONFIG = {
"section": {
"test_boolean_default": True,
"test_string_file": "~/myfile",
"test_option": "foobar✔",
},
"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)
)
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"]
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))
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"]
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)
)
@pytest.mark.skipif(not WIN, reason="requires Windows")
def test_windows_user_config_dir_no_roaming():
"""Test that Windows returns the user config directory without 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
@pytest.mark.skipif(not MAC, reason="requires macOS")
def test_mac_user_config_dir_no_xdg():
"""Test that macOS returns the user config directory without 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
@pytest.mark.skipif(not MAC, reason="requires macOS")
def test_mac_system_config_dirs_no_xdg():
"""Test that macOS returns the system config directories without 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]
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)
with pytest.raises(ValueError):
Config(APP_NAME, APP_AUTHOR, "test_config", validate=True)
with pytest.raises(TypeError):
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()
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)
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")
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.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.read_default_config()
assert config.data == DEFAULT_VALID_CONFIG
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.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")
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.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.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
@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")
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.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.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
@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")
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.read_default_config()
config.write_default_config()
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()
config.write_default_config(overwrite=True)
with open(temp_config_file) as f:
assert "--APPEND--" not in f.read()
def test_read_invalid_config_file():
config_file = "invalid_configrc"
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
@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")
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.read_default_config()
config.write_default_config()
with open(temp_config_file) as f:
assert "test_boolean_default = True" in f.read()
config["section"]["test_boolean_default"] = False
config.write()
with open(temp_config_file) as f:
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 = _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.write(outfile=outfile)
with open(outfile) as f:
assert "test_boolean_default = False" in f.read()
|