1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from cookiecutter.main import is_repo_url, expand_abbreviations, cookiecutter |
8
|
|
|
|
9
|
|
|
USER_CONFIG = u""" |
10
|
|
|
cookiecutters_dir: "{cookiecutters_dir}" |
11
|
|
|
replay_dir: "{replay_dir}" |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
@pytest.fixture(params=[ |
16
|
|
|
'gitolite@server:team/repo', |
17
|
|
|
'[email protected]:audreyr/cookiecutter.git', |
18
|
|
|
'https://github.com/audreyr/cookiecutter.git', |
19
|
|
|
'git+https://private.com/gitrepo', |
20
|
|
|
'hg+https://private.com/mercurialrepo', |
21
|
|
|
'https://bitbucket.org/pokoli/cookiecutter.hg', |
22
|
|
|
]) |
23
|
|
|
def remote_repo_url(request): |
24
|
|
|
return request.param |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_is_repo_url_for_remote_urls(remote_repo_url): |
28
|
|
|
"""Verify is_repo_url works.""" |
29
|
|
|
assert is_repo_url(remote_repo_url) is True |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
@pytest.fixture(params=[ |
33
|
|
|
'/audreyr/cookiecutter.git', |
34
|
|
|
'/home/audreyr/cookiecutter', |
35
|
|
|
( |
36
|
|
|
'c:\\users\\appveyor\\appdata\\local\\temp\\1\\pytest-0\\' |
37
|
|
|
'test_default_output_dir0\\template' |
38
|
|
|
), |
39
|
|
|
]) |
40
|
|
|
def local_repo_url(request): |
41
|
|
|
return request.param |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def test_is_repo_url_for_local_urls(local_repo_url): |
45
|
|
|
"""Verify is_repo_url works.""" |
46
|
|
|
assert is_repo_url(local_repo_url) is False |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_expand_abbreviations(): |
50
|
|
|
template = 'gh:audreyr/cookiecutter-pypackage' |
51
|
|
|
|
52
|
|
|
# This is not a valid repo url just yet! |
53
|
|
|
# First `main.expand_abbreviations` needs to translate it |
54
|
|
|
assert is_repo_url(template) is False |
55
|
|
|
|
56
|
|
|
expanded_template = expand_abbreviations(template, {}) |
57
|
|
|
assert is_repo_url(expanded_template) is True |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
@pytest.fixture(scope='session') |
61
|
|
|
def user_dir(tmpdir_factory): |
62
|
|
|
"""Fixture that simulates the user's home directory""" |
63
|
|
|
return tmpdir_factory.mktemp('user_dir') |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
@pytest.fixture(scope='session') |
67
|
|
|
def user_config_data(user_dir): |
68
|
|
|
"""Fixture that creates 2 Cookiecutter user config dirs in the user's home |
69
|
|
|
directory: |
70
|
|
|
* `cookiecutters_dir` |
71
|
|
|
* `cookiecutter_replay` |
72
|
|
|
|
73
|
|
|
:returns: Dict with name of both user config dirs |
74
|
|
|
""" |
75
|
|
|
cookiecutters_dir = user_dir.mkdir('cookiecutters') |
76
|
|
|
replay_dir = user_dir.mkdir('cookiecutter_replay') |
77
|
|
|
|
78
|
|
|
return { |
79
|
|
|
'cookiecutters_dir': str(cookiecutters_dir), |
80
|
|
|
'replay_dir': str(replay_dir), |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
@pytest.fixture(scope='session') |
85
|
|
|
def user_config_file(user_dir, user_config_data): |
86
|
|
|
"""Fixture that creates a config file called `config` in the user's home |
87
|
|
|
directory, with YAML from `user_config_data`. |
88
|
|
|
|
89
|
|
|
:param user_dir: Simulated user's home directory |
90
|
|
|
:param user_config_data: Dict of config values |
91
|
|
|
:returns: String of path to config file |
92
|
|
|
""" |
93
|
|
|
config_file = user_dir.join('config') |
94
|
|
|
|
95
|
|
|
config_text = USER_CONFIG.format(**user_config_data) |
96
|
|
|
config_file.write(config_text) |
97
|
|
|
return str(config_file) |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
@pytest.fixture |
101
|
|
|
def template_url(): |
102
|
|
|
"""URL to example Cookiecutter template on GitHub. |
103
|
|
|
|
104
|
|
|
Note: when used, git clone is mocked. |
105
|
|
|
""" |
106
|
|
|
return 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git' |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
@pytest.fixture |
110
|
|
|
def output_dir(tmpdir): |
111
|
|
|
"""Given a temporary dir, create an `output` subdirectory inside it and |
112
|
|
|
return its path (not a str but a py.path instance). |
113
|
|
|
""" |
114
|
|
|
return tmpdir.mkdir('output') |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
def test_cookiecutter_repository_url_should_clone( |
118
|
|
|
mocker, template_url, output_dir, user_config_file, user_config_data): |
119
|
|
|
"""`clone()` should be called with correct args when `cookiecutter()` is |
120
|
|
|
called. |
121
|
|
|
""" |
122
|
|
|
mock_clone = mocker.patch( |
123
|
|
|
'cookiecutter.main.clone', |
124
|
|
|
return_value='tests/fake-repo-tmpl', |
125
|
|
|
autospec=True |
126
|
|
|
) |
127
|
|
|
|
128
|
|
|
project_dir = cookiecutter( |
129
|
|
|
template_url, |
130
|
|
|
no_input=True, |
131
|
|
|
output_dir=str(output_dir), |
132
|
|
|
config_file=user_config_file |
133
|
|
|
) |
134
|
|
|
|
135
|
|
|
mock_clone.assert_called_once_with( |
136
|
|
|
repo_url=template_url, |
137
|
|
|
checkout=None, |
138
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
139
|
|
|
no_input=True |
140
|
|
|
) |
141
|
|
|
|
142
|
|
|
assert os.path.isdir(project_dir) |
143
|
|
|
|