Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def env():
environment = StrictEnvironment()
environment.loader = FileSystemLoader('.')
return environment
def test_should_read_userchoice(self, mocker, choices, context):
read_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
read_choice.return_value = 'all'
expected_choice = 'all'
actual_choice = prompt.prompt_choice_for_config(
context,
environment.StrictEnvironment(),
'orientation',
choices,
False # Ask the user for input
)
read_choice.assert_called_once_with('orientation', choices)
assert expected_choice == actual_choice
def test_should_return_first_option_if_no_input(
self, mocker, choices, context):
read_choice = mocker.patch('cookiecutter.prompt.read_user_choice')
expected_choice = choices[0]
actual_choice = prompt.prompt_choice_for_config(
context,
environment.StrictEnvironment(),
'orientation',
choices,
True # Suppress user input
)
assert not read_choice.called
assert expected_choice == actual_choice
def test_env_should_come_with_jinja2_time_extension():
env = StrictEnvironment(keep_trailing_newline=True)
assert 'jinja2_time.jinja2_time.TimeExtension' in env.extensions
def test_env_should_raise_for_unknown_extension():
context = {
'cookiecutter': {
'_extensions': ['foobar']
}
}
with pytest.raises(UnknownExtension) as err:
StrictEnvironment(context=context, keep_trailing_newline=True)
assert 'Unable to load extension: ' in str(err.value)
def render_context(context, output_folder_path):
cookiecutter_dict = OrderedDict()
# inject the output folder path at the beginning, so all variables can refer to it
cookiecutter_dict['_output_folder_path'] = output_folder_path;
env = StrictEnvironment(context=context)
for key, raw in iteritems(context['cookiecutter']):
if key.startswith('_'):
# unlike cookiecutter's prompt_for_config, we render internal variables
cookiecutter_dict[key] = render_obj(env, raw, cookiecutter_dict)
continue
try:
if isinstance(raw, list):
# We are dealing with a choice variable
val = prompt_choice_for_config(
cookiecutter_dict, env, key, raw, no_input=True
)
else:
# We are dealing with a regular variable
val = render_variable(env, raw, cookiecutter_dict)
:param script_path: Absolute path to the script to run.
:param cwd: The directory to run the script from.
:param context: Cookiecutter project template context.
"""
_, extension = os.path.splitext(script_path)
with io.open(script_path, 'r', encoding='utf-8') as file:
contents = file.read()
with tempfile.NamedTemporaryFile(
delete=False,
mode='wb',
suffix=extension
) as temp:
env = StrictEnvironment(
context=context,
keep_trailing_newline=True,
)
template = env.from_string(contents)
output = template.render(**context)
temp.write(output.encode('utf-8'))
run_script(temp.name, cwd)
def prompt_for_config(context, no_input=False):
"""
Prompts the user to enter new config, using context as a source for the
field names and sample values.
:param no_input: Prompt the user at command line for manual configuration?
"""
cookiecutter_dict = OrderedDict([])
env = StrictEnvironment(context=context)
# First pass: Handle simple and raw variables, plus choices.
# These must be done first because the dictionaries keys and
# values might refer to them.
for key, raw in iteritems(context[u'cookiecutter']):
if key.startswith(u'_'):
cookiecutter_dict[key] = raw
continue
try:
if isinstance(raw, list):
if isinstance(raw[0], dict):
val = _prompt_choice_and_subitems(
cookiecutter_dict, env, key, raw, no_input
)
cookiecutter_dict[key] = val