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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
"""Tests for function unzip() from zipfile module."""
import shutil
import tempfile
from pathlib import Path
import pytest
from cookiecutter import zipfile
from cookiecutter.exceptions import InvalidZipRepository
def mock_download():
"""Fake download function."""
with Path('tests/files/fake-repo-tmpl.zip').open('rb') as zf:
chunk = zf.read(1024)
while chunk:
yield chunk
chunk = zf.read(1024)
def mock_download_with_empty_chunks():
"""Fake download function."""
yield
with Path('tests/files/fake-repo-tmpl.zip').open('rb') as zf:
chunk = zf.read(1024)
while chunk:
yield chunk
chunk = zf.read(1024)
def test_unzip_local_file(mocker, clone_dir):
"""Local file reference can be unzipped."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
output_dir = zipfile.unzip(
'tests/files/fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_dir)
)
assert output_dir.startswith(tempfile.gettempdir())
assert not mock_prompt_and_delete.called
def test_unzip_protected_local_file_environment_password(mocker, clone_dir):
"""In `unzip()`, the environment can be used to provide a repo password."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
output_dir = zipfile.unzip(
'tests/files/protected-fake-repo-tmpl.zip',
is_url=False,
clone_to_dir=str(clone_dir),
password='sekrit',
)
assert output_dir.startswith(tempfile.gettempdir())
assert not mock_prompt_and_delete.called
def test_unzip_protected_local_file_bad_environment_password(mocker, clone_dir):
"""In `unzip()`, an error occurs if the environment has a bad password."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/protected-fake-repo-tmpl.zip',
is_url=False,
clone_to_dir=str(clone_dir),
password='not-the-right-password',
)
def test_unzip_protected_local_file_user_password_with_noinput(mocker, clone_dir):
"""Can't unpack a password-protected repo in no_input mode."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/protected-fake-repo-tmpl.zip',
is_url=False,
clone_to_dir=str(clone_dir),
no_input=True,
)
def test_unzip_protected_local_file_user_password(mocker, clone_dir):
"""A password-protected local file reference can be unzipped."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
mocker.patch('cookiecutter.zipfile.read_repo_password', return_value='sekrit')
output_dir = zipfile.unzip(
'tests/files/protected-fake-repo-tmpl.zip',
is_url=False,
clone_to_dir=str(clone_dir),
)
assert output_dir.startswith(tempfile.gettempdir())
assert not mock_prompt_and_delete.called
def test_unzip_protected_local_file_user_bad_password(mocker, clone_dir):
"""Error in `unzip()`, if user can't provide a valid password."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
mocker.patch(
'cookiecutter.zipfile.read_repo_password', return_value='not-the-right-password'
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/protected-fake-repo-tmpl.zip',
is_url=False,
clone_to_dir=str(clone_dir),
)
def test_empty_zip_file(mocker, clone_dir):
"""In `unzip()`, an empty file raises an error."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/empty.zip', is_url=False, clone_to_dir=str(clone_dir)
)
def test_non_repo_zip_file(mocker, clone_dir):
"""In `unzip()`, a repository must have a top level directory."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/not-a-repo.zip', is_url=False, clone_to_dir=str(clone_dir)
)
def test_bad_zip_file(mocker, clone_dir):
"""In `unzip()`, a corrupted zip file raises an error."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
with pytest.raises(InvalidZipRepository):
zipfile.unzip(
'tests/files/bad-zip-file.zip', is_url=False, clone_to_dir=str(clone_dir)
)
def test_unzip_url(mocker, clone_dir):
"""In `unzip()`, a url will be downloaded and unzipped."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
request = mocker.MagicMock()
request.iter_content.return_value = mock_download()
mocker.patch(
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)
output_dir = zipfile.unzip(
'https://example.com/path/to/fake-repo-tmpl.zip',
is_url=True,
clone_to_dir=str(clone_dir),
)
assert output_dir.startswith(tempfile.gettempdir())
assert not mock_prompt_and_delete.called
def test_unzip_url_with_empty_chunks(mocker, clone_dir):
"""In `unzip()` empty chunk must be ignored."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
request = mocker.MagicMock()
request.iter_content.return_value = mock_download_with_empty_chunks()
mocker.patch(
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)
output_dir = zipfile.unzip(
'https://example.com/path/to/fake-repo-tmpl.zip',
is_url=True,
clone_to_dir=str(clone_dir),
)
assert output_dir.startswith(tempfile.gettempdir())
assert not mock_prompt_and_delete.called
def test_unzip_url_existing_cache(mocker, clone_dir):
"""Url should be downloaded and unzipped, old zip file will be removed."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True
)
request = mocker.MagicMock()
request.iter_content.return_value = mock_download()
mocker.patch(
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)
# Create an existing cache of the zipfile
existing_zip = clone_dir.joinpath('fake-repo-tmpl.zip')
existing_zip.write_text('This is an existing zipfile')
output_dir = zipfile.unzip(
'https://example.com/path/to/fake-repo-tmpl.zip',
is_url=True,
clone_to_dir=str(clone_dir),
)
assert output_dir.startswith(tempfile.gettempdir())
assert mock_prompt_and_delete.call_count == 1
def test_unzip_url_existing_cache_no_input(mocker, clone_dir):
"""If no_input is provided, the existing file should be removed."""
request = mocker.MagicMock()
request.iter_content.return_value = mock_download()
mocker.patch(
'cookiecutter.zipfile.requests.get',
return_value=request,
autospec=True,
)
# Create an existing cache of the zipfile
existing_zip = clone_dir.joinpath('fake-repo-tmpl.zip')
existing_zip.write_text('This is an existing zipfile')
output_dir = zipfile.unzip(
'https://example.com/path/to/fake-repo-tmpl.zip',
is_url=True,
clone_to_dir=str(clone_dir),
no_input=True,
)
assert output_dir.startswith(tempfile.gettempdir())
def test_unzip_should_abort_if_no_redownload(mocker, clone_dir):
"""Should exit without cloning anything If no redownload."""
mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', side_effect=SystemExit, autospec=True
)
mock_requests_get = mocker.patch(
'cookiecutter.zipfile.requests.get',
autospec=True,
)
# Create an existing cache of the zipfile
existing_zip = clone_dir.joinpath('fake-repo-tmpl.zip')
existing_zip.write_text('This is an existing zipfile')
zipfile_url = 'https://example.com/path/to/fake-repo-tmpl.zip'
with pytest.raises(SystemExit):
zipfile.unzip(zipfile_url, is_url=True, clone_to_dir=str(clone_dir))
assert not mock_requests_get.called
def test_unzip_is_ok_to_reuse(mocker, clone_dir):
"""Already downloaded zip should not be downloaded again."""
mock_prompt_and_delete = mocker.patch(
'cookiecutter.zipfile.prompt_and_delete', return_value=False, autospec=True
)
request = mocker.MagicMock()
existing_zip = clone_dir.joinpath('fake-repo-tmpl.zip')
shutil.copy('tests/files/fake-repo-tmpl.zip', existing_zip)
output_dir = zipfile.unzip(
'https://example.com/path/to/fake-repo-tmpl.zip',
is_url=True,
clone_to_dir=str(clone_dir),
)
assert output_dir.startswith(tempfile.gettempdir())
assert mock_prompt_and_delete.call_count == 1
assert request.iter_content.call_count == 0
|