test_cli.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 src.taipy._entrypoint import _entrypoint
  16. from taipy._cli._base_cli import _CLI
  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. expected_help = """{run,manage-versions,create,help} ...
  38. positional arguments:
  39. {run,manage-versions,create,help}
  40. run Run a Taipy application.
  41. manage-versions Taipy version control system.
  42. create Create a new Taipy application.
  43. help Show the Taipy help message.
  44. """
  45. def test_taipy_command_alone_print_help(capsys):
  46. with patch("sys.argv", ["prog"]):
  47. _entrypoint()
  48. out, _ = capsys.readouterr()
  49. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  50. def test_taipy_help_command(capsys):
  51. with patch("sys.argv", ["prog", "help"]):
  52. with pytest.raises(SystemExit):
  53. _entrypoint()
  54. out, _ = capsys.readouterr()
  55. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  56. def test_help_non_existed_command(caplog):
  57. with patch("sys.argv", ["prog", "help", "non_existed_command"]):
  58. with pytest.raises(SystemExit):
  59. _entrypoint()
  60. assert "non_existed_command is not a valid command." in caplog.text
  61. def test_taipy_create_help(capsys):
  62. expected_help = "create [-h] [--template"
  63. with patch("sys.argv", ["prog", "help", "create"]):
  64. with pytest.raises(SystemExit):
  65. _entrypoint()
  66. out, _ = capsys.readouterr()
  67. assert preprocess_stdout(expected_help) in preprocess_stdout(out)