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_clone_handles_branch_typo(mocker, clone_dir, error_message):
"""In `clone()`, branch not found errors should raise an
appropriate exception.
"""
mocker.patch(
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
side_effect=[subprocess.CalledProcessError(
-1, 'cmd', output=error_message
)]
)
repository_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin'
with pytest.raises(exceptions.RepositoryCloneFailed) as err:
vcs.clone(
repository_url,
clone_to_dir=clone_dir,
checkout='unknown_branch',
no_input=True
)
assert str(err.value) == (
'The unknown_branch branch of repository '
'{} could not found, have you made a typo?'
).format(repository_url)
"""
mocker.patch(
'cookiecutter.vcs.is_vcs_installed',
autospec=True,
return_value=True
)
mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)
expected_repo_dir = os.path.normpath(os.path.join(clone_dir, repo_name))
branch = 'foobar'
repo_dir = vcs.clone(
repo_url,
checkout=branch,
clone_to_dir=clone_dir,
no_input=True
)
assert repo_dir == expected_repo_dir
mock_subprocess.assert_any_call(
[repo_type, 'clone', repo_url],
cwd=clone_dir,
stderr=subprocess.STDOUT
)
mock_subprocess.assert_any_call(
[repo_type, 'checkout', branch],
cwd=expected_repo_dir,
def test_clone_should_rstrip_trailing_slash_in_repo_url(mocker, clone_dir):
"""In `clone()`, repo URL's trailing slash should be stripped if one is
present.
"""
mocker.patch(
'cookiecutter.vcs.is_vcs_installed',
autospec=True,
return_value=True
)
mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)
vcs.clone(
'https://github.com/foo/bar/',
clone_to_dir=clone_dir,
no_input=True
)
mock_subprocess.assert_called_once_with(
['git', 'clone', 'https://github.com/foo/bar'],
cwd=clone_dir,
stderr=subprocess.STDOUT
)
autospec=True
)
mock_subprocess = mocker.patch(
'cookiecutter.vcs.subprocess.check_output',
autospec=True,
)
clone_to_dir = tmpdir.mkdir('clone')
# Create repo_dir to trigger prompt_and_delete
clone_to_dir.mkdir('cookiecutter-pytest-plugin')
repo_url = 'https://github.com/pytest-dev/cookiecutter-pytest-plugin.git'
with pytest.raises(SystemExit):
vcs.clone(repo_url, clone_to_dir=str(clone_to_dir))
assert not mock_subprocess.called
def test_git_clone_overwrite_with_no_prompt():
repo_dir = vcs.clone(
'https://github.com/audreyr/cookiecutter-pypackage.git',
no_input=True
)
assert repo_dir == 'cookiecutter-pypackage'
assert os.path.isfile('cookiecutter-pypackage/README.rst')
def test_hg_clone_cancel(monkeypatch):
monkeypatch.setattr(
'cookiecutter.vcs.read_user_yes_no',
lambda question, default: False
)
with pytest.raises(SystemExit):
vcs.clone('https://bitbucket.org/pokoli/cookiecutter-trytonmodule')
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)
or a URL to a git repository.
: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 extra_context: A dictionary of context that overrides default
and user configuration.
"""
# Get user config from ~/.cookiecutterrc or equivalent
# If no config file, sensible defaults from config.DEFAULT_CONFIG are used
config_dict = get_user_config()
template = expand_abbreviations(template, config_dict)
# TODO: find a better way to tell if it's a repo URL
if 'git@' in template or 'https://' in template:
repo_dir = clone(
repo_url=template,
checkout=checkout,
clone_to_dir=config_dict['cookiecutters_dir'],
no_input=no_input
)
else:
# If it's a local repo, no need to clone or copy to your
# cookiecutters_dir
repo_dir = template
context_file = os.path.join(repo_dir, 'cookiecutter.json')
logging.debug('context_file is {0}'.format(context_file))
context = generate_context(
context_file=context_file,
default_context=config_dict['default_context'],
def cookiecutter(input_dir, checkout=None, no_input=False):
"""
API equivalent to using Cookiecutter at the command line.
:param input_dir: A directory containing a project template dir,
or a URL to git repo.
:param checkout: The branch, tag or commit ID to checkout after clone
"""
# Get user config from ~/.cookiecutterrc or equivalent
# If no config file, sensible defaults from config.DEFAULT_CONFIG are used
config_dict = get_user_config()
# TODO: find a better way to tell if it's a repo URL
if "git@" in input_dir or "https://" in input_dir:
repo_dir = clone(
repo_url=input_dir,
checkout=checkout,
clone_to_dir=config_dict['cookiecutters_dir']
)
else:
# If it's a local repo, no need to clone or copy to your cookiecutters_dir
repo_dir = input_dir
context_file = os.path.join(repo_dir, 'cookiecutter.json')
logging.debug('context_file is {0}'.format(context_file))
context = generate_context(
context_file=context_file,
default_context=config_dict['default_context']
)