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
|
"""Tests to verify correct work with user configs and system/user variables inside."""
import os
import shutil
import pytest
from cookiecutter import config
from cookiecutter.exceptions import InvalidConfiguration
@pytest.fixture(scope='module')
def user_config_path():
"""Fixture. Return user config path for current user."""
return os.path.expanduser('~/.cookiecutterrc')
@pytest.fixture(scope='function')
def back_up_rc(user_config_path):
"""
Back up an existing cookiecutter rc and restore it after the test.
If ~/.cookiecutterrc is pre-existing, move it to a temp location
"""
user_config_path_backup = os.path.expanduser('~/.cookiecutterrc.backup')
if os.path.exists(user_config_path):
shutil.copy(user_config_path, user_config_path_backup)
os.remove(user_config_path)
yield
# Remove the ~/.cookiecutterrc that has been created in the test.
if os.path.exists(user_config_path):
os.remove(user_config_path)
# If it existed, restore the original ~/.cookiecutterrc.
if os.path.exists(user_config_path_backup):
shutil.copy(user_config_path_backup, user_config_path)
os.remove(user_config_path_backup)
@pytest.fixture
def custom_config():
"""Fixture. Return expected custom configuration for future tests validation."""
return {
'default_context': {
'full_name': 'Firstname Lastname',
'email': 'firstname.lastname@gmail.com',
'github_username': 'example',
'project': {
'description': 'description',
'tags': [
'first',
'second',
'third',
],
},
},
'cookiecutters_dir': '/home/example/some-path-to-templates',
'replay_dir': '/home/example/some-path-to-replay-files',
'abbreviations': {
'gh': 'https://github.com/{0}.git',
'gl': 'https://gitlab.com/{0}.git',
'bb': 'https://bitbucket.org/{0}',
'helloworld': 'https://github.com/hackebrot/helloworld',
},
}
@pytest.mark.usefixtures('back_up_rc')
def test_get_user_config_valid(user_config_path, custom_config):
"""Validate user config correctly parsed if exist and correctly formatted."""
shutil.copy('tests/test-config/valid-config.yaml', user_config_path)
conf = config.get_user_config()
assert conf == custom_config
@pytest.mark.usefixtures('back_up_rc')
def test_get_user_config_invalid(user_config_path):
"""Validate `InvalidConfiguration` raised when provided user config malformed."""
shutil.copy('tests/test-config/invalid-config.yaml', user_config_path)
with pytest.raises(InvalidConfiguration):
config.get_user_config()
@pytest.mark.usefixtures('back_up_rc')
def test_get_user_config_nonexistent():
"""Validate default app config returned, if user does not have own config."""
assert config.get_user_config() == config.DEFAULT_CONFIG
@pytest.fixture
def custom_config_path():
"""Fixture. Return path to custom user config for tests."""
return 'tests/test-config/valid-config.yaml'
def test_specify_config_path(mocker, custom_config_path, custom_config):
"""Validate provided custom config path should be respected and parsed."""
spy_get_config = mocker.spy(config, 'get_config')
user_config = config.get_user_config(custom_config_path)
spy_get_config.assert_called_once_with(custom_config_path)
assert user_config == custom_config
def test_default_config_path(user_config_path):
"""Validate app configuration. User config path should match default path."""
assert config.USER_CONFIG_PATH == user_config_path
def test_default_config_from_env_variable(
monkeypatch, custom_config_path, custom_config
):
"""Validate app configuration. User config path should be parsed from sys env."""
monkeypatch.setenv('COOKIECUTTER_CONFIG', custom_config_path)
user_config = config.get_user_config()
assert user_config == custom_config
def test_force_default_config(mocker, custom_config_path):
"""Validate `default_config=True` should ignore provided custom user config."""
spy_get_config = mocker.spy(config, 'get_config')
user_config = config.get_user_config(custom_config_path, default_config=True)
assert user_config == config.DEFAULT_CONFIG
assert not spy_get_config.called
def test_expand_user_for_directories_in_config(monkeypatch):
"""Validate user pointers expanded in user configs."""
def _expanduser(path):
return path.replace('~', 'Users/bob')
monkeypatch.setattr('os.path.expanduser', _expanduser)
config_file = 'tests/test-config/config-expand-user.yaml'
user_config = config.get_user_config(config_file)
assert user_config['replay_dir'] == 'Users/bob/replay-files'
assert user_config['cookiecutters_dir'] == 'Users/bob/templates'
def test_expand_vars_for_directories_in_config(monkeypatch):
"""Validate environment variables expanded in user configs."""
monkeypatch.setenv('COOKIES', 'Users/bob/cookies')
config_file = 'tests/test-config/config-expand-vars.yaml'
user_config = config.get_user_config(config_file)
assert user_config['replay_dir'] == 'Users/bob/cookies/replay-files'
assert user_config['cookiecutters_dir'] == 'Users/bob/cookies/templates'
def test_specify_config_values():
"""Validate provided custom config values should be respected."""
replay_dir = 'Users/bob/cookies/custom-replay-dir'
custom_config_updated = {**config.DEFAULT_CONFIG, 'replay_dir': replay_dir}
user_config = config.get_user_config(default_config={'replay_dir': replay_dir})
assert user_config == custom_config_updated
|