1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
test_utils |
5
|
|
|
------------ |
6
|
|
|
|
7
|
|
|
Tests for `cookiecutter.utils` module. |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
import os |
11
|
|
|
import pytest |
12
|
|
|
import stat |
13
|
|
|
import sys |
14
|
|
|
|
15
|
|
|
from cookiecutter import utils |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def make_readonly(path): |
19
|
|
|
"""Helper function that is called in the tests to change the access |
20
|
|
|
permissions of the given file. |
21
|
|
|
""" |
22
|
|
|
mode = os.stat(path).st_mode |
23
|
|
|
os.chmod(path, mode & ~stat.S_IWRITE) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_rmtree(): |
27
|
|
|
os.mkdir('foo') |
28
|
|
|
with open('foo/bar', "w") as f: |
29
|
|
|
f.write("Test data") |
30
|
|
|
make_readonly('foo/bar') |
31
|
|
|
utils.rmtree('foo') |
32
|
|
|
assert not os.path.exists('foo') |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_make_sure_path_exists(): |
36
|
|
|
if sys.platform.startswith('win'): |
37
|
|
|
existing_directory = os.path.abspath(os.curdir) |
38
|
|
|
uncreatable_directory = 'a*b' |
39
|
|
|
else: |
40
|
|
|
existing_directory = '/usr/' |
41
|
|
|
uncreatable_directory = '/this-doesnt-exist-and-cant-be-created/' |
42
|
|
|
|
43
|
|
|
assert utils.make_sure_path_exists(existing_directory) |
44
|
|
|
assert utils.make_sure_path_exists('tests/blah') |
45
|
|
|
assert utils.make_sure_path_exists('tests/trailingslash/') |
46
|
|
|
assert not utils.make_sure_path_exists(uncreatable_directory) |
47
|
|
|
utils.rmtree('tests/blah/') |
48
|
|
|
utils.rmtree('tests/trailingslash/') |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
def test_workin(): |
52
|
|
|
cwd = os.getcwd() |
53
|
|
|
ch_to = 'tests/files' |
54
|
|
|
|
55
|
|
|
class TestException(Exception): |
56
|
|
|
pass |
57
|
|
|
|
58
|
|
|
def test_work_in(): |
59
|
|
|
with utils.work_in(ch_to): |
60
|
|
|
test_dir = os.path.join(cwd, ch_to).replace("/", os.sep) |
61
|
|
|
assert test_dir == os.getcwd() |
62
|
|
|
raise TestException() |
63
|
|
|
|
64
|
|
|
# Make sure we return to the correct folder |
65
|
|
|
assert cwd == os.getcwd() |
66
|
|
|
|
67
|
|
|
# Make sure that exceptions are still bubbled up |
68
|
|
|
with pytest.raises(TestException): |
69
|
|
|
test_work_in() |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def test_prompt_should_ask_and_rm_repo_dir(mocker, tmpdir): |
73
|
|
|
"""In `prompt_and_delete()`, if the user agrees to delete/reclone the |
74
|
|
|
repo, the repo should be deleted. |
75
|
|
|
""" |
76
|
|
|
mock_read_user = mocker.patch( |
77
|
|
|
'cookiecutter.utils.read_user_yes_no', |
78
|
|
|
return_value=True, |
79
|
|
|
autospec=True |
80
|
|
|
) |
81
|
|
|
repo_dir = tmpdir.mkdir('repo') |
82
|
|
|
|
83
|
|
|
deleted = utils.prompt_and_delete(str(repo_dir)) |
84
|
|
|
|
85
|
|
|
assert mock_read_user.called |
86
|
|
|
assert not repo_dir.exists() |
87
|
|
|
assert deleted |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def test_prompt_should_ask_and_rm_repo_file(mocker, tmpdir): |
91
|
|
|
"""In `prompt_and_delete()`, if the user agrees to delete/reclone a |
92
|
|
|
repo file, the repo should be deleted. |
93
|
|
|
""" |
94
|
|
|
mock_read_user = mocker.patch( |
95
|
|
|
'cookiecutter.utils.read_user_yes_no', |
96
|
|
|
return_value=True, |
97
|
|
|
autospec=True |
98
|
|
|
) |
99
|
|
|
|
100
|
|
|
repo_file = tmpdir.join('repo.zip') |
101
|
|
|
repo_file.write('this is zipfile content') |
102
|
|
|
|
103
|
|
|
deleted = utils.prompt_and_delete(str(repo_file)) |
104
|
|
|
|
105
|
|
|
assert mock_read_user.called |
106
|
|
|
assert not repo_file.exists() |
107
|
|
|
assert deleted |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def test_prompt_should_ask_and_keep_repo_on_no_reuse(mocker, tmpdir): |
111
|
|
|
"""In `prompt_and_delete()`, if the user wants to keep their old |
112
|
|
|
cloned template repo, it should not be deleted. |
113
|
|
|
""" |
114
|
|
|
mock_read_user = mocker.patch( |
115
|
|
|
'cookiecutter.utils.read_user_yes_no', |
116
|
|
|
return_value=False, |
117
|
|
|
autospec=True |
118
|
|
|
) |
119
|
|
|
repo_dir = tmpdir.mkdir('repo') |
120
|
|
|
|
121
|
|
|
with pytest.raises(SystemExit): |
122
|
|
|
utils.prompt_and_delete(str(repo_dir)) |
123
|
|
|
|
124
|
|
|
assert mock_read_user.called |
125
|
|
|
assert repo_dir.exists() |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_prompt_should_ask_and_keep_repo_on_reuse(mocker, tmpdir): |
129
|
|
|
"""In `prompt_and_delete()`, if the user wants to keep their old |
130
|
|
|
cloned template repo, it should not be deleted. |
131
|
|
|
""" |
132
|
|
|
def answer(question, default): |
133
|
|
|
if 'okay to delete' in question: |
134
|
|
|
return False |
135
|
|
|
else: |
136
|
|
|
return True |
137
|
|
|
|
138
|
|
|
mock_read_user = mocker.patch( |
139
|
|
|
'cookiecutter.utils.read_user_yes_no', |
140
|
|
|
side_effect=answer, |
141
|
|
|
autospec=True |
142
|
|
|
) |
143
|
|
|
repo_dir = tmpdir.mkdir('repo') |
144
|
|
|
|
145
|
|
|
deleted = utils.prompt_and_delete(str(repo_dir)) |
146
|
|
|
|
147
|
|
|
assert mock_read_user.called |
148
|
|
|
assert repo_dir.exists() |
149
|
|
|
assert not deleted |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
def test_prompt_should_not_ask_if_no_input_and_rm_repo_dir(mocker, tmpdir): |
153
|
|
|
"""In `prompt_and_delete()`, if `no_input` is True, the call to |
154
|
|
|
`prompt.read_user_yes_no()` should be suppressed. |
155
|
|
|
""" |
156
|
|
|
mock_read_user = mocker.patch( |
157
|
|
|
'cookiecutter.prompt.read_user_yes_no', |
158
|
|
|
return_value=True, |
159
|
|
|
autospec=True |
160
|
|
|
) |
161
|
|
|
repo_dir = tmpdir.mkdir('repo') |
162
|
|
|
|
163
|
|
|
deleted = utils.prompt_and_delete(str(repo_dir), no_input=True) |
164
|
|
|
|
165
|
|
|
assert not mock_read_user.called |
166
|
|
|
assert not repo_dir.exists() |
167
|
|
|
assert deleted |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
def test_prompt_should_not_ask_if_no_input_and_rm_repo_file(mocker, tmpdir): |
171
|
|
|
"""In `prompt_and_delete()`, if `no_input` is True, the call to |
172
|
|
|
`prompt.read_user_yes_no()` should be suppressed. |
173
|
|
|
""" |
174
|
|
|
mock_read_user = mocker.patch( |
175
|
|
|
'cookiecutter.prompt.read_user_yes_no', |
176
|
|
|
return_value=True, |
177
|
|
|
autospec=True |
178
|
|
|
) |
179
|
|
|
|
180
|
|
|
repo_file = tmpdir.join('repo.zip') |
181
|
|
|
repo_file.write('this is zipfile content') |
182
|
|
|
|
183
|
|
|
deleted = utils.prompt_and_delete(str(repo_file), no_input=True) |
184
|
|
|
|
185
|
|
|
assert not mock_read_user.called |
186
|
|
|
assert not repo_file.exists() |
187
|
|
|
assert deleted |
188
|
|
|
|