|
9 | 9 | import click._termui_impl |
10 | 10 | from click._compat import WIN |
11 | 11 | from click._termui_impl import Editor |
| 12 | +from click._utils import UNSET |
12 | 13 | from click.exceptions import BadParameter |
13 | 14 | from click.exceptions import MissingParameter |
14 | 15 |
|
@@ -717,6 +718,58 @@ def cmd(arg1): |
717 | 718 | assert "my-default-value" not in result.output |
718 | 719 |
|
719 | 720 |
|
| 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_prompt |
| 751 | +): |
| 752 | + """When show_default is a string, the prompt should display that |
| 753 | + 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"] = default |
| 759 | + |
| 760 | + @click.command() |
| 761 | + @click.option("--arg1", **option_kwargs) |
| 762 | + def cmd(arg1): |
| 763 | + click.echo(arg1) |
| 764 | + |
| 765 | + 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_line |
| 769 | + if not_in_prompt is not None: |
| 770 | + assert not_in_prompt not in prompt_line |
| 771 | + |
| 772 | + |
720 | 773 | REPEAT = object() |
721 | 774 | """Sentinel value to indicate that the prompt is expected to be repeated. |
722 | 775 |
|
|
0 commit comments