test_argparser.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import argparse
  12. import re
  13. import sys
  14. import pytest
  15. from taipy._cli._base_cli import _CLI
  16. if sys.version_info >= (3, 10):
  17. argparse_options_str = "options:"
  18. else:
  19. argparse_options_str = "optional arguments:"
  20. def preprocess_stdout(stdout):
  21. stdout = stdout.replace("\n", " ").replace("\t", " ")
  22. return re.sub(" +", " ", stdout)
  23. def remove_subparser(name: str):
  24. """Remove a subparser from argparse."""
  25. _CLI._sub_taipyparsers.pop(name, None)
  26. if _CLI._subparser_action:
  27. _CLI._subparser_action._name_parser_map.pop(name, None)
  28. for action in _CLI._subparser_action._choices_actions:
  29. if action.dest == name:
  30. _CLI._subparser_action._choices_actions.remove(action)
  31. @pytest.fixture(autouse=True, scope="function")
  32. def clean_argparser():
  33. _CLI._parser = argparse.ArgumentParser(conflict_handler="resolve")
  34. _CLI._arg_groups = {}
  35. subcommands = list(_CLI._sub_taipyparsers.keys())
  36. for subcommand in subcommands:
  37. remove_subparser(subcommand)
  38. yield
  39. _CLI._subparser_action = None
  40. _CLI._sub_taipyparsers = {}
  41. def test_subparser(capfd):
  42. subcommand_1 = _CLI._add_subparser("subcommand_1", help="subcommand_1 help")
  43. subcommand_1.add_argument("--foo", "-f", help="foo help")
  44. subcommand_1.add_argument("--bar", "-b", help="bar help")
  45. subcommand_2 = _CLI._add_subparser("subcommand_2", help="subcommand_2 help")
  46. subcommand_2.add_argument("--doo", "-d", help="doo help")
  47. subcommand_2.add_argument("--baz", "-z", help="baz help")
  48. expected_subcommand_1_help_message = f"""subcommand_1 [-h] [--foo FOO] [--bar BAR]
  49. {argparse_options_str}
  50. -h, --help show this help message and exit
  51. --foo FOO, -f FOO foo help
  52. --bar BAR, -b BAR bar help
  53. """
  54. subcommand_1.print_help()
  55. stdout, _ = capfd.readouterr()
  56. assert preprocess_stdout(expected_subcommand_1_help_message) in preprocess_stdout(stdout)
  57. expected_subcommand_2_help_message = f"""subcommand_2 [-h] [--doo DOO] [--baz BAZ]
  58. {argparse_options_str}
  59. -h, --help show this help message and exit
  60. --doo DOO, -d DOO doo help
  61. --baz BAZ, -z BAZ baz help
  62. """
  63. subcommand_2.print_help()
  64. stdout, _ = capfd.readouterr()
  65. assert preprocess_stdout(expected_subcommand_2_help_message) in preprocess_stdout(stdout)
  66. def test_duplicate_subcommand():
  67. subcommand_1 = _CLI._add_subparser("subcommand_1", help="subcommand_1 help")
  68. subcommand_1.add_argument("--foo", "-f", help="foo help")
  69. subcommand_2 = _CLI._add_subparser("subcommand_1", help="subcommand_2 help")
  70. subcommand_2.add_argument("--bar", "-b", help="bar help")
  71. # The title of subcommand_2 is duplicated with subcommand_1, and therefore
  72. # there will be no new subcommand created
  73. assert len(_CLI._sub_taipyparsers) == 1
  74. def test_groupparser(capfd):
  75. group_1 = _CLI._add_groupparser("group_1", "group_1 desc")
  76. group_1.add_argument("--foo", "-f", help="foo help")
  77. group_1.add_argument("--bar", "-b", help="bar help")
  78. group_2 = _CLI._add_groupparser("group_2", "group_2 desc")
  79. group_2.add_argument("--doo", "-d", help="doo help")
  80. group_2.add_argument("--baz", "-z", help="baz help")
  81. expected_help_message = """
  82. group_1:
  83. group_1 desc
  84. --foo FOO, -f FOO foo help
  85. --bar BAR, -b BAR bar help
  86. group_2:
  87. group_2 desc
  88. --doo DOO, -d DOO doo help
  89. --baz BAZ, -z BAZ baz help
  90. """.strip()
  91. _CLI._parser.print_help()
  92. stdout, _ = capfd.readouterr()
  93. assert expected_help_message in stdout
  94. def test_duplicate_group():
  95. group_1 = _CLI._add_groupparser("group_1", "group_1 desc")
  96. group_1.add_argument("--foo", "-f", help="foo help")
  97. group_2 = _CLI._add_groupparser("group_1", "group_2 desc")
  98. group_2.add_argument("--bar", "-b", help="bar help")
  99. # The title of group_2 is duplicated with group_1, and therefore
  100. # there will be no new group created
  101. assert len(_CLI._arg_groups) == 1