forked from cookiecutter/cookiecutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
223 lines (159 loc) · 6.63 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Tests for `cookiecutter.utils` module."""
import stat
import sys
from pathlib import Path
import pytest
from cookiecutter import utils
def make_readonly(path):
"""Change the access permissions to readonly for a given file."""
mode = Path.stat(path).st_mode
Path.chmod(path, mode & ~stat.S_IWRITE)
def test_force_delete(mocker, tmp_path):
"""Verify `utils.force_delete` makes files writable."""
ro_file = Path(tmp_path, 'bar')
ro_file.write_text("Test data")
make_readonly(ro_file)
rmtree = mocker.Mock()
utils.force_delete(rmtree, ro_file, sys.exc_info())
assert (ro_file.stat().st_mode & stat.S_IWRITE) == stat.S_IWRITE
rmtree.assert_called_once_with(ro_file)
utils.rmtree(tmp_path)
def test_rmtree(tmp_path):
"""Verify `utils.rmtree` remove files marked as read-only."""
file_path = Path(tmp_path, "bar")
file_path.write_text("Test data")
make_readonly(file_path)
utils.rmtree(tmp_path)
assert not Path(tmp_path).exists()
def test_make_sure_path_exists(tmp_path):
"""Verify correct True/False response from `utils.make_sure_path_exists`.
Should return True if directory exist or created.
Should return False if impossible to create directory (for example protected)
"""
existing_directory = tmp_path
directory_to_create = Path(tmp_path, "not_yet_created")
utils.make_sure_path_exists(existing_directory)
utils.make_sure_path_exists(directory_to_create)
# Ensure by base system methods.
assert existing_directory.is_dir()
assert existing_directory.exists()
assert directory_to_create.is_dir()
assert directory_to_create.exists()
def test_make_sure_path_exists_correctly_handle_os_error(mocker):
"""Verify correct True/False response from `utils.make_sure_path_exists`.
Should return True if directory exist or created.
Should return False if impossible to create directory (for example protected)
"""
mocker.patch("pathlib.Path.mkdir", side_effect=OSError)
with pytest.raises(OSError) as err:
utils.make_sure_path_exists(Path('protected_path'))
assert str(err.value) == "Unable to create directory at protected_path"
def test_work_in(tmp_path):
"""Verify returning to original folder after `utils.work_in` use."""
cwd = Path.cwd()
ch_to = tmp_path
assert ch_to != Path.cwd()
# Under context manager we should work in tmp_path.
with utils.work_in(ch_to):
assert ch_to == Path.cwd()
# Make sure we return to the correct folder
assert cwd == Path.cwd()
def test_work_in_without_path():
"""Folder is not changed if no path provided."""
cwd = Path.cwd()
with utils.work_in():
assert cwd == Path.cwd()
assert cwd == Path.cwd()
def test_prompt_should_ask_and_rm_repo_dir(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user agrees to delete/reclone the \
repo, the repo should be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no', return_value=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = utils.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert not repo_dir.exists()
assert deleted
def test_prompt_should_ask_and_exit_on_user_no_answer(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user decline to delete/reclone the \
repo, cookiecutter should exit."""
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no',
return_value=False,
)
mock_sys_exit = mocker.patch('sys.exit', return_value=True)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = utils.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
assert not deleted
assert mock_sys_exit.called
def test_prompt_should_ask_and_rm_repo_file(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user agrees to delete/reclone a \
repo file, the repo should be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no', return_value=True, autospec=True
)
repo_file = tmp_path.joinpath('repo.zip')
repo_file.write_text('this is zipfile content')
deleted = utils.prompt_and_delete(str(repo_file))
assert mock_read_user.called
assert not repo_file.exists()
assert deleted
def test_prompt_should_ask_and_keep_repo_on_no_reuse(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user wants to keep their old \
cloned template repo, it should not be deleted."""
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no', return_value=False, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
with pytest.raises(SystemExit):
utils.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
def test_prompt_should_ask_and_keep_repo_on_reuse(mocker, tmp_path):
"""In `prompt_and_delete()`, if the user wants to keep their old \
cloned template repo, it should not be deleted."""
def answer(question, default):
return 'okay to delete' not in question
mock_read_user = mocker.patch(
'cookiecutter.utils.read_user_yes_no', side_effect=answer, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = utils.prompt_and_delete(str(repo_dir))
assert mock_read_user.called
assert repo_dir.exists()
assert not deleted
def test_prompt_should_not_ask_if_no_input_and_rm_repo_dir(mocker, tmp_path):
"""Prompt should not ask if no input and rm dir.
In `prompt_and_delete()`, if `no_input` is True, the call to
`prompt.read_user_yes_no()` should be suppressed.
"""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True, autospec=True
)
repo_dir = Path(tmp_path, 'repo')
repo_dir.mkdir()
deleted = utils.prompt_and_delete(str(repo_dir), no_input=True)
assert not mock_read_user.called
assert not repo_dir.exists()
assert deleted
def test_prompt_should_not_ask_if_no_input_and_rm_repo_file(mocker, tmp_path):
"""Prompt should not ask if no input and rm file.
In `prompt_and_delete()`, if `no_input` is True, the call to
`prompt.read_user_yes_no()` should be suppressed.
"""
mock_read_user = mocker.patch(
'cookiecutter.prompt.read_user_yes_no', return_value=True, autospec=True
)
repo_file = tmp_path.joinpath('repo.zip')
repo_file.write_text('this is zipfile content')
deleted = utils.prompt_and_delete(str(repo_file), no_input=True)
assert not mock_read_user.called
assert not repo_file.exists()
assert deleted