|
8 | 8 | import click._termui_impl |
9 | 9 | from click._compat import WIN |
10 | 10 | from click._termui_impl import Editor |
| 11 | +from click._utils import UNSET |
11 | 12 | from click.exceptions import BadParameter |
12 | 13 | from click.exceptions import MissingParameter |
13 | 14 |
|
@@ -612,17 +613,56 @@ def cmd(arg1): |
612 | 613 | assert "my-default-value" not in result.output |
613 | 614 |
|
614 | 615 |
|
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 | + |
616 | 655 | @click.command() |
617 | | - @click.option( |
618 | | - "--arg1", show_default="custom", prompt=True, default="my-default-value" |
619 | | - ) |
| 656 | + @click.option("--arg1", **option_kwargs) |
620 | 657 | 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 |
626 | 666 |
|
627 | 667 |
|
628 | 668 | REPEAT = object() |
|
0 commit comments