Skip to content

Commit ea636eb

Browse files
committed
Test show_default set as string and its display in prompt
1 parent 446c139 commit ea636eb

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Unreleased
1313
``semver.Version``. :issue:`3298` :pr:`3299`
1414
- Fix pager test pollution under parallel execution by using pytest's
1515
``tmp_path`` fixture instead of a shared temporary file path. :pr:`3238`
16-
- Add support for custom ``show_default`` string in prompts. :pr:`460a142`
16+
- Show custom ``show_default`` string in prompts, matching the existing
17+
help text behavior. :issue:`2836` :pr:`2837` :pr:`3165` :pr:`3262` :pr:`3280`
1718

1819
Version 8.3.2
1920
-------------

src/click/termui.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ def prompt(
123123
show_choices is true and text is "Group by" then the
124124
prompt will be "Group by (day, week): ".
125125
126-
.. versionadded:: 8.2
127-
``show_default`` can be a custom string.
126+
.. versionchanged:: 8.3.3
127+
``show_default`` can be a string to show a custom value instead
128+
of the actual default, matching the help text behavior.
128129
129130
.. versionchanged:: 8.3.1
130131
A space is no longer appended to the prompt.

tests/test_termui.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import click._termui_impl
99
from click._compat import WIN
1010
from click._termui_impl import Editor
11+
from click._utils import UNSET
1112
from click.exceptions import BadParameter
1213
from click.exceptions import MissingParameter
1314

@@ -612,17 +613,56 @@ def cmd(arg1):
612613
assert "my-default-value" not in result.output
613614

614615

615-
def test_string_show_default_shows_custom_string_in_prompt(runner):
616+
@pytest.mark.parametrize(
617+
("show_default", "default", "user_input", "in_prompt", "not_in_prompt"),
618+
[
619+
# Regular string replaces the actual default in the prompt.
620+
("custom", "actual", "\n", "(custom)", "actual"),
621+
# String with spaces.
622+
("custom label", "actual", "\n", "(custom label)", "actual"),
623+
# Unicode characters.
624+
("∞", "0", "\n", "(∞)", None),
625+
# Numeric default: custom string hides the number.
626+
("unlimited", 42, "\n", "(unlimited)", "42"),
627+
# Explicit default=None: custom string still appears, must provide input.
628+
("computed at runtime", None, "value\n", "(computed at runtime)", None),
629+
# No default kwarg at all (internal UNSET sentinel): same as None.
630+
("computed at runtime", UNSET, "value\n", "(computed at runtime)", None),
631+
# Empty string is falsy: suppresses any default display.
632+
("", "actual", "\n", None, "actual"),
633+
],
634+
ids=[
635+
"simple-string",
636+
"string-with-spaces",
637+
"unicode",
638+
"numeric-default",
639+
"default-is-none",
640+
"default-is-unset",
641+
"empty-string-is-falsy",
642+
],
643+
)
644+
def test_string_show_default_in_prompt(
645+
runner, show_default, default, user_input, in_prompt, not_in_prompt
646+
):
647+
"""When show_default is a string, the prompt should display that
648+
string in parentheses instead of the actual default value,
649+
matching the help text behavior. See pallets/click#2836."""
650+
651+
option_kwargs = {"show_default": show_default, "prompt": True}
652+
if default is not UNSET:
653+
option_kwargs["default"] = default
654+
616655
@click.command()
617-
@click.option(
618-
"--arg1", show_default="custom", prompt=True, default="my-default-value"
619-
)
656+
@click.option("--arg1", **option_kwargs)
620657
def cmd(arg1):
621-
pass
622-
623-
result = runner.invoke(cmd, input="my-input", standalone_mode=False)
624-
assert "(custom)" in result.output
625-
assert "my-default-value" not in result.output
658+
click.echo(arg1)
659+
660+
result = runner.invoke(cmd, input=user_input, standalone_mode=False)
661+
prompt_line = result.output.split("\n")[0]
662+
if in_prompt is not None:
663+
assert in_prompt in prompt_line
664+
if not_in_prompt is not None:
665+
assert not_in_prompt not in prompt_line
626666

627667

628668
REPEAT = object()

0 commit comments

Comments
 (0)