-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_comprehensive.py
More file actions
232 lines (187 loc) · 7.44 KB
/
Copy pathtest_comprehensive.py
File metadata and controls
232 lines (187 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
"""Test script to verify ExecuTrace functionality."""
import json
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
def run_cmd(cmd: list[str]) -> tuple[int, str]:
"""Run a command and return exit code and output."""
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
return result.returncode, result.stdout + result.stderr
except subprocess.TimeoutExpired:
return 1, "Command timed out"
except Exception as e:
return 1, str(e)
def test_cli_help():
"""Test that all CLI help messages work."""
print("Testing CLI help messages...")
commands = [
["python", "-m", "exectrace", "--help"],
["python", "-m", "exectrace", "record", "--help"],
["python", "-m", "exectrace", "replay", "--help"],
["python", "-m", "exectrace", "list", "--help"],
["python", "-m", "exectrace", "edit", "--help"],
["python", "-m", "exectrace", "delete", "--help"],
]
for cmd in commands:
code, output = run_cmd(cmd)
if code != 0:
print(f"❌ Failed: {' '.join(cmd)}")
print(output)
return False
if "usage:" not in output.lower():
print(f"❌ Unexpected output for: {' '.join(cmd)}")
return False
print("✅ All CLI help messages working\n")
return True
def test_imports():
"""Test that all modules can be imported."""
print("Testing module imports...")
try:
from exectrace.cli import main
from exectrace.storage.json_storage import JsonStorage
from exectrace.storage.xml_storage import XmlStorage
from exectrace.storage.factory import get_storage
from exectrace.recorder.session import RecorderSession
from exectrace.core.replayer import Replayer
from exectrace.core.editor import WorkflowEditor
from exectrace.utils.interactive import prompt_file_format
print("✅ All imports successful\n")
return True
except ImportError as e:
print(f"❌ Import failed: {e}\n")
return False
def test_storage_backends():
"""Test JSON and XML storage backends."""
print("Testing storage backends...")
with tempfile.TemporaryDirectory() as tmpdir:
# Test JSON storage
from exectrace.storage.json_storage import JsonStorage
from exectrace.core.models import Workflow
json_storage = JsonStorage(tmpdir)
workflow = Workflow(name="test-json")
workflow.add_action("command", {"command": "echo test", "cwd": "."})
json_storage.save_workflow(workflow)
loaded = json_storage.load_workflow("test-json")
if loaded.name != "test-json" or len(loaded.actions) != 1:
print("❌ JSON storage failed")
return False
print(" ✅ JSON storage working")
# Test XML storage
from exectrace.storage.xml_storage import XmlStorage
xml_storage = XmlStorage(tmpdir)
workflow = Workflow(name="test-xml")
workflow.add_action("command", {"command": "echo test", "cwd": "."})
xml_storage.save_workflow(workflow)
loaded = xml_storage.load_workflow("test-xml")
if loaded.name != "test-xml" or len(loaded.actions) != 1:
print("❌ XML storage failed")
return False
print(" ✅ XML storage working")
# Test factory
from exectrace.storage.factory import get_storage
json_backend = get_storage("json", tmpdir)
xml_backend = get_storage("xml", tmpdir)
if not isinstance(json_backend, JsonStorage) or not isinstance(xml_backend, XmlStorage):
print("❌ Storage factory failed")
return False
print(" ✅ Storage factory working")
print("✅ All storage backends working\n")
return True
def test_workflow_operations():
"""Test workflow operations (create, edit, delete)."""
print("Testing workflow operations...")
with tempfile.TemporaryDirectory() as tmpdir:
from exectrace.core.models import Workflow
from exectrace.storage.factory import get_storage
from exectrace.core.editor import WorkflowEditor
storage = get_storage("json", tmpdir)
# Create
workflow = Workflow(name="ops-test")
workflow.add_action("command", {"command": "ls", "cwd": "."})
storage.save_workflow(workflow)
print(" ✅ Create workflow")
# Edit using WorkflowEditor
editor = WorkflowEditor(storage)
loaded = editor.load("ops-test")
loaded.add_action("command", {"command": "pwd", "cwd": "."})
editor.save(loaded)
reloaded = storage.load_workflow("ops-test")
if len(reloaded.actions) != 2:
print("❌ Workflow edit failed")
return False
print(" ✅ Edit workflow")
# List
workflows = storage.list_workflows()
if "ops-test" not in workflows:
print("❌ Workflow list failed")
return False
print(" ✅ List workflows")
# Delete
path = storage.workflow_path("ops-test")
path.unlink()
if path.exists():
print("❌ Workflow delete failed")
return False
print(" ✅ Delete workflow")
print("✅ All workflow operations working\n")
return True
def test_replayer():
"""Test replay functionality."""
print("Testing replay functionality...")
with tempfile.TemporaryDirectory() as tmpdir:
from exectrace.core.models import Workflow
from exectrace.core.replayer import Replayer
from exectrace.storage.factory import get_storage
storage = get_storage("json", tmpdir)
# Create a simple workflow
workflow = Workflow(name="replay-test")
workflow.add_action("command", {"command": "echo 'test'", "cwd": "."})
storage.save_workflow(workflow)
# Test dry-run
replayer = Replayer(storage)
result = replayer.replay(workflow, dry_run=True)
if result != 1:
print("❌ Dry-run replay failed")
return False
print(" ✅ Dry-run replay working")
# Test explain mode
result = replayer.replay(workflow, explain=True, dry_run=True)
if result != 1:
print("❌ Explain mode failed")
return False
print(" ✅ Explain mode working")
print("✅ Replay functionality working\n")
return True
def main_test():
"""Run all tests."""
print("=" * 60)
print("ExecuTrace Test Suite")
print("=" * 60 + "\n")
tests = [
test_imports,
test_cli_help,
test_storage_backends,
test_workflow_operations,
test_replayer,
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"❌ Test failed with exception: {e}\n")
results.append(False)
print("=" * 60)
print(f"Results: {sum(results)}/{len(results)} tests passed")
print("=" * 60)
return all(results)
if __name__ == "__main__":
os.chdir("/home/w4nn4d13/Project/ExecuTrace")
success = main_test()
sys.exit(0 if success else 1)