test_help_cli.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. from unittest.mock import patch
  13. import pytest
  14. from taipy._entrypoint import _entrypoint
  15. def preprocess_stdout(stdout):
  16. stdout = stdout.replace("\n", " ").replace("\t", " ")
  17. return re.sub(" +", " ", stdout)
  18. expected_help = """{run,manage-versions,create,migrate,help} ...
  19. positional arguments:
  20. {run,manage-versions,create,migrate,help}
  21. run Run a Taipy application.
  22. manage-versions Taipy version control system.
  23. create Create a new Taipy application using pre-defined templates.
  24. migrate Migrate entities created from old taipy versions to be compatible with the current taipy
  25. version. The entity migration should be performed only after updating taipy code to the current version.
  26. help Show the Taipy help message.
  27. """
  28. def test_taipy_command_alone_print_help(capsys):
  29. with patch("sys.argv", ["prog"]):
  30. _entrypoint()
  31. out, _ = capsys.readouterr()
  32. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  33. def test_taipy_help_command(capsys):
  34. with patch("sys.argv", ["prog", "help"]):
  35. with pytest.raises(SystemExit):
  36. _entrypoint()
  37. out, _ = capsys.readouterr()
  38. assert preprocess_stdout(expected_help) in preprocess_stdout(out)
  39. def test_help_non_existed_command(caplog):
  40. with patch("sys.argv", ["prog", "help", "non_existed_command"]):
  41. with pytest.raises(SystemExit):
  42. _entrypoint()
  43. assert "non_existed_command is not a valid command." in caplog.text
  44. def test_taipy_create_help(capsys):
  45. expected_help = "create [-h] [--application"
  46. with patch("sys.argv", ["prog", "help", "create"]):
  47. with pytest.raises(SystemExit):
  48. _entrypoint()
  49. out, _ = capsys.readouterr()
  50. assert preprocess_stdout(expected_help) in preprocess_stdout(out)