test_argparser.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright 2023 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 src.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. def test_subparser(capfd):
  40. subcommand_1 = _CLI._add_subparser("subcommand_1", help="subcommand_1 help")
  41. subcommand_1.add_argument("--foo", "-f", help="foo help")
  42. subcommand_1.add_argument("--bar", "-b", help="bar help")
  43. subcommand_2 = _CLI._add_subparser("subcommand_2", help="subcommand_2 help")
  44. subcommand_2.add_argument("--doo", "-d", help="doo help")
  45. subcommand_2.add_argument("--baz", "-z", help="baz help")
  46. expected_subcommand_1_help_message = f"""subcommand_1 [-h] [--foo FOO] [--bar BAR]
  47. {argparse_options_str}
  48. -h, --help show this help message and exit
  49. --foo FOO, -f FOO foo help
  50. --bar BAR, -b BAR bar help
  51. """
  52. subcommand_1.print_help()
  53. stdout, _ = capfd.readouterr()
  54. assert preprocess_stdout(expected_subcommand_1_help_message) in preprocess_stdout(stdout)
  55. expected_subcommand_2_help_message = f"""subcommand_2 [-h] [--doo DOO] [--baz BAZ]
  56. {argparse_options_str}
  57. -h, --help show this help message and exit
  58. --doo DOO, -d DOO doo help
  59. --baz BAZ, -z BAZ baz help
  60. """
  61. subcommand_2.print_help()
  62. stdout, _ = capfd.readouterr()
  63. assert preprocess_stdout(expected_subcommand_2_help_message) in preprocess_stdout(stdout)
  64. def test_duplicate_subcommand():
  65. subcommand_1 = _CLI._add_subparser("subcommand_1", help="subcommand_1 help")
  66. subcommand_1.add_argument("--foo", "-f", help="foo help")
  67. subcommand_2 = _CLI._add_subparser("subcommand_1", help="subcommand_2 help")
  68. subcommand_2.add_argument("--bar", "-b", help="bar help")
  69. # The title of subcommand_2 is duplicated with subcommand_1, and therefore
  70. # there will be no new subcommand created
  71. assert len(_CLI._sub_taipyparsers) == 1
  72. def test_groupparser(capfd):
  73. group_1 = _CLI._add_groupparser("group_1", "group_1 desc")
  74. group_1.add_argument("--foo", "-f", help="foo help")
  75. group_1.add_argument("--bar", "-b", help="bar help")
  76. group_2 = _CLI._add_groupparser("group_2", "group_2 desc")
  77. group_2.add_argument("--doo", "-d", help="doo help")
  78. group_2.add_argument("--baz", "-z", help="baz help")
  79. expected_help_message = """
  80. group_1:
  81. group_1 desc
  82. --foo FOO, -f FOO foo help
  83. --bar BAR, -b BAR bar help
  84. group_2:
  85. group_2 desc
  86. --doo DOO, -d DOO doo help
  87. --baz BAZ, -z BAZ baz help
  88. """.strip()
  89. _CLI._parser.print_help()
  90. stdout, _ = capfd.readouterr()
  91. assert expected_help_message in stdout
  92. def test_duplicate_group():
  93. group_1 = _CLI._add_groupparser("group_1", "group_1 desc")
  94. group_1.add_argument("--foo", "-f", help="foo help")
  95. group_2 = _CLI._add_groupparser("group_1", "group_2 desc")
  96. group_2.add_argument("--bar", "-b", help="bar help")
  97. # The title of group_2 is duplicated with group_1, and therefore
  98. # there will be no new group created
  99. assert len(_CLI._arg_groups) == 1