7
7
import requests
8
8
import platform
9
9
import sys
10
+ import os
11
+ import ast
12
+ import importlib
10
13
11
14
from .test_cli import run_pf_command
15
+ from promptflow ._utils .context_utils import _change_working_dir
12
16
13
17
FLOWS_DIR = "./tests/test_configs/flows"
14
18
RUNS_DIR = "./tests/test_configs/runs"
20
24
@pytest .mark .cli_test
21
25
@pytest .mark .e2etest
22
26
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
- # )
27
27
def test_flow_build_executable (self ):
28
28
source = f"{ FLOWS_DIR } /web_classification/flow.dag.yaml"
29
29
target = "promptflow._sdk.operations._flow_operations.FlowOperations._run_pyinstaller"
@@ -41,29 +41,32 @@ def test_flow_build_executable(self):
41
41
"--format" ,
42
42
"executable" ,
43
43
)
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