test_argparser.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 re
  12. import sys
  13. from taipy.common._cli._base_cli._taipy_parser import _TaipyParser
  14. if sys.version_info >= (3, 10):
  15. argparse_options_str = "options:"
  16. else:
  17. argparse_options_str = "optional arguments:"
  18. def preprocess_stdout(stdout):
  19. stdout = stdout.replace("\n", " ").replace("\t", " ")
  20. return re.sub(" +", " ", stdout)
  21. def test_subparser(capfd):
  22. subcommand_1 = _TaipyParser._add_subparser("subcommand_1", help="subcommand_1 help")
  23. subcommand_1.add_argument("--foo", "-f", help="foo help")
  24. subcommand_1.add_argument("--bar", "-b", help="bar help")
  25. subcommand_2 = _TaipyParser._add_subparser("subcommand_2", help="subcommand_2 help")
  26. subcommand_2.add_argument("--doo", "-d", help="doo help")
  27. subcommand_2.add_argument("--baz", "-z", help="baz help")
  28. expected_subcommand_1_help_message = f"""subcommand_1 [-h] [--foo FOO] [--bar BAR]
  29. {argparse_options_str}
  30. -h, --help show this help message and exit
  31. --foo FOO, -f FOO foo help
  32. --bar BAR, -b BAR bar help
  33. """
  34. subcommand_1.print_help()
  35. stdout, _ = capfd.readouterr()
  36. assert preprocess_stdout(expected_subcommand_1_help_message) in preprocess_stdout(stdout)
  37. expected_subcommand_2_help_message = f"""subcommand_2 [-h] [--doo DOO] [--baz BAZ]
  38. {argparse_options_str}
  39. -h, --help show this help message and exit
  40. --doo DOO, -d DOO doo help
  41. --baz BAZ, -z BAZ baz help
  42. """
  43. subcommand_2.print_help()
  44. stdout, _ = capfd.readouterr()
  45. assert preprocess_stdout(expected_subcommand_2_help_message) in preprocess_stdout(stdout)
  46. def test_duplicate_subcommand():
  47. subcommand_1 = _TaipyParser._add_subparser("subcommand_1", help="subcommand_1 help")
  48. subcommand_1.add_argument("--foo", "-f", help="foo help")
  49. subcommand_2 = _TaipyParser._add_subparser("subcommand_1", help="subcommand_2 help")
  50. subcommand_2.add_argument("--bar", "-b", help="bar help")
  51. # The title of subcommand_2 is duplicated with subcommand_1, and therefore
  52. # there will be no new subcommand created
  53. assert len(_TaipyParser._sub_taipyparsers) == 1
  54. def test_groupparser(capfd):
  55. group_1 = _TaipyParser._add_groupparser("group_1", "group_1 desc")
  56. group_1.add_argument("--foo", "-f", help="foo help")
  57. group_1.add_argument("--bar", "-b", help="bar help")
  58. group_2 = _TaipyParser._add_groupparser("group_2", "group_2 desc")
  59. group_2.add_argument("--doo", "-d", help="doo help")
  60. group_2.add_argument("--baz", "-z", help="baz help")
  61. expected_help_message = """
  62. group_1:
  63. group_1 desc
  64. --foo FOO, -f FOO foo help
  65. --bar BAR, -b BAR bar help
  66. group_2:
  67. group_2 desc
  68. --doo DOO, -d DOO doo help
  69. --baz BAZ, -z BAZ baz help
  70. """.strip()
  71. _TaipyParser._parser.print_help()
  72. stdout, _ = capfd.readouterr()
  73. assert expected_help_message in stdout
  74. def test_duplicate_group():
  75. group_1 = _TaipyParser._add_groupparser("group_1", "group_1 desc")
  76. group_1.add_argument("--foo", "-f", help="foo help")
  77. group_2 = _TaipyParser._add_groupparser("group_1", "group_2 desc")
  78. group_2.add_argument("--bar", "-b", help="bar help")
  79. # The title of group_2 is duplicated with group_1, and therefore
  80. # there will be no new group created
  81. assert len(_TaipyParser._arg_groups) == 1