Skip to content
Myr

Demonstrate limits of substring matching

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 a09c1c6 · Browse files · 1 file+70 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+70 added−3 removed

  • tests:+70 added−3 removed

1 file+70 added−3 removed

modified:tests/test_termui.py+70 added-3 removed
1006 unmodified lines
1007 self.fail(f"rejected: {value}", param, ctx)1008 return value100910101011class _PasswordLengthType(click.ParamType):1012 """Mirrors the issue's original use case: a password validator1013 that references the user-typed value in its error message without1014 quoting it.1015 """10161017 name = "password"10181019 def convert(self, value, param, ctx):1020 if len(value) < 10:1021 self.fail(f"{value} is too short", param, ctx)1022 return value102310241025class _MixedQuotedAndRawType(click.ParamType):1026 """Custom type that mentions the user input both quoted (built-in1027 pattern) and raw within the same message.1028 """10291030 name = "mixed"10311032 def convert(self, value, param, ctx):1033 self.fail(f"got {value!r} which is the same as {value}", param, ctx)103410351036class _StaticMessageType(click.ParamType):1037 """Custom type whose error message never references the value."""10381039 name = "static"10401041 def convert(self, value, param, ctx):1042 self.fail("Authentication failed for this account", param, ctx)104310441045@pytest.mark.parametrize(1012 ("type", "expected_fragment", "unexpected_fragment"),1046 ("type", "user_input", "expected_fragment", "unexpected_fragment"),1047 [1048 pytest.param(1049 click.INT,1050 "bad",1051 "'***' is not a valid integer",1052 "bad",1053 id="builtin-int-masks-repr-value",1054 ),1055 pytest.param(1056 _CustomTypeNoValue(),1057 "bad",1058 "Password must be at least 4 characters",1059 None,1060 id="custom-no-value-shows-message",1061 ),1062 pytest.param(1063 _CustomTypeWithRawValue(),1064 "bad",1065 "The value you entered was invalid",1066 "bad",1067 id="custom-raw-value-falls-back-to-generic",1068 ),1069 pytest.param(1070 _PasswordLengthType(),1071 "PASSWORD",1072 "'***' is too short",1073 "PASSWORD",1074 id="unquoted-custom-message-should-mask-not-fallback",1075 ),1076 pytest.param(1077 _MixedQuotedAndRawType(),1078 "leakybits",1079 "Error:",1080 "leakybits",1081 id="repr-branch-leaves-raw-occurrence-visible",1082 ),1083 pytest.param(1084 click.IntRange(min=10, max=99),1085 "1",1086 "is not in the range",1087 None,1088 id="intrange-numeric-substring-falls-back-to-generic",1089 ),1090 pytest.param(1091 _StaticMessageType(),1092 "ent",1093 "Authentication failed for this account",1094 None,1095 id="partial-word-match-falls-back-to-generic",1096 ),1097 ],1098)1034def test_hide_input_error_message(runner, type, expected_fragment, unexpected_fragment):1099def test_hide_input_error_message(1100 runner, type, user_input, expected_fragment, unexpected_fragment1101):1102 """https://github.com/pallets/click/issues/2809"""11031104 @click.command()1105 @click.option("--password", prompt=True, hide_input=True, type=type)1106 def cli(password):1107 click.echo(password)11081042 result = runner.invoke(cli, input="bad")1109 result = runner.invoke(cli, input=user_input)1110 assert expected_fragment in result.output1111 if unexpected_fragment is not None:1112 assert unexpected_fragment not in result.output