Skip to content
Myr

Better doc and tests for multiple

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 abaef53 · Browse files · 1 file+143 added−54 removed
This commit

State

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

Trailers

  • Refs: https://github.com/pallets/click/pull/3030#discussion_r2289207325

Diff stats

1 file+143 added−54 removed

  • tests:+143 added−54 removed

1 file+143 added−54 removed

modified:tests/test_options.py+143 added-54 removed
172 unmodified lines
173 assert "Error: Missing option '-m' / '--message'." in result.output174175176@pytest.mark.parametrize(177 ("args", "multiple", "nargs", "default", "expected"),177 ("multiple", "nargs", "default", "expected"),178 [179 # If multiple values are allowed, defaults should be iterable.180 ([], True, 1, [], ()),181 ([], True, 1, (), ()),182 ([], True, 1, tuple(), ()),183 ([], True, 1, set(), ()),184 ([], True, 1, frozenset(), ()),185 ([], True, 1, {}, ()),180 (True, 1, [], ()),181 (True, 1, (), ()),182 (True, 1, tuple(), ()),183 (True, 1, set(), ()),184 (True, 1, frozenset(), ()),185 (True, 1, {}, ()),186 # Special values.187 ([], True, 1, None, ()),188 ([], True, 1, UNSET, ()),187 (True, 1, None, ()),188 (True, 1, UNSET, ()),189 # Number of values are kept as-is in the default.190 ([], True, 1, [1], (1,)),191 ([], True, 1, [1, 2], (1, 2)),192 ([], True, 1, [1, 2, 3], (1, 2, 3)),193 ([], True, 1, [1.1, 2.2], (1.1, 2.2)),194 ([], True, 1, ["1", "2"], ("1", "2")),195 ([], True, 1, [None, None], (None, None)),196 # XXX Why items of the defaults are converted to strings and not left as197 # integers?198 ([], True, 1, {1, 2}, ("1", "2")),199 ([], True, 1, frozenset([1, 2]), ("1", "2")),200 ([], True, 1, {1: "a", 2: "b"}, ("1", "2")),190 (True, 1, [1], (1,)),191 (True, 1, [1, 2], (1, 2)),192 (True, 1, [1, 2, 3], (1, 2, 3)),193 (True, 1, [1.1, 2.2], (1.1, 2.2)),194 (True, 1, ["1", "2"], ("1", "2")),195 (True, 1, [None, None], (None, None)),196 # Contrary to list or tuples, native Python types not supported by Click are197 # not recognized and are converted to the default format: tuple of strings.198 # Refs: https://github.com/pallets/click/issues/3036199 (True, 1, {1, 2}, ("1", "2")),200 (True, 1, frozenset([1, 2]), ("1", "2")),201 (True, 1, {1: "a", 2: "b"}, ("1", "2")),202 # Multiple values with nargs > 1.202 ([], True, 2, [], ()),203 ([], True, 2, (), ()),204 ([], True, 2, tuple(), ()),205 ([], True, 2, set(), ()),206 ([], True, 2, frozenset(), ()),207 ([], True, 2, {}, ()),208 ([], True, 2, None, ()),209 ([], True, 2, UNSET, ()),210 ([], True, 2, [[1, 2]], ((1, 2),)),211 ([], True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),212 ([], True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),213 ([], True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),214 ([], True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),215 ([], True, 2, [[None, None], [None, None]], ((None, None), (None, None))),216 ([], True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),217 ([], True, 2, [[1, 2.2], None], ((1, 2.2), None)),218 #219 ([], False, 2, [1, 2], (1, 2)),220 #203 (True, 2, [], ()),204 (True, 2, (), ()),205 (True, 2, tuple(), ()),206 (True, 2, set(), ()),207 (True, 2, frozenset(), ()),208 (True, 2, {}, ()),209 (True, 2, None, ()),210 (True, 2, UNSET, ()),211 (True, 2, [[1, 2]], ((1, 2),)),212 (True, 2, [[1, 2], [3, 4]], ((1, 2), (3, 4))),213 (True, 2, [[1, 2], [3, 4], [5, 6]], ((1, 2), (3, 4), (5, 6))),214 (True, 2, [[1.1, 2.2], [3.3, 4.4]], ((1.1, 2.2), (3.3, 4.4))),215 (True, 2, [["1", "2"], ["3", "4"]], (("1", "2"), ("3", "4"))),216 (True, 2, [[None, None], [None, None]], ((None, None), (None, None))),217 (True, 2, [[1, 2.2], ["3", None]], ((1, 2.2), (3, None))),218 (True, 2, [[1, 2.2], None], ((1, 2.2), None)),219 # Default of the right length works for non-multiples.220 (False, 2, [1, 2], (1, 2)),221 ],222)223def test_good_defaults_for_multiple(runner, args, multiple, nargs, default, expected):223def test_good_defaults_for_multiple(runner, multiple, nargs, default, expected):224 @click.command()225 @click.option("-a", multiple=multiple, nargs=nargs, default=default)226 def cmd(a):227 click.echo(repr(a), nl=False)228229 result = runner.invoke(cmd, args)229 result = runner.invoke(cmd)230 assert result.output == repr(expected)231232233@pytest.mark.parametrize(234 ("multiple", "nargs", "default", "message"),234 ("multiple", "nargs", "default", "exception", "message"),235 [236 # Non-iterables defaults.237 (True, 1, "Yo", "Error: Invalid value for '-a': Value must be an iterable."),238 (True, 1, "", "Error: Invalid value for '-a': Value must be an iterable."),237 (238 True,239 1,240 "Yo",241 None,242 "Error: Invalid value for '-a': Value must be an iterable.",243 ),244 (245 True,246 1,247 "",248 None,249 "Error: Invalid value for '-a': Value must be an iterable.",250 ),251 (252 True,253 1,254 True,255 None,256 "Error: Invalid value for '-a': Value must be an iterable.",257 ),258 (259 True,260 1,261 False,262 None,263 "Error: Invalid value for '-a': Value must be an iterable.",264 ),251 (True, 1, 12, "Error: Invalid value for '-a': Value must be an iterable."),252 (True, 1, 7.9, "Error: Invalid value for '-a': Value must be an iterable."),265 (266 True,267 1,268 12,269 None,270 "Error: Invalid value for '-a': Value must be an iterable.",271 ),272 (273 True,274 1,275 7.9,276 None,277 "Error: Invalid value for '-a': Value must be an iterable.",278 ),279 #254 (False, 2, 42, "Error: Invalid value for '-a': Value must be an iterable."),280 (281 False,282 2,283 42,284 None,285 "Error: Invalid value for '-a': Value must be an iterable.",286 ),287 (288 True,289 2,290 ["test string which is not a list in the list"],291 None,292 "Error: Invalid value for '-a': Value must be an iterable.",293 ),294 # Multiple options, each with 2 args, but with wrong length.295 (296 True,297 2,298 (1,),299 None,300 "Error: Invalid value for '-a': Value must be an iterable.",301 ),302 (303 True,304 2,305 (1, 2, 3),306 None,307 "Error: Invalid value for '-a': Value must be an iterable.",308 ),309 (310 True,311 2,312 [tuple()],313 ValueError,314 r"'nargs' must be 0 \(or None\) for type <click\.types\.Tuple object at "315 r"0x[0-9A-Fa-f]+>, but it was 2\.",316 ),317 (318 True,319 2,320 [(1,)],321 ValueError,322 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "323 r"0x[0-9A-Fa-f]+>, but it was 2\.",324 ),325 (326 True,327 2,328 [(1, 2, 3)],329 ValueError,330 r"'nargs' must be 3 \(or None\) for type <click\.types\.Tuple object at "331 r"0x[0-9A-Fa-f]+>, but it was 2\.",332 ),333 # A mix of valid and invalid defaults.334 (335 True,336 2,337 [[1, 2.2], []],338 None,339 "Error: Invalid value for '-a': 2 values are required, but 0 were given.",340 ),341 # Default values that are iterable but not of the right length.342 (343 False,344 2,345 [1],346 None,347 "Error: Invalid value for '-a': Takes 2 values but 1 was given.",348 ),349 (350 True,351 2,352 [[1]],353 ValueError,354 r"'nargs' must be 1 \(or None\) for type <click\.types\.Tuple object at "355 r"0x[0-9A-Fa-f]+>, but it was 2\.",356 ),357 ],358)283def test_bad_defaults_for_multiple(runner, multiple, nargs, default, message):284 @click.command()285 @click.option("-a", multiple=multiple, nargs=nargs, default=default)286 def cmd(a):287 click.echo(repr(a))359def test_bad_defaults_for_multiple(360 runner, multiple, nargs, default, exception, message361):362 if exception:363 assert issubclass(exception, Exception)364 else:365 assert exception is None366289 result = runner.invoke(cmd, [])290 assert message in result.stderr367 with (368 pytest.raises(exception, match=re.compile(message))369 if exception370 else nullcontext()371 ):372373 @click.command()374 @click.option("-a", multiple=multiple, nargs=nargs, default=default)375 def cmd(a):376 click.echo(repr(a))377378 result = runner.invoke(cmd)379 assert message in result.stderr380381382@pytest.mark.parametrize("env_key", ["MYPATH", "AUTO_MYPATH"])383def test_empty_envvar(runner, env_key):384 @click.command()385 @click.option("--mypath", type=click.Path(exists=True), envvar="MYPATH")
1821 unmodified lines