Skip to content

Commit 9b38d91

Browse files
author
Ying Chen
committed
update
1 parent d4fb8a4 commit 9b38d91

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

src/promptflow/tests/sdk_cli_test/e2etests/test_executable.py

+33-30
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
import requests
88
import platform
99
import sys
10+
import os
11+
import ast
12+
import importlib
1013

1114
from .test_cli import run_pf_command
15+
from promptflow._utils.context_utils import _change_working_dir
1216

1317
FLOWS_DIR = "./tests/test_configs/flows"
1418
RUNS_DIR = "./tests/test_configs/runs"
@@ -20,10 +24,6 @@
2024
@pytest.mark.cli_test
2125
@pytest.mark.e2etest
2226
class TestExecutable:
23-
# @pytest.mark.skipif(
24-
# sys.platform == "win32" or sys.platform == "darwin",
25-
# reason="Raise Exception: Process terminated with exit code 4294967295",
26-
# )
2727
def test_flow_build_executable(self):
2828
source = f"{FLOWS_DIR}/web_classification/flow.dag.yaml"
2929
target = "promptflow._sdk.operations._flow_operations.FlowOperations._run_pyinstaller"
@@ -41,29 +41,32 @@ def test_flow_build_executable(self):
4141
"--format",
4242
"executable",
4343
)
44-
# Start the Python script as a subprocess
45-
app_file = Path(temp_dir, "app.py").as_posix()
46-
if not Path(app_file).exists():
47-
raise Exception(f"File {app_file} does not exist.")
48-
process = subprocess.Popen([sys.executable, app_file], stderr=subprocess.PIPE, shell=platform.system() == 'Windows')
49-
time.sleep(5)
50-
try:
51-
error_message = process.stderr.read().decode("utf-8")
52-
if process.poll() is not None:
53-
print(f"Error output from child process: {error_message}")
54-
raise Exception(f"Streamlit server did not start successfully. "
55-
f"error code: {process.returncode} message:{error_message}")
56-
else:
57-
try:
58-
response = requests.get("http://localhost:8501")
59-
if response.status_code == 200:
60-
print("Streamlit server started successfully.")
61-
else:
62-
raise Exception(f"Streamlit server did not start successfully. "
63-
f"error code: {process.returncode} message:{error_message}")
64-
except requests.exceptions.ConnectionError:
65-
raise Exception(f"Could not connect to Streamlit server. error code: "
66-
f"{process.returncode} message:{error_message}")
67-
finally:
68-
process.terminate()
69-
process.wait()
44+
with _change_working_dir(temp_dir):
45+
for filename in os.listdir(temp_dir):
46+
file_path = Path(temp_dir, filename)
47+
if os.path.isfile(file_path) and filename.endswith('.py'):
48+
with open(file_path, 'r') as file:
49+
try:
50+
tree = ast.parse(file.read())
51+
except SyntaxError as e:
52+
raise SyntaxError(f"Syntax error in file {file_path}: {e}")
53+
54+
for node in ast.walk(tree):
55+
if isinstance(node, (ast.Import, ast.ImportFrom)):
56+
for alias in node.names:
57+
module_name = alias.name
58+
if isinstance(node, ast.ImportFrom):
59+
module_name = node.module
60+
try:
61+
module = importlib.import_module(module_name)
62+
if isinstance(node, ast.ImportFrom):
63+
getattr(module, alias.name)
64+
except ImportError:
65+
raise ImportError(f"Module {module_name} in file {file_path} does not exist")
66+
except AttributeError:
67+
module_name = f"{node.module}.{alias.name}"
68+
try:
69+
importlib.import_module(module_name)
70+
except ImportError:
71+
raise ImportError(
72+
f"Cannot import {alias.name} from module {node.module} in file {file_path}")

0 commit comments

Comments
 (0)