Completed
Pull Request — master (#961)
by
unknown
01:20
created

test_unzip_url_existing_cache()   B

Complexity

Conditions 3

Size

Total Lines 33

Duplication

Lines 33
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
dl 33
loc 33
rs 8.8571
c 2
b 0
f 0
1
# -*- coding: utf-8 -*-
2
import os
3
4
import pytest
5
6
from cookiecutter import zipfile
7
8
9
def mock_download():
10
    with open('tests/files/fake-repo-tmpl.zip', 'rb') as zf:
11
        chunk = zf.read(1024)
12
        while chunk:
13
            yield chunk
14
            chunk = zf.read(1024)
15
16
17
def test_unzip_local_file(mocker, tmpdir):
18
    """In `unzip()`, a local file reference is just unzipped where it is.
19
    """
20
    mock_prompt_and_delete = mocker.patch(
21
        'cookiecutter.zipfile.prompt_and_delete',
22
        return_value=True,
23
        autospec=True
24
    )
25
26
    clone_to_dir = tmpdir.mkdir('clone')
27
28
    output_dir = zipfile.unzip(
29
        'tests/files/fake-repo-tmpl.zip',
30
        is_url=False,
31
        clone_to_dir=str(clone_to_dir)
32
    )
33
34
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
35
    assert not mock_prompt_and_delete.called
36
37
38
def test_unzip_should_abort_not_overwrite_template(mocker, tmpdir):
39
    """In `unzip()`, if user doesn't want to overwrite an existing cached
40
    template, Cookiecutter should exit.
41
    """
42
    mocker.patch(
43
        'cookiecutter.zipfile.prompt_and_delete',
44
        side_effect=SystemExit,
45
        autospec=True
46
    )
47
48
    clone_to_dir = tmpdir.mkdir('clone')
49
    clone_to_dir.mkdir('fake-repo-tmpl')
50
51
    with pytest.raises(SystemExit):
52
        zipfile.unzip(
53
            'tests/files/fake-repo-tmpl.zip',
54
            is_url=False,
55
            clone_to_dir=str(clone_to_dir)
56
        )
57
58
59 View Code Duplication
def test_unzip_url(mocker, tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
60
    """In `unzip()`, a url will be downloaded and unzipped
61
    """
62
    mock_prompt_and_delete = mocker.patch(
63
        'cookiecutter.zipfile.prompt_and_delete',
64
        return_value=True,
65
        autospec=True
66
    )
67
68
    request = mocker.MagicMock()
69
    request.iter_content.return_value = mock_download()
70
71
    mocker.patch(
72
        'cookiecutter.zipfile.requests.get',
73
        return_value=request,
74
        autospec=True,
75
    )
76
77
    clone_to_dir = tmpdir.mkdir('clone')
78
79
    output_dir = zipfile.unzip(
80
        'https://example.com/path/to/fake-repo-tmpl.zip',
81
        is_url=True,
82
        clone_to_dir=str(clone_to_dir)
83
    )
84
85
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
86
    assert not mock_prompt_and_delete.called
87
88
89 View Code Duplication
def test_unzip_url_existing_cache(mocker, tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
90
    """In `unzip()`, a url will be downloaded and unzipped; an existing zip file
91
    will be removed.
92
    """
93
    mock_prompt_and_delete = mocker.patch(
94
        'cookiecutter.zipfile.prompt_and_delete',
95
        return_value=True,
96
        autospec=True
97
    )
98
99
    request = mocker.MagicMock()
100
    request.iter_content.return_value = mock_download()
101
102
    mocker.patch(
103
        'cookiecutter.zipfile.requests.get',
104
        return_value=request,
105
        autospec=True,
106
    )
107
108
    clone_to_dir = tmpdir.mkdir('clone')
109
110
    # Create an existing cache of the zipfile
111
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
112
    existing_zip.write('This is an existing zipfile')
113
114
    output_dir = zipfile.unzip(
115
        'https://example.com/path/to/fake-repo-tmpl.zip',
116
        is_url=True,
117
        clone_to_dir=str(clone_to_dir)
118
    )
119
120
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
121
    assert mock_prompt_and_delete.call_count == 1
122
123
124 View Code Duplication
def test_unzip_url_existing_template(mocker, tmpdir):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
125
    """In `unzip()`, a url will be downloaded and unzipped; an existing
126
    template directory will be removed
127
    """
128
    mock_prompt_and_delete = mocker.patch(
129
        'cookiecutter.zipfile.prompt_and_delete',
130
        return_value=True,
131
        autospec=True
132
    )
133
134
    request = mocker.MagicMock()
135
    request.iter_content.return_value = mock_download()
136
137
    mocker.patch(
138
        'cookiecutter.zipfile.requests.get',
139
        return_value=request,
140
        autospec=True,
141
    )
142
143
    clone_to_dir = tmpdir.mkdir('clone')
144
145
    # Create an existing rolled out template directory
146
    clone_to_dir.mkdir('fake-repo-tmpl')
147
148
    output_dir = zipfile.unzip(
149
        'https://example.com/path/to/fake-repo-tmpl.zip',
150
        is_url=True,
151
        clone_to_dir=str(clone_to_dir)
152
    )
153
154
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
155
    assert mock_prompt_and_delete.call_count == 1
156
157
158
def test_unzip_url_existing_cache_and_template(mocker, tmpdir):
159
    """In `unzip()`, a url will be downloaded and unzipped; an existing
160
    zipfile cache and template directory will both be removed
161
    """
162
    mock_prompt_and_delete = mocker.patch(
163
        'cookiecutter.zipfile.prompt_and_delete',
164
        return_value=True,
165
        autospec=True
166
    )
167
168
    def mock_download():
169
        with open('tests/files/fake-repo-tmpl.zip', 'rb') as zipfile:
170
            chunk = zipfile.read(1024)
171
            while chunk:
172
                yield chunk
173
                chunk = zipfile.read(1024)
174
175
    request = mocker.MagicMock()
176
    request.iter_content.return_value = mock_download()
177
178
    mocker.patch(
179
        'cookiecutter.zipfile.requests.get',
180
        return_value=request,
181
        autospec=True,
182
    )
183
184
    clone_to_dir = tmpdir.mkdir('clone')
185
186
    # Create an existing cache of the zipfile
187
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
188
    existing_zip.write('This is an existing zipfile')
189
190
    # Create an existing rolled out template directory
191
    clone_to_dir.mkdir('fake-repo-tmpl')
192
193
    output_dir = zipfile.unzip(
194
        'https://example.com/path/to/fake-repo-tmpl.zip',
195
        is_url=True,
196
        clone_to_dir=str(clone_to_dir)
197
    )
198
199
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
200
    assert mock_prompt_and_delete.call_count == 1
201
202
203
def test_unzip_should_abort_if_no_redownload(mocker, tmpdir):
204
    """In `unzip()`, if user doesn't want to download, Cookiecutter should exit
205
    without cloning anything.
206
    """
207
    mocker.patch(
208
        'cookiecutter.zipfile.prompt_and_delete',
209
        side_effect=SystemExit,
210
        autospec=True
211
    )
212
213
    mock_requests_get = mocker.patch(
214
        'cookiecutter.zipfile.requests.get',
215
        autospec=True,
216
    )
217
218
    clone_to_dir = tmpdir.mkdir('clone')
219
220
    # Create an existing cache of the zipfile
221
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
222
    existing_zip.write('This is an existing zipfile')
223
224
    zipfile_url = 'https://example.com/path/to/fake-repo-tmpl.zip'
225
226
    with pytest.raises(SystemExit):
227
        zipfile.unzip(zipfile_url, is_url=True, clone_to_dir=str(clone_to_dir))
228
229
    assert not mock_requests_get.called
230