1
0

test_default_template.py 6.9 KB

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