1+ #!/usr/bin/env python3
2+
3+ import os
4+ import sys
5+ import subprocess
6+ import json
7+ from datetime import datetime
8+
9+ class PFTaskTester :
10+ def __init__ (self ):
11+ self .results = {}
12+ self .workspace = '/workspace'
13+
14+ def run_command (self , cmd , timeout = 60 ):
15+ """Run a command and return (success, stdout, stderr)"""
16+ try :
17+ os .chdir (self .workspace )
18+ result = subprocess .run (
19+ cmd ,
20+ shell = True ,
21+ capture_output = True ,
22+ text = True ,
23+ timeout = timeout
24+ )
25+ return result .returncode == 0 , result .stdout , result .stderr
26+ except subprocess .TimeoutExpired :
27+ return False , "" , "Command timed out"
28+ except Exception as e :
29+ return False , "" , str (e )
30+
31+ def test_task (self , task_name , command , description = "" ):
32+ """Test a single pf task"""
33+ print (f"\n { '=' * 50 } " )
34+ print (f"Testing: { task_name } " )
35+ print (f"Command: { command } " )
36+ if description :
37+ print (f"Description: { description } " )
38+ print ('=' * 50 )
39+
40+ success , stdout , stderr = self .run_command (command )
41+
42+ self .results [task_name ] = {
43+ 'command' : command ,
44+ 'success' : success ,
45+ 'stdout' : stdout [:500 ] if stdout else "" ,
46+ 'stderr' : stderr [:500 ] if stderr else "" ,
47+ 'description' : description
48+ }
49+
50+ if success :
51+ print (f"✓ { task_name } : PASSED" )
52+ if stdout :
53+ print (f"Output: { stdout [:200 ]} ..." )
54+ else :
55+ print (f"✗ { task_name } : FAILED" )
56+ if stderr :
57+ print (f"Error: { stderr [:200 ]} ..." )
58+
59+ return success
60+
61+ def test_all_pf_tasks (self ):
62+ """Test all tasks defined in .pf file"""
63+ print ("=== COMPREHENSIVE PF TASK TESTING ===" )
64+ print (f"Started at: { datetime .now ()} " )
65+ print (f"Workspace: { self .workspace } " )
66+
67+ # Read the .pf file to understand what we're testing
68+ try :
69+ with open (f"{ self .workspace } /.pf" , 'r' ) as f :
70+ pf_content = f .read ()
71+ print (f"\n PF file content preview:\n { pf_content [:300 ]} ..." )
72+ except Exception as e :
73+ print (f"Could not read .pf file: { e } " )
74+
75+ # Test each task from the .pf file
76+ # Note: Testing the underlying commands since pf tool may not be available
77+
78+ tasks_to_test = [
79+ # Basic functionality tests
80+ ("test-import" , "python3 -c 'import cdp; print(cdp.accessibility)'" ,
81+ "Test basic CDP module import" ),
82+
83+ # Code generation
84+ ("generate" , "python3 generator/generate.py" ,
85+ "Generate CDP bindings from JSON specs" ),
86+
87+ # Testing tasks
88+ ("test-generate" , "python3 -m pytest generator/ -v" ,
89+ "Run tests on the generator code" ),
90+
91+ ("test-cdp" , "python3 -m pytest test/ -v" ,
92+ "Run tests on the CDP modules" ),
93+
94+ # Type checking tasks
95+ ("mypy-generate" , "python3 -m mypy generator/" ,
96+ "Type check the generator code" ),
97+
98+ ("mypy-cdp" , "python3 -m mypy cdp/" ,
99+ "Type check the CDP modules" ),
100+
101+ # Documentation
102+ ("docs" , "cd docs && python3 -m sphinx -b html . _build/html" ,
103+ "Build documentation" ),
104+
105+ # Combined tasks (these map to pf tasks)
106+ ("typecheck-combined" , "python3 -m mypy generator/ && python3 -m mypy cdp/" ,
107+ "Combined type checking (typecheck pf task)" ),
108+
109+ ("test-combined" , "python3 -m pytest test/ -v && python3 -m pytest generator/ -v && python3 -c 'import cdp; print(cdp.accessibility)'" ,
110+ "Combined testing (test pf task)" ),
111+
112+ ("check-combined" , "python3 -m pytest test/ -v && python3 -c 'import cdp; print(cdp.accessibility)'" ,
113+ "Quick check (check pf task)" ),
114+ ]
115+
116+ # Run all tests
117+ passed = 0
118+ total = len (tasks_to_test )
119+
120+ for task_name , command , description in tasks_to_test :
121+ if self .test_task (task_name , command , description ):
122+ passed += 1
123+
124+ # Summary
125+ print (f"\n { '=' * 60 } " )
126+ print ("FINAL TEST RESULTS" )
127+ print ('=' * 60 )
128+
129+ for task_name in self .results :
130+ result = self .results [task_name ]
131+ status = "✓ PASS" if result ['success' ] else "✗ FAIL"
132+ print (f"{ task_name :20} { status } " )
133+
134+ print (f"\n Summary: { passed } /{ total } tasks passed" )
135+
136+ if passed == total :
137+ print ("🎉 ALL PF TASKS ARE WORKING CORRECTLY!" )
138+ print ("✓ Every command in the .pf file has been tested and works." )
139+ else :
140+ print ("⚠️ SOME PF TASKS NEED ATTENTION" )
141+ print ("✗ Failed tasks need to be fixed or removed per rules." )
142+
143+ # Save detailed results
144+ self .save_results ()
145+
146+ return passed == total
147+
148+ def save_results (self ):
149+ """Save test results to file"""
150+ try :
151+ with open (f"{ self .workspace } /pf_test_results.json" , 'w' ) as f :
152+ json .dump ({
153+ 'timestamp' : datetime .now ().isoformat (),
154+ 'summary' : {
155+ 'total_tasks' : len (self .results ),
156+ 'passed_tasks' : sum (1 for r in self .results .values () if r ['success' ]),
157+ 'failed_tasks' : sum (1 for r in self .results .values () if not r ['success' ])
158+ },
159+ 'results' : self .results
160+ }, f , indent = 2 )
161+ print (f"\n 📄 Detailed results saved to: pf_test_results.json" )
162+ except Exception as e :
163+ print (f"Could not save results: { e } " )
164+
165+ def main ():
166+ tester = PFTaskTester ()
167+ success = tester .test_all_pf_tasks ()
168+
169+ if not success :
170+ print ("\n ⚠️ ACTION REQUIRED:" )
171+ print ("Some pf tasks failed. Per rules, these need to be:" )
172+ print ("1. Fixed if they're still relevant" )
173+ print ("2. Removed if they're no longer needed" )
174+ print ("3. Updated if they're outdated" )
175+
176+ return 0 if success else 1
177+
178+ if __name__ == "__main__" :
179+ sys .exit (main ())
0 commit comments