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_invalid_slug(cookies, context, slug):
"""Invalid slug should failed pre-generation hook."""
context.update({"project_slug": slug})
result = cookies.bake(extra_context=context)
assert result.exit_code != 0
assert isinstance(result.exception, FailedHookException)
def test_empty_hooks():
# OSError.errno=8 is not thrown on Windows when the script is empty
# because it always runs through shell instead of needing a shebang.
with pytest.raises(FailedHookException) as excinfo:
generate.generate_files(
context={
'cookiecutter': {'shellhooks': 'shellhooks'}
},
repo_dir='tests/test-shellhooks-empty/',
overwrite_if_exists=True
)
assert 'shebang' in str(excinfo.value)
def test_no_whitenoise_and_no_cloud_provider(cookies, context):
"""It should not generate project if neither whitenoise or cloud provider are set"""
context.update({"use_whitenoise": "n", "cloud_provider": "None"})
result = cookies.bake(extra_context=context)
assert result.exit_code != 0
assert isinstance(result.exception, FailedHookException)
def test_pre_gen_hook(tmpdir):
context = {
'cookiecutter': {
"repo_dir": "foobar",
"abort_pre_gen": "yes",
"abort_post_gen": "no"
}
}
with pytest.raises(exceptions.FailedHookException):
generate.generate_files(
repo_dir='tests/hooks-abort-render',
context=context,
output_dir=str(tmpdir)
)
assert not tmpdir.join('foobar').isdir()
def test_post_gen_hook(tmpdir):
context = {
'cookiecutter': {
"repo_dir": "foobar",
"abort_pre_gen": "no",
"abort_post_gen": "yes"
}
}
with pytest.raises(exceptions.FailedHookException):
generate.generate_files(
repo_dir='tests/hooks-abort-render',
context=context,
output_dir=str(tmpdir)
)
assert not tmpdir.join('foobar').isdir()
if script_path.endswith('.py'):
script_command = [sys.executable, script_path]
else:
script_command = [script_path]
utils.make_executable(script_path)
try:
proc = subprocess.Popen(
script_command,
shell=run_thru_shell,
cwd=cwd
)
exit_status = proc.wait()
if exit_status != EXIT_SUCCESS:
raise FailedHookException(
'Hook script failed (exit status: {})'.format(exit_status)
)
except OSError as os_error:
if os_error.errno == errno.ENOEXEC:
raise FailedHookException(
'Hook script failed, might be an '
'empty file or missing a shebang'
)
raise FailedHookException(
'Hook script failed (error: {})'.format(os_error)
)
def init_evaluation(challenge_name, kind, dev):
template_dir = Path(__file__).parent / "templates" / "evaluation"
try:
cookiecutter(
template=str(template_dir.absolute()),
no_input=True,
extra_context={
"challenge_name": challenge_name,
"evalutils_name": __name__.split(".")[0],
"evalutils_version": __version__,
"challenge_kind": kind,
"dev_build": 1 if dev else 0,
},
)
click.echo(f"Created project {challenge_name}")
except FailedHookException:
exit(1)
kwargs = {}
if args.get('name'):
kwargs = {
'no_input': True,
'extra_context': {
'project_name': args.get('name'),
'use_postgres': 'n' if args.get('without_postgres') else 'y',
'use_redis': 'y' if args.get('redis') else 'n',
'use_uvloop': 'y' if args.get('uvloop') else 'n',
},
}
try:
result = cookiecutter(template_path, **kwargs)
except (FailedHookException, OutputDirExistsException) as exc:
if isinstance(exc, OutputDirExistsException):
echo(click.style(
'\n\nDirectory with such name already exists!\n',
fg='red',
))
return
folder = Path(result).name
echo(click.style(
'\n\nSuccessfully generated!\n',
fg='bright_green',
))
echo('cd ' + click.style(f'{folder}/', fg='bright_blue'))
echo('make run\n\n')
def _run_hook_from_repo_dir(repo_dir, hook_name, project_dir, context,
delete_project_on_failure):
"""Run hook from repo directory, clean project directory if hook fails.
:param repo_dir: Project template input directory.
:param hook_name: The hook to execute.
:param project_dir: The directory to execute the script from.
:param context: Cookiecutter project context.
:param delete_project_on_failure: Delete the project directory on hook
failure?
"""
with work_in(repo_dir):
try:
run_hook(hook_name, project_dir, context)
except FailedHookException:
if delete_project_on_failure:
rmtree(project_dir)
logger.error(
"Stopping generation because {} hook "
"script didn't exit successfully".format(hook_name)
)
raise