1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import pytest |
3
|
|
|
|
4
|
|
|
from cookiecutter import zipfile |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
def test_prompt_should_ask_and_rm_dir(mocker, tmpdir): |
8
|
|
|
"""In `prompt_and_delete()`, if the user agrees to delete/reclone the |
9
|
|
|
repo, the repo should be deleted. |
10
|
|
|
""" |
11
|
|
|
mock_read_user = mocker.patch( |
12
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
13
|
|
|
return_value=True, |
14
|
|
|
autospec=True |
15
|
|
|
) |
16
|
|
|
dir = tmpdir.mkdir('repo') |
17
|
|
|
|
18
|
|
|
zipfile.prompt_and_delete(str(dir)) |
19
|
|
|
|
20
|
|
|
assert mock_read_user.called |
21
|
|
|
assert not dir.exists() |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
def test_prompt_should_ask_and_keep_dir(mocker, tmpdir): |
25
|
|
|
"""In `prompt_and_delete()`, if the user wants to keep their old |
26
|
|
|
cloned template repo, it should not be deleted. |
27
|
|
|
""" |
28
|
|
|
mock_read_user = mocker.patch( |
29
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
30
|
|
|
return_value=False, |
31
|
|
|
autospec=True |
32
|
|
|
) |
33
|
|
|
dir = tmpdir.mkdir('repo') |
34
|
|
|
|
35
|
|
|
with pytest.raises(SystemExit): |
36
|
|
|
zipfile.prompt_and_delete(str(dir)) |
37
|
|
|
|
38
|
|
|
assert mock_read_user.called |
39
|
|
|
assert dir.exists() |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_prompt_should_not_ask_if_no_input_and_rm_dir(mocker, tmpdir): |
43
|
|
|
"""In `prompt_and_delete()`, if `no_input` is True, the call to |
44
|
|
|
`zipfile.read_user_yes_no()` should be suppressed. |
45
|
|
|
""" |
46
|
|
|
mock_read_user = mocker.patch( |
47
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
48
|
|
|
return_value=True, |
49
|
|
|
autospec=True |
50
|
|
|
) |
51
|
|
|
dir = tmpdir.mkdir('repo') |
52
|
|
|
|
53
|
|
|
zipfile.prompt_and_delete(str(dir), no_input=True) |
54
|
|
|
|
55
|
|
|
assert not mock_read_user.called |
56
|
|
|
assert not dir.exists() |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def test_prompt_should_ask_and_rm_file(mocker, tmpdir): |
60
|
|
|
"""In `prompt_and_delete()`, if the user agrees to delete/reclone the |
61
|
|
|
template zipfile, the zipfile should be deleted. |
62
|
|
|
""" |
63
|
|
|
mock_read_user = mocker.patch( |
64
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
65
|
|
|
return_value=True, |
66
|
|
|
autospec=True |
67
|
|
|
) |
68
|
|
|
file = tmpdir.join('repo.zip') |
69
|
|
|
file.write('this is zipfile content') |
70
|
|
|
|
71
|
|
|
zipfile.prompt_and_delete(str(file)) |
72
|
|
|
|
73
|
|
|
assert mock_read_user.called |
74
|
|
|
assert not file.exists() |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_prompt_should_ask_and_keep_file(mocker, tmpdir): |
78
|
|
|
"""In `prompt_and_delete()`, if the user wants to keep their old |
79
|
|
|
downloaded template zipfile, it should not be deleted. |
80
|
|
|
""" |
81
|
|
|
mock_read_user = mocker.patch( |
82
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
83
|
|
|
return_value=False, |
84
|
|
|
autospec=True |
85
|
|
|
) |
86
|
|
|
file = tmpdir.join('repo.zip') |
87
|
|
|
file.write('this is zipfile content') |
88
|
|
|
|
89
|
|
|
with pytest.raises(SystemExit): |
90
|
|
|
zipfile.prompt_and_delete(str(file)) |
91
|
|
|
|
92
|
|
|
assert mock_read_user.called |
93
|
|
|
assert file.exists() |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
def test_prompt_should_not_ask_if_no_input_and_rm_file(mocker, tmpdir): |
97
|
|
|
"""In `prompt_and_delete()`, if `no_input` is True, the call to |
98
|
|
|
`zipfile.read_user_yes_no()` should be suppressed. |
99
|
|
|
""" |
100
|
|
|
mock_read_user = mocker.patch( |
101
|
|
|
'cookiecutter.zipfile.read_user_yes_no', |
102
|
|
|
return_value=True, |
103
|
|
|
autospec=True |
104
|
|
|
) |
105
|
|
|
file = tmpdir.join('repo.zip') |
106
|
|
|
file.write('this is zipfile content') |
107
|
|
|
|
108
|
|
|
zipfile.prompt_and_delete(str(file), no_input=True) |
109
|
|
|
|
110
|
|
|
assert not mock_read_user.called |
111
|
|
|
assert not file.exists() |
112
|
|
|
|