Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_repository_url_with_no_context_file(
mocker, template_url, user_config_data):
mocker.patch(
'cookiecutter.repository.clone',
return_value='tests/fake-repo-bad',
autospec=True
)
with pytest.raises(exceptions.RepositoryNotFound) as err:
repository.determine_repo_dir(
template_url,
abbreviations={},
clone_to_dir=None,
checkout=None,
no_input=True
)
assert str(err.value) == (
'A valid repository for "{}" could not be found in the following '
'locations:\n{}'.format(
template_url,
'tests/fake-repo-bad',
)
def test_must_fail_when_repo_not_found(self):
location = str(Path("my", "folder"))
with patch.object(repository, "unzip") as unzip_mock:
unzip_mock.side_effect = RepositoryNotFound("repo")
with self.assertRaises(ArbitraryProjectDownloadFailed):
generate_non_cookiecutter_project(location, self.output_dir)
"""In `clone()`, repository not found errors should raise an
appropriate exception.
"""
# side_effect is set to an iterable here (and below),
# because of a Python 3.4 unittest.mock regression
# http://bugs.python.org/issue23661
mocker.patch(
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
side_effect=[subprocess.CalledProcessError(
-1, 'cmd', output=error_message
)]
)
repository_url = 'https://github.com/hackebro/cookiedozer'
with pytest.raises(exceptions.RepositoryNotFound) as err:
vcs.clone(
repository_url,
clone_to_dir=clone_dir,
no_input=True
)
assert str(err.value) == (
'The repository {} could not be found, have you made a typo?'
).format(repository_url)
def test_local_repo_typo(tmpdir):
"""An unknown local repository should raise a `RepositoryNotFound`
exception.
"""
template_path = os.path.join('tests', 'unknown-repo')
with pytest.raises(exceptions.RepositoryNotFound) as err:
repository.determine_repo_dir(
template_path,
abbreviations={},
clone_to_dir=str(tmpdir),
checkout=None,
no_input=True
)
assert str(err.value) == (
'A valid repository for "{}" could not be found in the following '
'locations:\n{}'.format(
template_path,
'\n'.join([
template_path,
str(tmpdir / 'tests/unknown-repo')
]),
LOG.debug("%s location is a zip file", location)
download_fn = functools.partial(
repository.unzip, zip_uri=location, is_url=repository.is_repo_url(location), no_input=no_input
)
# Else, treat it as a git/hg/ssh URL and try to clone
elif repository.is_repo_url(location):
LOG.debug("%s location is a source control repository", location)
download_fn = functools.partial(repository.clone, repo_url=location, no_input=no_input)
else:
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)
try:
return _download_and_copy(download_fn, output_dir)
except exceptions.RepositoryNotFound:
# Download failed because the zip or the repository was not found
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)
LOG.debug("%s location is a zip file", location)
download_fn = functools.partial(
repository.unzip, zip_uri=location, is_url=repository.is_repo_url(location), no_input=no_input
)
# Else, treat it as a git/hg/ssh URL and try to clone
elif repository.is_repo_url(location):
LOG.debug("%s location is a source control repository", location)
download_fn = functools.partial(repository.clone, repo_url=location, no_input=no_input)
else:
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)
try:
return _download_and_copy(download_fn, output_dir)
except exceptions.RepositoryNotFound:
# Download failed because the zip or the repository was not found
raise ArbitraryProjectDownloadFailed(msg=BAD_LOCATION_ERROR_MSG)
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_json(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)
)
# Create the platform directory (if it doesn't already exist)
output_path = self.bundle_path(app).parent
output_path.mkdir(parents=True, exist_ok=True)
# Unroll the template
self.cookiecutter(
str(cached_template),
no_input=True,
output_dir=str(output_path),
checkout=self.python_version_tag,
extra_context=extra_context
)
except subprocess.CalledProcessError:
# Computer is offline
# status code == 128 - certificate validation error.
raise NetworkFailure("clone template repository")
except cookiecutter_exceptions.RepositoryNotFound:
# Either the template path is invalid,
# or it isn't a cookiecutter template (i.e., no cookiecutter.json)
raise InvalidTemplateRepository(app.template)
except cookiecutter_exceptions.RepositoryCloneFailed:
# Branch does not exist for python version
raise TemplateUnsupportedVersion(self.python_version_tag)
)
try:
# Unroll the new app template
self.cookiecutter(
str(cached_template),
no_input=True,
output_dir=str(self.base_path),
checkout="v0.3",
extra_context=context
)
except subprocess.CalledProcessError:
# Computer is offline
# status code == 128 - certificate validation error.
raise NetworkFailure("clone template repository")
except cookiecutter_exceptions.RepositoryNotFound:
# Either the template path is invalid,
# or it isn't a cookiecutter template (i.e., no cookiecutter.json)
raise InvalidTemplateRepository(template)
print("""
Application '{formal_name}' has been generated. To run your application, type:
cd {app_name}
briefcase dev
""".format(**context))
try:
subprocess.check_output(
[repo_type, 'clone', repo_url],
cwd=clone_to_dir,
stderr=subprocess.STDOUT,
)
if checkout is not None:
subprocess.check_output(
[repo_type, 'checkout', checkout],
cwd=repo_dir,
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as clone_error:
output = clone_error.output.decode('utf-8')
if 'not found' in output.lower():
raise RepositoryNotFound(
'The repository {} could not be found, '
'have you made a typo?'.format(repo_url)
)
if any(error in output for error in BRANCH_ERRORS):
raise RepositoryCloneFailed(
'The {} branch of repository {} could not found, '
'have you made a typo?'.format(checkout, repo_url)
)
raise
return repo_dir