1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
|
4
|
|
|
import pytest |
5
|
|
|
|
6
|
|
|
from cookiecutter import repository, exceptions |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
@pytest.mark.parametrize('template, is_url', [ |
10
|
|
|
('/path/to/zipfile.zip', False), |
11
|
|
|
('https://example.com/path/to/zipfile.zip', True), |
12
|
|
|
('http://example.com/path/to/zipfile.zip', True), |
13
|
|
|
]) |
14
|
|
|
def test_zipfile_unzip(mocker, template, is_url, user_config_data): |
15
|
|
|
"""`unzip()` should be called with correct args when |
16
|
|
|
`determine_repo_dir()` is passed a zipfile, or a URL |
17
|
|
|
to a zipfile. |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
mock_clone = mocker.patch( |
21
|
|
|
'cookiecutter.repository.unzip', |
22
|
|
|
return_value='tests/fake-repo-tmpl', |
23
|
|
|
autospec=True |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
project_dir, cleanup = repository.determine_repo_dir( |
27
|
|
|
template, |
28
|
|
|
abbreviations={}, |
29
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
30
|
|
|
checkout=None, |
31
|
|
|
no_input=True, |
32
|
|
|
password=None, |
33
|
|
|
) |
34
|
|
|
|
35
|
|
|
mock_clone.assert_called_once_with( |
36
|
|
|
zip_uri=template, |
37
|
|
|
is_url=is_url, |
38
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
39
|
|
|
no_input=True, |
40
|
|
|
password=None, |
41
|
|
|
) |
42
|
|
|
|
43
|
|
|
assert os.path.isdir(project_dir) |
44
|
|
|
assert cleanup |
45
|
|
|
assert 'tests/fake-repo-tmpl' == project_dir |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
@pytest.fixture |
49
|
|
|
def template_url(): |
50
|
|
|
"""URL to example Cookiecutter template on GitHub. |
51
|
|
|
|
52
|
|
|
Note: when used, git clone is mocked. |
53
|
|
|
""" |
54
|
|
|
return 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git' |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
def test_repository_url_should_clone( |
58
|
|
|
mocker, template_url, user_config_data): |
59
|
|
|
"""`clone()` should be called with correct args when |
60
|
|
|
`determine_repo_dir()` is passed a repository template url. |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
mock_clone = mocker.patch( |
64
|
|
|
'cookiecutter.repository.clone', |
65
|
|
|
return_value='tests/fake-repo-tmpl', |
66
|
|
|
autospec=True |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
project_dir, cleanup = repository.determine_repo_dir( |
70
|
|
|
template_url, |
71
|
|
|
abbreviations={}, |
72
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
73
|
|
|
checkout=None, |
74
|
|
|
no_input=True |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
mock_clone.assert_called_once_with( |
78
|
|
|
repo_url=template_url, |
79
|
|
|
checkout=None, |
80
|
|
|
clone_to_dir=user_config_data['cookiecutters_dir'], |
81
|
|
|
no_input=True |
82
|
|
|
) |
83
|
|
|
|
84
|
|
|
assert os.path.isdir(project_dir) |
85
|
|
|
assert not cleanup |
86
|
|
|
assert 'tests/fake-repo-tmpl' == project_dir |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def test_repository_url_with_no_context_file( |
90
|
|
|
mocker, template_url, user_config_data): |
91
|
|
|
mocker.patch( |
92
|
|
|
'cookiecutter.repository.clone', |
93
|
|
|
return_value='tests/fake-repo-bad', |
94
|
|
|
autospec=True |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
with pytest.raises(exceptions.RepositoryNotFound) as err: |
98
|
|
|
repository.determine_repo_dir( |
99
|
|
|
template_url, |
100
|
|
|
abbreviations={}, |
101
|
|
|
clone_to_dir=None, |
102
|
|
|
checkout=None, |
103
|
|
|
no_input=True |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
assert str(err.value) == ( |
107
|
|
|
'A valid repository for "{}" could not be found in the following ' |
108
|
|
|
'locations:\n{}'.format( |
109
|
|
|
template_url, |
110
|
|
|
'tests/fake-repo-bad', |
111
|
|
|
) |
112
|
|
|
) |
113
|
|
|
|