forked from cookiecutter/cookiecutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_get_config.py
116 lines (100 loc) · 4.1 KB
/
test_get_config.py
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
"""Collection of tests around loading cookiecutter config."""
import os
import pytest
from ruyaml import YAMLError
# from yaml import YAMLError
from cookiecutter import config
from cookiecutter.exceptions import ConfigDoesNotExistException, InvalidConfiguration
def test_merge_configs():
"""Verify default and user config merged in expected way."""
default = {
'cookiecutters_dir': '/home/example/some-path-to-templates',
'replay_dir': '/home/example/some-path-to-replay-files',
'default_context': {},
'abbreviations': {
'gh': 'https://github.com/{0}.git',
'gl': 'https://gitlab.com/{0}.git',
'bb': 'https://bitbucket.org/{0}',
},
}
user_config = {
'default_context': {
'full_name': 'Raphael Pierzina',
'github_username': 'hackebrot',
},
'abbreviations': {
'gl': 'https://gitlab.com/hackebrot/{0}.git',
'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git',
},
}
expected_config = {
'cookiecutters_dir': '/home/example/some-path-to-templates',
'replay_dir': '/home/example/some-path-to-replay-files',
'default_context': {
'full_name': 'Raphael Pierzina',
'github_username': 'hackebrot',
},
'abbreviations': {
'gh': 'https://github.com/{0}.git',
'gl': 'https://gitlab.com/hackebrot/{0}.git',
'bb': 'https://bitbucket.org/{0}',
'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git',
},
}
assert config.merge_configs(default, user_config) == expected_config
def test_get_config():
"""Verify valid config opened and rendered correctly."""
conf = config.get_config('tests/test-config/valid-config.yaml')
expected_conf = {
'cookiecutters_dir': '/home/example/some-path-to-templates',
'replay_dir': '/home/example/some-path-to-replay-files',
'default_context': {
'full_name': 'Firstname Lastname',
'email': 'firstname.lastname@gmail.com',
'github_username': 'example',
},
'abbreviations': {
'gh': 'https://github.com/{0}.git',
'gl': 'https://gitlab.com/{0}.git',
'bb': 'https://bitbucket.org/{0}',
'helloworld': 'https://github.com/hackebrot/helloworld',
},
}
assert conf == expected_conf
def test_get_config_does_not_exist():
"""Check that `exceptions.ConfigDoesNotExistException` is raised when \
attempting to get a non-existent config file."""
expected_error_msg = 'Config file tests/not-exist.yaml does not exist.'
with pytest.raises(ConfigDoesNotExistException) as exc_info:
config.get_config('tests/not-exist.yaml')
assert str(exc_info.value) == expected_error_msg
def test_invalid_config():
"""An invalid config file should raise an `InvalidConfiguration` \
exception."""
expected_error_msg = (
'Unable to parse YAML file tests/test-config/invalid-config.yaml.'
)
with pytest.raises(InvalidConfiguration) as exc_info:
config.get_config('tests/test-config/invalid-config.yaml')
assert expected_error_msg in str(exc_info.value)
assert isinstance(exc_info.value.__cause__, YAMLError)
def test_get_config_with_defaults():
"""A config file that overrides 1 of 3 defaults."""
conf = config.get_config('tests/test-config/valid-partial-config.yaml')
default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/')
default_replay_dir = os.path.expanduser('~/.cookiecutter_replay/')
expected_conf = {
'cookiecutters_dir': default_cookiecutters_dir,
'replay_dir': default_replay_dir,
'default_context': {
'full_name': 'Firstname Lastname',
'email': 'firstname.lastname@gmail.com',
'github_username': 'example',
},
'abbreviations': {
'gh': 'https://github.com/{0}.git',
'gl': 'https://gitlab.com/{0}.git',
'bb': 'https://bitbucket.org/{0}',
},
}
assert conf == expected_conf