test_cli.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. from unittest.mock import patch
  14. import pytest
  15. from taipy._cli._base_cli import _CLI
  16. from taipy._entrypoint import _entrypoint
  17. def preprocess_stdout(stdout):
  18. stdout = stdout.replace("\n", " ").replace("\t", " ")
  19. return re.sub(" +", " ", stdout)
  20. def remove_subparser(name: str):
  21. """Remove a subparser from the _CLI class."""
  22. _CLI._sub_taipyparsers.pop(name, None)
  23. if _CLI._subparser_action:
  24. _CLI._subparser_action._name_parser_map.pop(name, None)
  25. for action in _CLI._subparser_action._choices_actions:
  26. if action.dest == name:
  27. _CLI._subparser_action._choices_actions.remove(action)
  28. @pytest.fixture(autouse=True, scope="function")
  29. def clean_argparser():
  30. _CLI._parser = argparse.ArgumentParser(conflict_handler="resolve")
  31. _CLI._subparser_action = None
  32. _CLI._arg_groups = {}
  33. subcommands = list(_CLI._sub_taipyparsers.keys())
  34. for subcommand in subcommands:
  35. remove_subparser(subcommand)
  36. yield
  37. _CLI._subparser_action = None
  38. _CLI._sub_taipyparsers = {}
  39. expected_help = """{run,manage-versions,create,migrate,help} ...
  40. positional arguments:
  41. {run,manage-versions,create,migrate,help}
  42. run Run a Taipy application.
  43. manage-versions Taipy version control system.
  44. create Create a new Taipy application.
  45. migrate Migrate entities created from old taipy versions to be compatible with the current taipy
  46. version. The entity migration should be performed only after updating taipy code to the current version.
  47. help Show the Taipy help message.
  48. """
  49. def test_taipy_command_alone_print_help(capsys):
  50. with patch("sys.argv", ["prog"]):
  51. _entrypoint()
  52. out, _ = capsys.readouterr()
  53. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  54. def test_taipy_help_command(capsys):
  55. with patch("sys.argv", ["prog", "help"]):
  56. with pytest.raises(SystemExit):
  57. _entrypoint()
  58. out, _ = capsys.readouterr()
  59. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  60. def test_help_non_existed_command(caplog):
  61. with patch("sys.argv", ["prog", "help", "non_existed_command"]):
  62. with pytest.raises(SystemExit):
  63. _entrypoint()
  64. assert "non_existed_command is not a valid command." in caplog.text
  65. def test_taipy_create_help(capsys):
  66. expected_help = "create [-h] [--template"
  67. with patch("sys.argv", ["prog", "help", "create"]):
  68. with pytest.raises(SystemExit):
  69. _entrypoint()
  70. out, _ = capsys.readouterr()
  71. assert preprocess_stdout(expected_help) in preprocess_stdout(out)