Test `show_default` set as string and its display in prompt
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 607316f · Browse files · 3 files·+54 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+54 added−11 removed
- tests:+48 added−8 removed
- other:+6 added−3 removed
Provenance
- Commit
39413d9- Tree
2eb1ffc- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
607316f
3 files changed+54 added−11 removed
modified:src/click/termui.py+3 added-2 removed
121 unmodified lines122 For example if type is a Choice of either day or week,123 show_choices is true and text is "Group by" then the124 prompt will be "Group by (day, week): ".125 126- .. versionadded:: 8.2127- ``show_default`` can be a custom string.126+ .. versionchanged:: 8.3.3127+ ``show_default`` can be a string to show a custom value instead128+ of the actual default, matching the help text behavior.129 130 .. versionchanged:: 8.3.1131 A space is no longer appended to the prompt.132 759 unmodified linesmodified:tests/test_termui.py+48 added-8 removed
7 unmodified lines8 9 import click._termui_impl10 from click._compat import WIN11 from click._termui_impl import Editor12+from click._utils import UNSET13 from click.exceptions import BadParameter14 from click.exceptions import MissingParameter15 16 700 unmodified lines717 result = runner.invoke(cmd, input="my-input", standalone_mode=False)718 assert "my-default-value" not in result.output719 720 720-def test_string_show_default_shows_custom_string_in_prompt(runner):721+@pytest.mark.parametrize(722+ ("show_default", "default", "user_input", "in_prompt", "not_in_prompt"),723+ [724+ # Regular string replaces the actual default in the prompt.725+ ("custom", "actual", "\n", "(custom)", "actual"),726+ # String with spaces.727+ ("custom label", "actual", "\n", "(custom label)", "actual"),728+ # Unicode characters.729+ ("∞", "0", "\n", "(∞)", None),730+ # Numeric default: custom string hides the number.731+ ("unlimited", 42, "\n", "(unlimited)", "42"),732+ # Explicit default=None: custom string still appears, must provide input.733+ ("computed at runtime", None, "value\n", "(computed at runtime)", None),734+ # No default kwarg at all (internal UNSET sentinel): same as None.735+ ("computed at runtime", UNSET, "value\n", "(computed at runtime)", None),736+ # Empty string is falsy: suppresses any default display.737+ ("", "actual", "\n", None, "actual"),738+ ],739+ ids=[740+ "simple-string",741+ "string-with-spaces",742+ "unicode",743+ "numeric-default",744+ "default-is-none",745+ "default-is-unset",746+ "empty-string-is-falsy",747+ ],748+)749+def test_string_show_default_in_prompt(750+ runner, show_default, default, user_input, in_prompt, not_in_prompt751+):752+ """When show_default is a string, the prompt should display that753+ string in parentheses instead of the actual default value,754+ matching the help text behavior. See pallets/click#2836."""755+756+ option_kwargs = {"show_default": show_default, "prompt": True}757+ if default is not UNSET:758+ option_kwargs["default"] = default759+760 @click.command()722- @click.option(723- "--arg1", show_default="custom", prompt=True, default="my-default-value"724- )761+ @click.option("--arg1", **option_kwargs)762 def cmd(arg1):726- pass763+ click.echo(arg1)764 728- result = runner.invoke(cmd, input="my-input", standalone_mode=False)729- assert "(custom)" in result.output730- assert "my-default-value" not in result.output765+ result = runner.invoke(cmd, input=user_input, standalone_mode=False)766+ prompt_line = result.output.split("\n")[0]767+ if in_prompt is not None:768+ assert in_prompt in prompt_line769+ if not_in_prompt is not None:770+ assert not_in_prompt not in prompt_line771 772 773 REPEAT = object()774 """Sentinel value to indicate that the prompt is expected to be repeated.212 unmodified linesmodified:CHANGES.rst+3 added-1 removed
19 unmodified lines20 - Add optional randomized parallel test execution using ``pytest-randomly`` and21 ``pytest-xdist`` to detect test pollution and race conditions. :pr:`3151`22 - Add contributor documentation for running stress tests, randomized23 parallel tests, and Flask smoke tests. :pr:`3151` :pr:`3177`24-- Add support for custom ``show_default`` string in prompts. :pr:`460a142`24+- Show custom ``show_default`` string in prompts, matching the existing25+ help text behavior. :issue:`2836` :pr:`2837` :pr:`3165` :pr:`3262` :pr:`3280`26+ :pr:`3328``27 28 Version 8.3.229 -------------30 1323 unmodified lines