1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
test_get_user_config |
6
|
|
|
-------------------- |
7
|
|
|
|
8
|
|
|
Tests formerly known from a unittest residing in test_config.py named |
9
|
|
|
TestGetUserConfig.test_get_user_config_valid |
10
|
|
|
TestGetUserConfig.test_get_user_config_invalid |
11
|
|
|
TestGetUserConfig.test_get_user_config_nonexistent |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
import shutil |
15
|
|
|
|
16
|
|
|
import os |
17
|
|
|
import pytest |
18
|
|
|
|
19
|
|
|
from cookiecutter import config |
20
|
|
|
from cookiecutter.exceptions import InvalidConfiguration |
21
|
|
|
from tests.utils import dir_tests |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
@pytest.fixture(scope='function') |
25
|
|
|
def user_config_path(): |
26
|
|
|
return os.path.expanduser('~/.cookiecutterrc') |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_get_user_config_valid(user_config_path): |
30
|
|
|
""" |
31
|
|
|
Get config from a valid ~/.cookiecutterrc file |
32
|
|
|
""" |
33
|
|
|
shutil.copy(dir_tests('test-config/valid-config.yaml'), user_config_path) |
34
|
|
|
conf = config.get_user_config() |
35
|
|
|
expected_conf = { |
36
|
|
|
'cookiecutters_dir': '/home/example/some-path-to-templates', |
37
|
|
|
'replay_dir': '/home/example/some-path-to-replay-files', |
38
|
|
|
'default_context': { |
39
|
|
|
'full_name': 'Firstname Lastname', |
40
|
|
|
'email': '[email protected]', |
41
|
|
|
'github_username': 'example' |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
assert conf == expected_conf |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def test_get_user_config_invalid(user_config_path): |
48
|
|
|
""" |
49
|
|
|
Get config from an invalid ~/.cookiecutterrc file |
50
|
|
|
""" |
51
|
|
|
shutil.copy(dir_tests('test-config/invalid-config.yaml'), user_config_path) |
52
|
|
|
with pytest.raises(InvalidConfiguration): |
53
|
|
|
config.get_user_config() |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def test_get_user_config_nonexistent(): |
57
|
|
|
""" |
58
|
|
|
Get config from a nonexistent ~/.cookiecutterrc file |
59
|
|
|
""" |
60
|
|
|
assert config.get_user_config() == config.get_default_config() |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
@pytest.fixture |
64
|
|
|
def custom_config(): |
65
|
|
|
return { |
66
|
|
|
'cookiecutters_dir': '/foo/bar/some-path-to-templates', |
67
|
|
|
'replay_dir': '/foo/bar/some-path-to-replay-files', |
68
|
|
|
'default_context': { |
69
|
|
|
'full_name': 'Cookiemonster', |
70
|
|
|
'github_username': 'hackebrot' |
71
|
|
|
}, |
72
|
|
|
'abbreviations': { |
73
|
|
|
'cookiedozer': 'https://github.com/hackebrot/cookiedozer.git', |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
@pytest.fixture |
79
|
|
|
def custom_config_path(tmpdir, custom_config): |
80
|
|
|
user_config_file = tmpdir.join('user_config') |
81
|
|
|
|
82
|
|
|
user_config_file.write(config.yaml.dump(custom_config)) |
83
|
|
|
return str(user_config_file) |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
def test_specify_config_path(mocker, custom_config_path, custom_config): |
87
|
|
|
spy_get_config = mocker.spy(config, 'get_config') |
88
|
|
|
|
89
|
|
|
user_config = config.get_user_config(custom_config_path) |
90
|
|
|
spy_get_config.assert_called_once_with(custom_config_path) |
91
|
|
|
|
92
|
|
|
assert user_config == custom_config |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
def test_default_config_path(user_config_path): |
96
|
|
|
assert config.get_user_config_path() == user_config_path |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
def test_default_config_from_env_variable( |
100
|
|
|
monkeypatch, custom_config_path, custom_config): |
101
|
|
|
|
102
|
|
|
monkeypatch.setenv('COOKIECUTTER_CONFIG', custom_config_path) |
103
|
|
|
|
104
|
|
|
user_config = config.get_user_config() |
105
|
|
|
assert user_config == custom_config |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def test_force_default_config(mocker): |
109
|
|
|
spy_get_config = mocker.spy(config, u'get_config') |
110
|
|
|
|
111
|
|
|
user_config = config.get_user_config(None) |
112
|
|
|
|
113
|
|
|
assert user_config == config.get_default_config() |
114
|
|
|
assert not spy_get_config.called |
115
|
|
|
|