Skip to content

Commit

Permalink
Alert user if choice is invalid (#1496)
Browse files Browse the repository at this point in the history
* If an invalid value is provided for a choice variable (such as from a config file), alert the user instead of silently ignoring it.

* black

* Issue a warning instead of a ValueError.

* assert generated_context == expected_context

* test_apply_overwrites_invalid_overwrite

* flake8

* Verify variables overwrite for list if variable not in list not ignored.
  • Loading branch information
dHannasch committed Apr 27, 2021
1 parent 00f0f13 commit b0c5e3f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
11 changes: 10 additions & 1 deletion cookiecutter/generate.py
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import shutil
import warnings
from collections import OrderedDict

from binaryornot.check import is_binary
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
39 changes: 33 additions & 6 deletions tests/test_generate_context.py
Expand Up @@ -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):
Expand Down

0 comments on commit b0c5e3f

Please sign in to comment.