Forráskód Böngészése

bug/ move template test files to general test folder (#583)

* move template test files to general test folder

* fix: remove automatic taipy version requirement hook

* fix: use PYTHONPATH in subprocess to access taipy

* handle windows latin-1

* modify env only for windows

---------

Co-authored-by: Toan Quach <shiro@Shiros-MacBook-Pro.local>
Co-authored-by: trgiangdo <dtr.giang.1299@gmail.com>
Co-authored-by: Jean-Robin <jeanrobin.medori@avaiga.com>
Co-authored-by: Fred Lefévère-Laoide <90181748+FredLL-Avaiga@users.noreply.github.com>
Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Toan Quach 1 éve
szülő
commit
170a14b149

+ 0 - 5
taipy/templates/default/hooks/post_gen_project.py

@@ -12,8 +12,6 @@
 import os
 import os
 import shutil
 import shutil
 
 
-import taipy
-
 
 
 def handle_services(use_rest, use_core):
 def handle_services(use_rest, use_core):
     if use_core or use_rest:
     if use_core or use_rest:
@@ -150,9 +148,6 @@ def generate_main_file():
         app_main_file.write(main_lines)
         app_main_file.write(main_lines)
 
 
 
 
-with open(os.path.join(os.getcwd(), "requirements.txt"), "a") as requirement_file:
-    requirement_file.write(f"taipy=={taipy.version._get_version()}\n")
-
 use_core = "{{ cookiecutter.__core }}".upper()
 use_core = "{{ cookiecutter.__core }}".upper()
 use_rest = "{{ cookiecutter.__rest }}".upper()
 use_rest = "{{ cookiecutter.__rest }}".upper()
 handle_services(use_rest in ["YES", "Y"], use_core in ["YES", "Y"])
 handle_services(use_rest in ["YES", "Y"], use_core in ["YES", "Y"])

+ 1 - 0
taipy/templates/default/{{cookiecutter.__root_folder_name}}/requirements.txt

@@ -0,0 +1 @@
+taipy

+ 0 - 6
taipy/templates/scenario-management/hooks/post_gen_project.py

@@ -11,12 +11,6 @@
 
 
 import os
 import os
 
 
-import taipy
-
-# Add taipy version to requirements.txt
-with open(os.path.join(os.getcwd(), "requirements.txt"), "a") as requirement_file:
-    requirement_file.write(f"taipy=={taipy.version._get_version()}\n")
-
 # Use TOML config file or not
 # Use TOML config file or not
 use_toml_config = "{{ cookiecutter.__use_toml_config }}".upper()
 use_toml_config = "{{ cookiecutter.__use_toml_config }}".upper()
 if use_toml_config == "YES" or use_toml_config == "Y":
 if use_toml_config == "YES" or use_toml_config == "Y":

+ 0 - 0
taipy/templates/tests/__init__.py → tests/templates/__init__.py


+ 18 - 31
taipy/templates/tests/test_default_template.py → tests/templates/test_default_template.py

@@ -29,17 +29,12 @@ def test_default_answer(tmpdir):
     assert (
     assert (
         os.listdir(os.path.join(tmpdir, "taipy_application")).sort() == ["requirements.txt", "main.py", "images"].sort()
         os.listdir(os.path.join(tmpdir, "taipy_application")).sort() == ["requirements.txt", "main.py", "images"].sort()
     )
     )
-    with open(os.path.join(tmpdir, "taipy_application", "requirements.txt")) as requirements_file:
-        # Assert post_gen_project hook is successful
-        assert "taipy==" in requirements_file.read()
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "taipy_application"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
+    assert "[Taipy][INFO]  * Server starting on" in stdout
 
 
 
 
 def test_main_file_with_and_without_extension(tmpdir):
 def test_main_file_with_and_without_extension(tmpdir):
@@ -85,14 +80,12 @@ def test_with_core_service(tmpdir):
     with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
     with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
         assert "core = Core()" in main_file.read()
         assert "core = Core()" in main_file.read()
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "taipy_application"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
-    assert "[Taipy][INFO] Development mode: " in str(stdout, "utf-8")
+    assert "[Taipy][INFO]  * Server starting on" in stdout
+    assert "[Taipy][INFO] Development mode: " in stdout
 
 
 
 
 def test_with_rest_service(tmpdir):
 def test_with_rest_service(tmpdir):
@@ -112,14 +105,12 @@ def test_with_rest_service(tmpdir):
     with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
     with open(os.path.join(tmpdir, "taipy_application", "main.py")) as main_file:
         assert "rest = Rest()" in main_file.read()
         assert "rest = Rest()" in main_file.read()
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "taipy_application"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
-    assert "[Taipy][INFO] Development mode: " in str(stdout, "utf-8")
+    assert "[Taipy][INFO]  * Server starting on" in stdout
+    assert "[Taipy][INFO] Development mode: " in stdout
 
 
 
 
 def test_with_both_core_rest_services(tmpdir):
 def test_with_both_core_rest_services(tmpdir):
@@ -141,14 +132,12 @@ def test_with_both_core_rest_services(tmpdir):
         assert "rest = Rest()" in main_file.read()
         assert "rest = Rest()" in main_file.read()
         assert "core = Core()" not in main_file.read()
         assert "core = Core()" not in main_file.read()
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "taipy_application"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "taipy_application"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
-    assert "[Taipy][INFO] Development mode: " in str(stdout, "utf-8")
+    assert "[Taipy][INFO]  * Server starting on" in stdout
+    assert "[Taipy][INFO] Development mode: " in stdout
 
 
 
 
 def test_multipage_gui_template(tmpdir):
 def test_multipage_gui_template(tmpdir):
@@ -170,11 +159,9 @@ def test_multipage_gui_template(tmpdir):
         == ["name_1", "name_2", "name_3", "root.md", "root.py", "__init__.py"].sort()
         == ["name_1", "name_2", "name_3", "root.md", "root.py", "__init__.py"].sort()
     )
     )
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "foo_app"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "foo_app"), "main.py")
+    assert "[Taipy][INFO]  * Server starting on" in stdout
 
 
 
 
 def test_multipage_gui_template_with_invalid_page_name(tmpdir, capfd):
 def test_multipage_gui_template_with_invalid_page_name(tmpdir, capfd):

+ 7 - 17
taipy/templates/tests/test_scenario_mgt_template.py → tests/templates/test_scenario_mgt_template.py

@@ -35,9 +35,6 @@ def test_scenario_management_with_toml_config(tmpdir):
         == ["requirements.txt", "main.py", "algos", "config", "pages"].sort()
         == ["requirements.txt", "main.py", "algos", "config", "pages"].sort()
     )
     )
 
 
-    # Assert post_gen_project hook is successful
-    with open(os.path.join(tmpdir, "foo_app", "requirements.txt")) as requirements_file:
-        assert "taipy==" in requirements_file.read()
     assert (
     assert (
         os.listdir(os.path.join(tmpdir, "foo_app", "config")).sort()
         os.listdir(os.path.join(tmpdir, "foo_app", "config")).sort()
         == ["__init__.py", "config.py", "config.toml"].sort()
         == ["__init__.py", "config.py", "config.toml"].sort()
@@ -45,14 +42,12 @@ def test_scenario_management_with_toml_config(tmpdir):
     with open(os.path.join(tmpdir, "foo_app", "config", "config.py")) as config_file:
     with open(os.path.join(tmpdir, "foo_app", "config", "config.py")) as config_file:
         assert 'Config.load("config/config.toml")' in config_file.read()
         assert 'Config.load("config/config.toml")' in config_file.read()
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "foo_app"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "foo_app"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO] Configuration 'config/config.toml' successfully loaded." in str(stdout, "utf-8")
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
+    assert "[Taipy][INFO] Configuration 'config/config.toml' successfully loaded." in stdout
+    assert "[Taipy][INFO]  * Server starting on" in stdout
 
 
 
 
 def test_scenario_management_without_toml_config(tmpdir):
 def test_scenario_management_without_toml_config(tmpdir):
@@ -74,19 +69,14 @@ def test_scenario_management_without_toml_config(tmpdir):
         == ["requirements.txt", "main.py", "algos", "config", "pages"].sort()
         == ["requirements.txt", "main.py", "algos", "config", "pages"].sort()
     )
     )
 
 
-    # Assert post_gen_project hook is successful
-    with open(os.path.join(tmpdir, "foo_app", "requirements.txt")) as requirements_file:
-        assert "taipy==" in requirements_file.read()
     assert os.listdir(os.path.join(tmpdir, "foo_app", "config")).sort() == ["__init__.py", "config.py"].sort()
     assert os.listdir(os.path.join(tmpdir, "foo_app", "config")).sort() == ["__init__.py", "config.py"].sort()
     with open(os.path.join(tmpdir, "foo_app", "config", "config.py")) as config_file:
     with open(os.path.join(tmpdir, "foo_app", "config", "config.py")) as config_file:
         config_content = config_file.read()
         config_content = config_file.read()
         assert 'Config.load("config/config.toml")' not in config_content
         assert 'Config.load("config/config.toml")' not in config_content
         assert all([x in config_content for x in ["Config.configure_csv_data_node", "Config.configure_task"]])
         assert all([x in config_content for x in ["Config.configure_csv_data_node", "Config.configure_task"]])
 
 
-    oldpwd = os.getcwd()
-    os.chdir(os.path.join(tmpdir, "foo_app"))
-    stdout = _run_template("main.py")
-    os.chdir(oldpwd)
+    taipy_path = os.getcwd()
+    stdout = _run_template(taipy_path, os.path.join(tmpdir, "foo_app"), "main.py")
 
 
     # Assert the message when the application is run successfully is in the stdout
     # Assert the message when the application is run successfully is in the stdout
-    assert "[Taipy][INFO]  * Server starting on" in str(stdout, "utf-8")
+    assert "[Taipy][INFO]  * Server starting on" in stdout

+ 19 - 4
taipy/templates/tests/utils.py → tests/templates/utils.py

@@ -9,13 +9,28 @@
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # specific language governing permissions and limitations under the License.
 # specific language governing permissions and limitations under the License.
 
 
+import os
+import platform
 import subprocess
 import subprocess
 import sys
 import sys
 
 
 
 
-def _run_template(main_path, time_out=30):
+def _bytes_to_str(b: bytes):
+    return str(b, "latin-1" if platform.system() == "Windows" else "utf-8")
+
+
+def _run_template(taipy_path, cwd, main_path, time_out=30):
     """Run the templates on a subprocess and get stdout after timeout"""
     """Run the templates on a subprocess and get stdout after timeout"""
-    with subprocess.Popen([sys.executable, main_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
+    env = dict(PYTHONPATH=taipy_path)
+    if platform.system() == "Windows":
+        env.update(os.environ)
+    with subprocess.Popen(
+        [sys.executable, main_path],
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        cwd=cwd,
+        env=env,
+    ) as proc:
         try:
         try:
             stdout, stderr = proc.communicate(timeout=time_out)
             stdout, stderr = proc.communicate(timeout=time_out)
         except subprocess.TimeoutExpired:
         except subprocess.TimeoutExpired:
@@ -23,7 +38,7 @@ def _run_template(main_path, time_out=30):
             stdout, stderr = proc.communicate()
             stdout, stderr = proc.communicate()
 
 
     # Print the error if there is any (for debugging)
     # Print the error if there is any (for debugging)
-    if stderr := str(stderr, "utf-8"):
+    if stderr := _bytes_to_str(stderr):
         print(stderr)
         print(stderr)
 
 
-    return stdout
+    return _bytes_to_str(stdout)