diff --git a/cookiecutter/generate.py b/cookiecutter/generate.py index a6a43e170..7dbd9867b 100644 --- a/cookiecutter/generate.py +++ b/cookiecutter/generate.py @@ -4,6 +4,7 @@ import logging import os import shutil +import warnings from collections import OrderedDict from binaryornot.check import is_binary @@ -62,6 +63,11 @@ def apply_overwrites_to_context(context, overwrite_context): # see ``cookiecutter.prompt.prompt_choice_for_config`` context_value.remove(overwrite) context_value.insert(0, overwrite) + else: + raise ValueError( + "{} provided for choice variable {}, but the " + "choices are {}.".format(overwrite, variable, context_value) + ) else: # Simply overwrite the value for this variable context[variable] = overwrite @@ -103,7 +109,10 @@ def generate_context( # Overwrite context variable defaults with the default context from the # user's global config, if available if default_context: - apply_overwrites_to_context(obj, default_context) + try: + apply_overwrites_to_context(obj, default_context) + except ValueError as ex: + warnings.warn("Invalid default received: " + str(ex)) if extra_context: apply_overwrites_to_context(obj, extra_context) diff --git a/tests/test_generate_context.py b/tests/test_generate_context.py index 69d014845..2a2bc06dd 100644 --- a/tests/test_generate_context.py +++ b/tests/test_generate_context.py @@ -151,15 +151,42 @@ def test_apply_overwrites_sets_non_list_value(template_context): assert template_context['repo_name'] == 'foobar' -def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite( - template_context, -): +def test_apply_overwrites_does_not_modify_choices_for_invalid_overwrite(): """Verify variables overwrite for list if variable not in list ignored.""" - generate.apply_overwrites_to_context( - context=template_context, overwrite_context={'orientation': 'foobar'} + expected_context = { + 'choices_template': OrderedDict( + [ + ('full_name', 'Raphael Pierzina'), + ('github_username', 'hackebrot'), + ('project_name', 'Kivy Project'), + ('repo_name', '{{cookiecutter.project_name|lower}}'), + ('orientation', ['all', 'landscape', 'portrait']), + ] + ) + } + + generated_context = generate.generate_context( + context_file='tests/test-generate-context/choices_template.json', + default_context={ + 'not_in_template': 'foobar', + 'project_name': 'Kivy Project', + 'orientation': 'foobar', + }, + extra_context={ + 'also_not_in_template': 'foobar2', + 'github_username': 'hackebrot', + }, ) - assert template_context['orientation'] == ['all', 'landscape', 'portrait'] + assert generated_context == expected_context + + +def test_apply_overwrites_invalid_overwrite(template_context): + """Verify variables overwrite for list if variable not in list not ignored.""" + with pytest.raises(ValueError): + generate.apply_overwrites_to_context( + context=template_context, overwrite_context={'orientation': 'foobar'} + ) def test_apply_overwrites_sets_default_for_choice_variable(template_context):