cookiecutter
167 строк · 5.7 Кб
1"""Tests to verify correct work with user configs and system/user variables inside."""
2
3import os4import shutil5
6import pytest7
8from cookiecutter import config9from cookiecutter.exceptions import InvalidConfiguration10
11
12@pytest.fixture(scope='module')13def user_config_path():14"""Fixture. Return user config path for current user."""15return os.path.expanduser('~/.cookiecutterrc')16
17
18@pytest.fixture(scope='function')19def back_up_rc(user_config_path):20"""21Back up an existing cookiecutter rc and restore it after the test.
22
23If ~/.cookiecutterrc is pre-existing, move it to a temp location
24"""
25user_config_path_backup = os.path.expanduser('~/.cookiecutterrc.backup')26
27if os.path.exists(user_config_path):28shutil.copy(user_config_path, user_config_path_backup)29os.remove(user_config_path)30
31yield32# Remove the ~/.cookiecutterrc that has been created in the test.33if os.path.exists(user_config_path):34os.remove(user_config_path)35
36# If it existed, restore the original ~/.cookiecutterrc.37if os.path.exists(user_config_path_backup):38shutil.copy(user_config_path_backup, user_config_path)39os.remove(user_config_path_backup)40
41
42@pytest.fixture43def custom_config():44"""Fixture. Return expected custom configuration for future tests validation."""45return {46'default_context': {47'full_name': 'Firstname Lastname',48'email': 'firstname.lastname@gmail.com',49'github_username': 'example',50'project': {51'description': 'description',52'tags': [53'first',54'second',55'third',56],57},58},59'cookiecutters_dir': '/home/example/some-path-to-templates',60'replay_dir': '/home/example/some-path-to-replay-files',61'abbreviations': {62'gh': 'https://github.com/{0}.git',63'gl': 'https://gitlab.com/{0}.git',64'bb': 'https://bitbucket.org/{0}',65'helloworld': 'https://github.com/hackebrot/helloworld',66},67}68
69
70@pytest.mark.usefixtures('back_up_rc')71def test_get_user_config_valid(user_config_path, custom_config) -> None:72"""Validate user config correctly parsed if exist and correctly formatted."""73shutil.copy('tests/test-config/valid-config.yaml', user_config_path)74conf = config.get_user_config()75
76assert conf == custom_config77
78
79@pytest.mark.usefixtures('back_up_rc')80def test_get_user_config_invalid(user_config_path) -> None:81"""Validate `InvalidConfiguration` raised when provided user config malformed."""82shutil.copy('tests/test-config/invalid-config.yaml', user_config_path)83with pytest.raises(InvalidConfiguration):84config.get_user_config()85
86
87@pytest.mark.usefixtures('back_up_rc')88def test_get_user_config_nonexistent() -> None:89"""Validate default app config returned, if user does not have own config."""90assert config.get_user_config() == config.DEFAULT_CONFIG91
92
93@pytest.fixture94def custom_config_path() -> str:95"""Fixture. Return path to custom user config for tests."""96return 'tests/test-config/valid-config.yaml'97
98
99def test_specify_config_path(mocker, custom_config_path, custom_config) -> None:100"""Validate provided custom config path should be respected and parsed."""101spy_get_config = mocker.spy(config, 'get_config')102
103user_config = config.get_user_config(custom_config_path)104spy_get_config.assert_called_once_with(custom_config_path)105
106assert user_config == custom_config107
108
109def test_default_config_path(user_config_path) -> None:110"""Validate app configuration. User config path should match default path."""111assert user_config_path == config.USER_CONFIG_PATH112
113
114def test_default_config_from_env_variable(115monkeypatch, custom_config_path, custom_config116) -> None:117"""Validate app configuration. User config path should be parsed from sys env."""118monkeypatch.setenv('COOKIECUTTER_CONFIG', custom_config_path)119
120user_config = config.get_user_config()121assert user_config == custom_config122
123
124def test_force_default_config(mocker, custom_config_path) -> None:125"""Validate `default_config=True` should ignore provided custom user config."""126spy_get_config = mocker.spy(config, 'get_config')127
128user_config = config.get_user_config(custom_config_path, default_config=True)129
130assert user_config == config.DEFAULT_CONFIG131assert not spy_get_config.called132
133
134def test_expand_user_for_directories_in_config(monkeypatch) -> None:135"""Validate user pointers expanded in user configs."""136
137def _expanduser(path):138return path.replace('~', 'Users/bob')139
140monkeypatch.setattr('os.path.expanduser', _expanduser)141
142config_file = 'tests/test-config/config-expand-user.yaml'143
144user_config = config.get_user_config(config_file)145assert user_config['replay_dir'] == 'Users/bob/replay-files'146assert user_config['cookiecutters_dir'] == 'Users/bob/templates'147
148
149def test_expand_vars_for_directories_in_config(monkeypatch) -> None:150"""Validate environment variables expanded in user configs."""151monkeypatch.setenv('COOKIES', 'Users/bob/cookies')152
153config_file = 'tests/test-config/config-expand-vars.yaml'154
155user_config = config.get_user_config(config_file)156assert user_config['replay_dir'] == 'Users/bob/cookies/replay-files'157assert user_config['cookiecutters_dir'] == 'Users/bob/cookies/templates'158
159
160def test_specify_config_values() -> None:161"""Validate provided custom config values should be respected."""162replay_dir = 'Users/bob/cookies/custom-replay-dir'163custom_config_updated = {**config.DEFAULT_CONFIG, 'replay_dir': replay_dir}164
165user_config = config.get_user_config(default_config={'replay_dir': replay_dir})166
167assert user_config == custom_config_updated168