test_default_template.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 os
  12. import pytest
  13. from cookiecutter.exceptions import FailedHookException
  14. from cookiecutter.main import cookiecutter
  15. from .utils import _run_template
  16. def test_default_answer(tmpdir):
  17. cookiecutter(
  18. template="taipy/templates/default",
  19. output_dir=str(tmpdir),
  20. no_input=True,
  21. )
  22. assert os.listdir(tmpdir) == ["taipy_application"]
  23. assert sorted(os.listdir(os.path.join(tmpdir, "taipy_application"))) == sorted(["requirements.txt", "main.py"])
  24. taipy_path = os.getcwd()
  25. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  26. # Assert the message when the application is run successfully is in the stdout
  27. assert "[Taipy][INFO] * Server starting on" in stdout
  28. def test_main_file_with_and_without_extension(tmpdir):
  29. cookiecutter(
  30. template="taipy/templates/default",
  31. output_dir=str(tmpdir),
  32. no_input=True,
  33. extra_context={
  34. "Application main Python file": "app.py",
  35. },
  36. )
  37. assert sorted(os.listdir(os.path.join(tmpdir, "taipy_application"))) == sorted(["requirements.txt", "app.py"])
  38. cookiecutter(
  39. template="taipy/templates/default",
  40. output_dir=str(tmpdir),
  41. no_input=True,
  42. extra_context={
  43. "Application root folder name": "foo_app",
  44. "Application main Python file": "app",
  45. },
  46. )
  47. assert sorted(os.listdir(os.path.join(tmpdir, "foo_app"))) == sorted(["requirements.txt", "app.py"])
  48. def test_with_orchestrator_service(tmpdir):
  49. cookiecutter(
  50. template="taipy/templates/default",
  51. output_dir=str(tmpdir),
  52. no_input=True,
  53. extra_context={
  54. "Does the application use scenario management or version management?": "y",
  55. "Does the application use Rest API?": "no",
  56. },
  57. )
  58. assert sorted(os.listdir(os.path.join(tmpdir, "taipy_application"))) == sorted(
  59. ["requirements.txt", "main.py", "configuration", "algorithms"]
  60. )
  61. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  62. assert "orchestrator = Orchestrator()" in main_file.read()
  63. taipy_path = os.getcwd()
  64. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  65. # Assert the message when the application is run successfully is in the stdout
  66. assert "[Taipy][INFO] * Server starting on" in stdout
  67. assert "[Taipy][INFO] Development mode: " in stdout
  68. def test_with_rest_service(tmpdir):
  69. cookiecutter(
  70. template="taipy/templates/default",
  71. output_dir=str(tmpdir),
  72. no_input=True,
  73. extra_context={
  74. "Does the application use scenario management or version management?": "n",
  75. "Does the application use Rest API?": "yes",
  76. },
  77. )
  78. assert sorted(os.listdir(os.path.join(tmpdir, "taipy_application"))) == sorted(["requirements.txt", "main.py"])
  79. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  80. assert "rest = Rest()" in main_file.read()
  81. taipy_path = os.getcwd()
  82. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  83. # Assert the message when the application is run successfully is in the stdout
  84. assert "[Taipy][INFO] * Server starting on" in stdout
  85. assert "[Taipy][INFO] Development mode: " in stdout
  86. def test_with_both_orchestrator_rest_services(tmpdir):
  87. cookiecutter(
  88. template="taipy/templates/default",
  89. output_dir=str(tmpdir),
  90. no_input=True,
  91. extra_context={
  92. "Does the application use scenario management or version management?": "y",
  93. "Does the application use Rest API?": "yes",
  94. },
  95. )
  96. assert sorted(os.listdir(os.path.join(tmpdir, "taipy_application"))) == sorted(
  97. ["requirements.txt", "main.py", "configuration", "algorithms"]
  98. )
  99. with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
  100. assert "rest = Rest()" in main_file.read()
  101. assert "orchestrator = Orchestrator()" not in main_file.read()
  102. taipy_path = os.getcwd()
  103. stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
  104. # Assert the message when the application is run successfully is in the stdout
  105. assert "[Taipy][INFO] * Server starting on" in stdout
  106. assert "[Taipy][INFO] Development mode: " in stdout
  107. def test_multipage_gui_template(tmpdir):
  108. cookiecutter(
  109. template="taipy/templates/default",
  110. output_dir=str(tmpdir),
  111. no_input=True,
  112. extra_context={
  113. "Application root folder name": "foo_app",
  114. "Page names in multi-page application?": "name_1 name_2 name_3",
  115. },
  116. )
  117. assert sorted(os.listdir(os.path.join(tmpdir, "foo_app"))) == sorted(["requirements.txt", "main.py", "pages"])
  118. assert sorted(os.listdir(os.path.join(tmpdir, "foo_app", "pages"))) == sorted(
  119. ["name_1", "name_2", "name_3", "root.md", "root.py", "__init__.py"]
  120. )
  121. taipy_path = os.getcwd()
  122. stdout = _run_template(taipy_path, os.path.join(tmpdir, "foo_app"), "main.py")
  123. assert "[Taipy][INFO] * Server starting on" in stdout
  124. def test_multipage_gui_template_with_invalid_page_name(tmpdir, capfd):
  125. with pytest.raises(FailedHookException):
  126. cookiecutter(
  127. template="taipy/templates/default",
  128. output_dir=str(tmpdir),
  129. no_input=True,
  130. extra_context={
  131. "Application root folder name": "foo_app",
  132. "Page names in multi-page application?": "valid_var_name 1_invalid_var_name",
  133. },
  134. )
  135. _, stderr = capfd.readouterr()
  136. assert 'Page name "1_invalid_var_name" is not a valid Python identifier' in stderr
  137. assert not os.path.exists(os.path.join(tmpdir, "foo_app"))
  138. def test_with_git(tmpdir):
  139. cookiecutter(
  140. template="taipy/templates/default",
  141. output_dir=str(tmpdir),
  142. no_input=True,
  143. extra_context={
  144. "Application root folder name": "foo_app",
  145. "Do you want to initialize a new Git repository?": "y",
  146. },
  147. )
  148. assert os.listdir(tmpdir) == ["foo_app"]
  149. assert sorted(os.listdir(os.path.join(tmpdir, "foo_app"))) == sorted(
  150. ["requirements.txt", "main.py", ".git", ".gitignore"]
  151. )