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

test_unzip_url()   B

Complexity

Conditions 6

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
dl 35
loc 35
rs 7.5384

1 Method

Rating   Name   Duplication   Size   Complexity  
A mock_download() 6 6 3
1
# -*- coding: utf-8 -*-
2
import os
3
from unittest.mock import MagicMock
4
5
import pytest
6
7
from cookiecutter import exceptions, zipfile
8
9
10
def test_unzip_local_file(mocker, tmpdir):
11
    """In `unzip()`, a local file reference is just unzipped where it is.
12
    """
13
    mock_prompt_and_delete = mocker.patch(
14
        'cookiecutter.zipfile.prompt_and_delete',
15
        return_value=True,
16
        autospec=True
17
    )
18
19
    clone_to_dir = tmpdir.mkdir('clone')
20
21
    output_dir = zipfile.unzip(
22
        'tests/files/fake-repo-tmpl.zip',
23
        is_url=False,
24
        clone_to_dir=str(clone_to_dir)
25
    )
26
27
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
28
    assert not mock_prompt_and_delete.called
29
30
31
def test_unzip_should_abort_if_user_does_not_want_to_overwrite_template(mocker, tmpdir):
32
    """In `unzip()`, if user doesn't want to overwrite an existing cached
33
    template, Cookiecutter should exit.
34
    """
35
    mocker.patch(
36
        'cookiecutter.zipfile.prompt_and_delete',
37
        side_effect=SystemExit,
38
        autospec=True
39
    )
40
41
    clone_to_dir = tmpdir.mkdir('clone')
42
    existing_tmpl = clone_to_dir.mkdir('fake-repo-tmpl')
43
44
    with pytest.raises(SystemExit):
45
        zipfile.unzip(
46
            'tests/files/fake-repo-tmpl.zip',
47
            is_url=False,
48
            clone_to_dir=str(clone_to_dir)
49
        )
50
51
52 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...
53
    """In `unzip()`, a url will be downloaded and unzipped
54
    """
55
    mock_prompt_and_delete = mocker.patch(
56
        'cookiecutter.zipfile.prompt_and_delete',
57
        return_value=True,
58
        autospec=True
59
    )
60
61
    def mock_download():
62
        with open('tests/files/fake-repo-tmpl.zip', 'rb') as zipfile:
63
            chunk = zipfile.read(1024)
64
            while chunk:
65
                yield chunk
66
                chunk = zipfile.read(1024)
67
68
    request = MagicMock()
69
    request.iter_content.return_value = mock_download()
70
71
    mock_requests_get = 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
    def mock_download():
100
        with open('tests/files/fake-repo-tmpl.zip', 'rb') as zipfile:
101
            chunk = zipfile.read(1024)
102
            while chunk:
103
                yield chunk
104
                chunk = zipfile.read(1024)
105
106
    request = MagicMock()
107
    request.iter_content.return_value = mock_download()
108
109
    mock_requests_get = mocker.patch(
110
        'cookiecutter.zipfile.requests.get',
111
        return_value=request,
112
        autospec=True,
113
    )
114
115
    clone_to_dir = tmpdir.mkdir('clone')
116
117
    # Create an existing cache of the zipfile
118
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
119
    existing_zip.write('This is an existing zipfile')
120
121
    output_dir = zipfile.unzip(
122
        'https://example.com/path/to/fake-repo-tmpl.zip',
123
        is_url=True,
124
        clone_to_dir=str(clone_to_dir)
125
    )
126
127
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
128
    assert mock_prompt_and_delete.call_count == 1
129
130
131 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...
132
    """In `unzip()`, a url will be downloaded and unzipped; an existing
133
    template directory will be removed
134
    """
135
    mock_prompt_and_delete = mocker.patch(
136
        'cookiecutter.zipfile.prompt_and_delete',
137
        return_value=True,
138
        autospec=True
139
    )
140
141
    def mock_download():
142
        with open('tests/files/fake-repo-tmpl.zip', 'rb') as zipfile:
143
            chunk = zipfile.read(1024)
144
            while chunk:
145
                yield chunk
146
                chunk = zipfile.read(1024)
147
148
    request = MagicMock()
149
    request.iter_content.return_value = mock_download()
150
151
    mock_requests_get = mocker.patch(
152
        'cookiecutter.zipfile.requests.get',
153
        return_value=request,
154
        autospec=True,
155
    )
156
157
    clone_to_dir = tmpdir.mkdir('clone')
158
159
    # Create an existing rolled out template directory
160
    existing_template = clone_to_dir.mkdir('fake-repo-tmpl')
161
162
    output_dir = zipfile.unzip(
163
        'https://example.com/path/to/fake-repo-tmpl.zip',
164
        is_url=True,
165
        clone_to_dir=str(clone_to_dir)
166
    )
167
168
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
169
    assert mock_prompt_and_delete.call_count == 1
170
171
172
def test_unzip_url_existing_cache_and_template(mocker, tmpdir):
173
    """In `unzip()`, a url will be downloaded and unzipped; an existing
174
    zipfile cache and template directory will both be removed
175
    """
176
    mock_prompt_and_delete = mocker.patch(
177
        'cookiecutter.zipfile.prompt_and_delete',
178
        return_value=True,
179
        autospec=True
180
    )
181
182
    def mock_download():
183
        with open('tests/files/fake-repo-tmpl.zip', 'rb') as zipfile:
184
            chunk = zipfile.read(1024)
185
            while chunk:
186
                yield chunk
187
                chunk = zipfile.read(1024)
188
189
    request = MagicMock()
190
    request.iter_content.return_value = mock_download()
191
192
    mock_requests_get = mocker.patch(
193
        'cookiecutter.zipfile.requests.get',
194
        return_value=request,
195
        autospec=True,
196
    )
197
198
    clone_to_dir = tmpdir.mkdir('clone')
199
200
    # Create an existing cache of the zipfile
201
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
202
    existing_zip.write('This is an existing zipfile')
203
204
    # Create an existing rolled out template directory
205
    existing_template = clone_to_dir.mkdir('fake-repo-tmpl')
206
207
    output_dir = zipfile.unzip(
208
        'https://example.com/path/to/fake-repo-tmpl.zip',
209
        is_url=True,
210
        clone_to_dir=str(clone_to_dir)
211
    )
212
213
    assert output_dir == os.path.join(str(clone_to_dir), 'fake-repo-tmpl')
214
    assert mock_prompt_and_delete.call_count == 1
215
216
217
def test_unzip_should_abort_if_user_does_not_want_to_redownload(mocker, tmpdir):
218
    """In `unzip()`, if user doesn't want to download, Cookiecutter should exit
219
    without cloning anything.
220
    """
221
    mocker.patch(
222
        'cookiecutter.zipfile.prompt_and_delete',
223
        side_effect=SystemExit,
224
        autospec=True
225
    )
226
227
    mock_requests_get = mocker.patch(
228
        'cookiecutter.zipfile.requests.get',
229
        autospec=True,
230
    )
231
232
    clone_to_dir = tmpdir.mkdir('clone')
233
234
    # Create an existing cache of the zipfile
235
    existing_zip = clone_to_dir.join('fake-repo-tmpl.zip')
236
    existing_zip.write('This is an existing zipfile')
237
238
    zipfile_url = 'https://example.com/path/to/fake-repo-tmpl.zip'
239
240
    with pytest.raises(SystemExit):
241
        zipfile.unzip(zipfile_url, is_url=True, clone_to_dir=str(clone_to_dir))
242
243
    assert not mock_requests_get.called
244
245