def test_unzip_should_abort_if_no_redownload(mocker, tmpdir): """In `unzip()`, if user doesn't want to download, Cookiecutter should exit without cloning anything. """ mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', side_effect=SystemExit, autospec=True ) mock_requests_get = mocker.patch( 'cookiecutter.zipfile.requests.get', autospec=True, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir)) assert not mock_requests_get.called
def test_unzip_should_abort_if_no_redownload(mocker, tmpdir): """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, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir)) assert not mock_requests_get.called
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_empty_zip_file(mocker, tmpdir): """In `unzip()`, an empty file raises an error.""" mocker.patch('cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip('tests/files/empty.zip', is_url=False, clone_to_dir=str(clone_to_dir))
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='******', )
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_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_unzip_url_existing_cache_no_input(mocker, tmpdir): """In `unzip()`, if no_input is provided, the existing file will be removed. """ request = mocker.MagicMock() request.iter_content.return_value = mock_download() mocker.patch( 'cookiecutter.zipfile.requests.get', return_value=request, autospec=True, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir), no_input=True ) assert output_dir.startswith(tempfile.gettempdir())
def test_unzip_url_existing_cache(mocker, tmpdir): """In `unzip()`, a url will be downloaded and unzipped; an existing 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, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir) ) assert output_dir.startswith(tempfile.gettempdir()) assert mock_prompt_and_delete.call_count == 1
def test_unzip_url(mocker, tmpdir): """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, ) clone_to_dir = tmpdir.mkdir('clone') output_dir = zipfile.unzip( 'https://example.com/path/to/fake-repo-tmpl.zip', is_url=True, clone_to_dir=str(clone_to_dir) ) assert output_dir.startswith(tempfile.gettempdir()) assert not mock_prompt_and_delete.called
def test_unzip_url_existing_cache_no_input(mocker, tmpdir): """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, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir), no_input=True, ) assert output_dir.startswith(tempfile.gettempdir())
def test_unzip_url_existing_cache(mocker, tmpdir): """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, ) clone_to_dir = tmpdir.mkdir('clone') # Create an existing cache of the zipfile existing_zip = clone_to_dir.join('fake-repo-tmpl.zip') existing_zip.write('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_to_dir), ) assert output_dir.startswith(tempfile.gettempdir()) assert mock_prompt_and_delete.call_count == 1
def test_unzip_url(mocker, tmpdir): """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, ) clone_to_dir = tmpdir.mkdir('clone') output_dir = zipfile.unzip( 'https://example.com/path/to/fake-repo-tmpl.zip', is_url=True, clone_to_dir=str(clone_to_dir), ) assert output_dir.startswith(tempfile.gettempdir()) assert not mock_prompt_and_delete.called
def test_bad_zip_file(mocker, tmpdir): """In `unzip()`, a corrupted zip file raises an error. """ mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True ) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip( 'tests/files/bad-zip-file.zip', is_url=False, clone_to_dir=str(clone_to_dir) )
def test_non_repo_zip_file(mocker, tmpdir): """In `unzip()`, a repository must have a top level directory """ mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True ) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip( 'tests/files/not-a-repo.zip', is_url=False, clone_to_dir=str(clone_to_dir) )
def determine_repo_dir(template, abbreviations, clone_to_dir, checkout, no_input, password=None): """ Locate the repository directory from a template reference. Applies repository abbreviations to the template reference. If the template refers to a repository URL, clone it. If the template is a path to a local repository, use it. :param template: A directory containing a project template directory, or a URL to a git repository. :param abbreviations: A dictionary of repository abbreviation definitions. :param clone_to_dir: The directory to clone the repository into. :param checkout: The branch, tag or commit ID to checkout after clone. :param no_input: Prompt the user at command line for manual configuration? :param password: The password to use when extracting the repository. :return: A tuple containing the cookiecutter template directory, and a boolean descriving whether that directory should be cleaned up after the template has been instantiated. :raises: `RepositoryNotFound` if a repository directory could not be found. """ template = expand_abbreviations(template, abbreviations) if is_zip_file(template): unzipped_dir = unzip(zip_uri=template, is_url=is_repo_url(template), clone_to_dir=clone_to_dir, no_input=no_input, password=password) repository_candidates = [unzipped_dir] cleanup = True elif is_repo_url(template): cloned_repo = clone( repo_url=template, checkout=checkout, clone_to_dir=clone_to_dir, no_input=no_input, ) repository_candidates = [cloned_repo] cleanup = False else: repository_candidates = [ template, os.path.join(clone_to_dir, template) ] cleanup = False for repo_candidate in repository_candidates: if repository_has_cookiecutter_config(repo_candidate): return repo_candidate, cleanup raise RepositoryNotFound( 'A valid repository for "{}" could not be found in the following ' 'locations:\n{}'.format(template, '\n'.join(repository_candidates)))
def test_unzip_protected_local_file_bad_environment_password(mocker, tmpdir): """In `unzip()`, an error occurs if the environment has a bad password. """ mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True ) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip( 'tests/files/protected-fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_to_dir), password='******' )
def test_unzip_protected_local_file_user_password_with_noinput(mocker, tmpdir): """In `unzip()`, you can't unpack a password-protected repo in no_input mode """ mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True ) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip( 'tests/files/protected-fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_to_dir), no_input=True )
def test_unzip_protected_local_file_user_bad_password(mocker, tmpdir): """In `unzip()`, If you can't provide a valid password, you get an error """ 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' ) clone_to_dir = tmpdir.mkdir('clone') with pytest.raises(InvalidZipRepository): zipfile.unzip( 'tests/files/protected-fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_to_dir) )
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_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_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='******', ) assert output_dir.startswith(tempfile.gettempdir()) assert not mock_prompt_and_delete.called
def test_unzip_local_file(mocker, tmpdir): """In `unzip()`, a local file reference can be unzipped. """ mock_prompt_and_delete = mocker.patch( 'cookiecutter.zipfile.prompt_and_delete', return_value=True, autospec=True ) clone_to_dir = tmpdir.mkdir('clone') output_dir = zipfile.unzip( 'tests/files/fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_to_dir) ) assert output_dir.startswith(tempfile.gettempdir()) assert not mock_prompt_and_delete.called
def test_unzip_protected_local_file_environment_password(mocker, tmpdir): """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 ) clone_to_dir = tmpdir.mkdir('clone') output_dir = zipfile.unzip( 'tests/files/protected-fake-repo-tmpl.zip', is_url=False, clone_to_dir=str(clone_to_dir), password='******' ) assert output_dir.startswith(tempfile.gettempdir()) assert not mock_prompt_and_delete.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
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