1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from cookiecutter import config |
6
|
|
|
from cookiecutter.exceptions import ( |
7
|
|
|
ConfigDoesNotExistException, InvalidConfiguration |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def test_merge_configs(): |
12
|
|
|
default = { |
13
|
|
|
'cookiecutters_dir': '/home/example/some-path-to-templates', |
14
|
|
|
'replay_dir': '/home/example/some-path-to-replay-files', |
15
|
|
|
'default_context': {}, |
16
|
|
|
'abbreviations': { |
17
|
|
|
'gh': 'https://github.com/{0}.git', |
18
|
|
|
'gl': 'https://gitlab.com/{0}.git', |
19
|
|
|
'bb': 'https://bitbucket.org/{0}', |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
user_config = { |
24
|
|
|
'default_context': { |
25
|
|
|
'full_name': 'Raphael Pierzina', |
26
|
|
|
'github_username': 'hackebrot', |
27
|
|
|
}, |
28
|
|
|
'abbreviations': { |
29
|
|
|
'gl': 'https://gitlab.com/hackebrot/{0}.git', |
30
|
|
|
'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git', |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
expected_config = { |
35
|
|
|
'cookiecutters_dir': '/home/example/some-path-to-templates', |
36
|
|
|
'replay_dir': '/home/example/some-path-to-replay-files', |
37
|
|
|
'default_context': { |
38
|
|
|
'full_name': 'Raphael Pierzina', |
39
|
|
|
'github_username': 'hackebrot', |
40
|
|
|
}, |
41
|
|
|
'abbreviations': { |
42
|
|
|
'gh': 'https://github.com/{0}.git', |
43
|
|
|
'gl': 'https://gitlab.com/hackebrot/{0}.git', |
44
|
|
|
'bb': 'https://bitbucket.org/{0}', |
45
|
|
|
'pytest-plugin': 'https://github.com/pytest-dev/pytest-plugin.git', |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
assert config.merge_configs(default, user_config) == expected_config |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def test_get_config(): |
53
|
|
|
""" |
54
|
|
|
Opening and reading config file |
55
|
|
|
""" |
56
|
|
|
conf = config.get_config('tests/test-config/valid-config.yaml') |
57
|
|
|
expected_conf = { |
58
|
|
|
'cookiecutters_dir': '/home/example/some-path-to-templates', |
59
|
|
|
'replay_dir': '/home/example/some-path-to-replay-files', |
60
|
|
|
'default_context': { |
61
|
|
|
'full_name': 'Firstname Lastname', |
62
|
|
|
'email': '[email protected]', |
63
|
|
|
'github_username': 'example' |
64
|
|
|
}, |
65
|
|
|
'abbreviations': { |
66
|
|
|
'gh': 'https://github.com/{0}.git', |
67
|
|
|
'gl': 'https://gitlab.com/{0}.git', |
68
|
|
|
'bb': 'https://bitbucket.org/{0}', |
69
|
|
|
'helloworld': 'https://github.com/hackebrot/helloworld' |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
assert conf == expected_conf |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_get_config_does_not_exist(): |
76
|
|
|
""" |
77
|
|
|
Check that `exceptions.ConfigDoesNotExistException` is raised when |
78
|
|
|
attempting to get a non-existent config file. |
79
|
|
|
""" |
80
|
|
|
with pytest.raises(ConfigDoesNotExistException): |
81
|
|
|
config.get_config('tests/test-config/this-does-not-exist.yaml') |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
def test_invalid_config(): |
85
|
|
|
""" |
86
|
|
|
An invalid config file should raise an `InvalidConfiguration` exception. |
87
|
|
|
""" |
88
|
|
|
with pytest.raises(InvalidConfiguration) as excinfo: |
89
|
|
|
config.get_config('tests/test-config/invalid-config.yaml') |
90
|
|
|
|
91
|
|
|
expected_error_msg = ( |
92
|
|
|
'Unable to parse YAML file ' |
93
|
|
|
'tests/test-config/invalid-config.yaml. ' |
94
|
|
|
'Error: ' |
95
|
|
|
) |
96
|
|
|
assert expected_error_msg in str(excinfo.value) |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_get_config_with_defaults(): |
100
|
|
|
""" |
101
|
|
|
A config file that overrides 1 of 3 defaults |
102
|
|
|
""" |
103
|
|
|
conf = config.get_config('tests/test-config/valid-partial-config.yaml') |
104
|
|
|
default_cookiecutters_dir = os.path.expanduser('~/.cookiecutters/') |
105
|
|
|
default_replay_dir = os.path.expanduser('~/.cookiecutter_replay/') |
106
|
|
|
expected_conf = { |
107
|
|
|
'cookiecutters_dir': default_cookiecutters_dir, |
108
|
|
|
'replay_dir': default_replay_dir, |
109
|
|
|
'default_context': { |
110
|
|
|
'full_name': 'Firstname Lastname', |
111
|
|
|
'email': '[email protected]', |
112
|
|
|
'github_username': 'example' |
113
|
|
|
}, |
114
|
|
|
'abbreviations': { |
115
|
|
|
'gh': 'https://github.com/{0}.git', |
116
|
|
|
'gl': 'https://gitlab.com/{0}.git', |
117
|
|
|
'bb': 'https://bitbucket.org/{0}', |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
assert conf == expected_conf |
121
|
|
|
|