Skip to content
Myr

Add comments about the test cases

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

Refs: https://github.com/pallets/click/pull/3030/files#r2289213427 https://github.com/pallets/click/pull/3030/files#r2289216839 https://github.com/pallets/click/pull/3030/files#r2289227725

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 11bfb2c · Browse files · 2 files+164 added−31 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/files#r2289213427
  • https: //github.com/pallets/click/pull/3030/files#r2289216839
  • https: //github.com/pallets/click/pull/3030/files#r2289227725

Diff stats

2 files+164 added−31 removed

  • tests:+164 added−31 removed

2 files+164 added−31 removed

modified:tests/test_basic.py+38 added-9 removed
275 unmodified lines
276 assert result.output == expect277278279@pytest.mark.parametrize(280 ("default", "args", "expect"),280 ("default", "args", "expected"),281 # These test cases are similar to the ones in282 # tests/test_options.py::test_default_dual_option_callback, so keep them in sync.283 (282 # See: https://github.com/pallets/click/issues/3024#issuecomment-3146199461284 # Each option is returning its own flag_value, whatever the default is.285 (True, ["--upper"], "upper"),286 (True, ["--lower"], "lower"),285 (True, [], "True"),287 (False, ["--upper"], "upper"),288 (False, ["--lower"], "lower"),289 (None, ["--upper"], "upper"),290 (None, ["--lower"], "lower"),291 (UNSET, ["--upper"], "upper"),292 (UNSET, ["--lower"], "lower"),293 # Check that the last option wins when both are specified.294 (True, ["--upper", "--lower"], "lower"),295 (True, ["--lower", "--upper"], "upper"),296 # Check that the default is returned as-is when no option is specified.297 ("upper", [], "upper"),298 ("lower", [], "lower"),299 ("uPPer", [], "uPPer"),300 ("lOwEr", [], "lOwEr"),301 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),302 (None, [], None),303 # Default is normalized to None if it is UNSET.304 (UNSET, [], None),305 # Non-string defaults are process as strings by the default Parameter's type.306 (True, [], "True"),307 (False, [], "False"),308 (42, [], "42"),309 (12.3, [], "12.3"),310 ),311)289def test_flag_value_dual_options(runner, default, args, expect):312def test_flag_value_dual_options(runner, default, args, expected):313 """Check how default is processed when options compete for the same variable name.314315 Covers the regression reported in316 https://github.com/pallets/click/issues/3024#issuecomment-3146199461317 """318319 @click.command()291 @click.option("--upper", "transformation", flag_value="upper", default=default)292 @click.option("--lower", "transformation", flag_value="lower")293 def cli(transformation):294 click.echo(repr(transformation), nl=False)320 @click.option("--upper", "case", flag_value="upper", default=default)321 @click.option("--lower", "case", flag_value="lower")322 def cli(case):323 click.echo(repr(case), nl=False)324325 result = runner.invoke(cli, args)297 assert result.output == repr(expect)326 assert result.output == repr(expected)327328329def test_file_option(runner):330 @click.command()
403 unmodified lines
modified:tests/test_options.py+126 added-22 removed
1import enum2import os3import re4import sys5import tempfile67if sys.version_info < (3, 11):8 enum.StrEnum = enum.Enum # type: ignore[assignment]9
1354 unmodified lines
136413651366@pytest.mark.parametrize(1367 ("default", "args", "expected"),1367 [1368 (None, [], "A real None"),1369 (UNSET, [], "A real UNSET"),1370 ("random", [], "random"),1368 # These test cases are similar to the ones in1369 # tests/test_basic.py::test_flag_value_dual_options, so keep them in sync.1370 (1371 # Each option is returning its own flag_value, whatever the default is.1372 (True, ["--js"], "js"),1373 (True, ["--xml"], "xml"),1374 (False, ["--js"], "js"),1375 (False, ["--xml"], "xml"),1376 (None, ["--js"], "js"),1377 (None, ["--xml"], "xml"),1373 ],1378 (UNSET, ["--js"], "js"),1379 (UNSET, ["--xml"], "xml"),1380 # Check that the last option wins when both are specified.1381 (True, ["--js", "--xml"], "xml"),1382 (True, ["--xml", "--js"], "js"),1383 # Check that the default is returned as-is when no option is specified.1384 ("js", [], "js"),1385 ("xml", [], "xml"),1386 ("jS", [], "jS"),1387 ("xMl", [], "xMl"),1388 (" ᕕ( ᐛ )ᕗ ", [], " ᕕ( ᐛ )ᕗ "),1389 (None, [], None),1390 (UNSET, [], UNSET),1391 # Non-string defaults are process as strings by the default Parameter's type.1392 (True, [], "True"),1393 (False, [], "False"),1394 (42, [], "42"),1395 (12.3, [], "12.3"),1396 ),1397)1375def test_flag_value_dual_callback(runner, default, args, expected):1376 """1398def test_default_dual_option_callback(runner, default, args, expected):1399 """Check how default is processed by the callback when options compete for the same1400 variable name.14011402 Reproduction of the issue reported in1403 https://github.com/pallets/click/pull/3030#discussion_r22715718191404 """14051406 def _my_func(ctx, param, value):1382 if value is None:1383 return "A real None"1384 if value is UNSET:1385 return "A real UNSET"1386 return value1407 # Print the value received by the callback as-is, so we can check for it.1408 return f"Callback value: {value!r}"14091410 @click.command()1411 @click.option("--js", "fmt", flag_value="js", callback=_my_func, default=default)1412 @click.option("--xml", "fmt", flag_value="xml", callback=_my_func)1413 def main(fmt):1392 click.secho(repr(fmt), nl=False)1414 click.secho(fmt, nl=False)14151416 result = runner.invoke(main, args)1395 assert result.output == repr(expected)1417 assert result.output == f"Callback value: {expected!r}"1418 assert result.exit_code == 0141914201421@pytest.mark.parametrize(
287 unmodified lines
1709 with pytest.warns(UserWarning):1710 runner.invoke(cli, [])171117121691def test_custom_type_click_class_flag_value(runner):1692 """A reproduction of1713NO_CONFIG = object()1714"""A sentinel value to indicate no configuration file is provided."""171517161717@pytest.mark.parametrize(1718 ("args", "expected"),1719 (1720 # Option default to None.1721 ([], None),1722 # Config has no default value, and no flag value, so it requires an argument.1723 (1724 ["--config"],1725 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1726 ),1727 (1728 ["--no-config", "--config"],1729 re.compile(re.escape("Error: Option '--config' requires an argument.\n")),1730 ),1731 # Passing --no-config defaults to the sentinel value because of the flag_value,1732 # and then the custom type receives that sentinel and returns a message.1733 (["--no-config"], "No configuration file provided."),1734 # Passing --config with an argument returns the file path.1735 (["--config", "foo.conf"], "foo.conf"),1736 # An argument is not allowed for --no-config, so it raises an error.1737 (1738 ["--no-config", "foo.conf"],1739 re.compile(1740 r"Usage: main \[OPTIONS\]\n"1741 r"Try 'main --help' for help.\n"1742 r"\n"1743 r"Error: Got unexpected extra argument (.+)\n"1744 ),1745 ),1746 # Passing --config with an argument that does not exist raises an error.1747 (1748 ["--config", "random-file.conf"],1749 re.compile(1750 r"Usage: main \[OPTIONS\]\n"1751 r"Try 'main --help' for help.\n"1752 r"\n"1753 r"Error: Invalid value for '-c' / '--config': "1754 r"File 'random-file.conf' does not exist.\n"1755 ),1756 ),1757 (1758 ["--config", "--no-config"],1759 re.compile(1760 r"Usage: main \[OPTIONS\]\n"1761 r"Try 'main --help' for help.\n"1762 r"\n"1763 r"Error: Invalid value for '-c' / '--config': "1764 r"File '--no-config' does not exist.\n"1765 ),1766 ),1767 (1768 ["--config", "--no-config", "foo.conf"],1769 re.compile(1770 r"Usage: main \[OPTIONS\]\n"1771 r"Try 'main --help' for help.\n"1772 r"\n"1773 r"Error: Invalid value for '-c' / '--config': "1774 r"File '--no-config' does not exist.\n"1775 ),1776 ),1777 # --config is passed last and overrides the --no-config option.1778 (["--no-config", "--config", "foo.conf"], "foo.conf"),1779 ),1780)1781def test_dual_options_custom_type_sentinel_flag_value(runner, args, expected):1782 """Check that an object-based sentinel, used as a flag value, is returned as-is1783 to a custom type that is shared by two options, competing for the same variable1784 name.17851786 A reproduction of1787 https://github.com/pallets/click/issues/3024#issuecomment-31465113561788 """17891696 NO_CONFIG = object()16971790 class ConfigParamType(click.ParamType):1699 name = "config"1791 """A custom type that accepts a file path or a sentinel value."""17921793 def convert(self, value, param, ctx):1794 if value is NO_CONFIG:1703 return value1795 return "No configuration file provided."1796 else:1797 return click.Path(exists=True, dir_okay=False).convert(1798 value, param, ctx1799 )
3 unmodified lines
1803 @click.option("--no-config", "config", flag_value=NO_CONFIG, type=ConfigParamType())1804 def main(config):1805 click.echo(repr(config), nl=False)18061715 result = runner.invoke(main)1716 assert result.output == repr(None)1807 with tempfile.NamedTemporaryFile(mode="w") as named_tempfile:1808 if "foo.conf" in args:1809 named_tempfile.write("Blah blah")1810 named_tempfile.flush()1811 args = [named_tempfile.name if a == "foo.conf" else a for a in args]18121813 result = runner.invoke(main, args)18141815 if isinstance(expected, re.Pattern):1816 assert re.match(expected, result.output)1817 else:1818 assert result.output == repr(1819 named_tempfile.name if expected == "foo.conf" else expected1820 )182118221823class EngineType(enum.Enum):1824 OSS = enum.auto()1825 PRO = enum.auto()1826 MAX = enum.auto()
217 unmodified lines