8000 Fix variables with null default not being required (#1919) by limtis0 · Pull Request #1920 · cookiecutter/cookiecutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix variables with null default not being required (#1919) #1920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cookiecutter/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ def read_user_variable(var_name, default_value, prompts=None, prefix=""):
if prompts and var_name in prompts.keys() and prompts[var_name]
else var_name
)
return Prompt.ask(f"{prefix}{question}", default=default_value)

while True:
variable = Prompt.ask(f"{prefix}{question}", default=default_value)
if variable is not None:
break

return variable


class YesNoPrompt(Confirm):
Expand Down
26 changes: 22 additions & 4 deletions tests/test_read_user_variable.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
"""test_read_user_variable."""
import pytest
from cookiecutter.prompt import read_user_variable

VARIABLE = 'project_name'
DEFAULT = 'Kivy Project'


def test_click_invocation(mocker):
@pytest.fixture
def mock_prompt(mocker):
"""Return a mocked version of the 'Prompt.ask' function."""
return mocker.patch('rich.prompt.Prompt.ask')


def test_click_invocation(mock_prompt):
"""Test click function called correctly by cookiecutter.

Test for string type invocation.
"""
prompt = mocker.patch('rich.prompt.Prompt.ask')
prompt.return_value = DEFAULT
mock_prompt.return_value = DEFAULT

assert read_user_variable(VARIABLE, DEFAULT) == DEFAULT

prompt.assert_called_once_with(VARIABLE, default=DEFAULT)
mock_prompt.assert_called_once_with(VARIABLE, default=DEFAULT)


def test_input_loop_with_null_default_value(mock_prompt):
"""Test `Prompt.ask` is run repeatedly until a valid answer is provided.

Test for `default_value` parameter equal to None.
"""
# Simulate user providing None input initially and then a valid input
mock_prompt.side_effect = [None, DEFAULT]

assert read_user_variable(VARIABLE, None) == DEFAULT
assert mock_prompt.call_count == 2
0