-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·274 lines (225 loc) · 8.13 KB
/
run_tests.py
File metadata and controls
executable file
·274 lines (225 loc) · 8.13 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python3
"""
run_tests.py - Run PVS verification tests for all examples
"""
import os
import sys
import subprocess
import shutil
import argparse
import yaml
from pathlib import Path
from typing import List, Tuple, Dict, Optional
# Colors for terminal output
class Colors:
GREEN = '\033[0;32m'
RED = '\033[0;31m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
NC = '\033[0m' # No Color
def log(msg: str, color: str = ''):
"""Print colored message."""
if color:
print(f"{color}{msg}{Colors.NC}")
else:
print(msg)
def clean_example_caches(examples_dir: Path):
"""Clean PVS cache files from all example directories."""
log("Cleaning PVS cache files from examples...", Colors.YELLOW)
cleaned_count = 0
for example_dir in examples_dir.iterdir():
if not example_dir.is_dir():
continue
# Remove .pvscontext files
pvscontext = example_dir / ".pvscontext"
if pvscontext.exists():
pvscontext.unlink()
cleaned_count += 1
# Remove pvsbin directories
pvsbin = example_dir / "pvsbin"
if pvsbin.exists() and pvsbin.is_dir():
shutil.rmtree(pvsbin)
cleaned_count += 1
# Remove test_output directories
test_output = example_dir / "test_output"
if test_output.exists() and test_output.is_dir():
shutil.rmtree(test_output)
# Remove output directories
output = example_dir / "output"
if output.exists() and output.is_dir():
shutil.rmtree(output)
# Remove auto-generated ADT files
for adt_file in example_dir.glob("*_adt.pvs"):
adt_file.unlink()
cleaned_count += 1
for adt_reduce_file in example_dir.glob("*_adt_reduce.pvs"):
adt_reduce_file.unlink()
cleaned_count += 1
log(f"Cleaned {cleaned_count} cache files/directories", Colors.GREEN)
print()
def clean_test_cache(example_dir: Path):
"""Clean PVS cache files from a single test directory."""
pvscontext = example_dir / ".pvscontext"
if pvscontext.exists():
pvscontext.unlink()
pvsbin = example_dir / "pvsbin"
if pvsbin.exists() and pvsbin.is_dir():
shutil.rmtree(pvsbin)
def run_test(name: str, example_dir: Path, transform: Path, constraints: Path,
input_schema: Path, output_schema: Path, pvs_path: Path) -> bool:
"""Run a single verification test."""
log(f"Testing: {name}", Colors.YELLOW)
print(f" Directory: {example_dir}")
output_dir = example_dir / "test_output"
# Build command
cmd = [
sys.executable,
"pvs_main.py",
"verify",
"--transform", str(transform),
"--constraints", str(constraints),
"--input-schema", str(input_schema),
"--output-schema", str(output_schema),
"--pvs", str(pvs_path),
"--output", str(output_dir),
"--instance-name", f"{name}_test"
]
try:
# Run verification (suppress output)
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=300 # 5 minute timeout
)
if result.returncode == 0:
log(" ✓ PASSED", Colors.GREEN)
# Clean up test output
if output_dir.exists():
shutil.rmtree(output_dir)
# Clean up cache files from example directory
clean_test_cache(example_dir)
return True
else:
log(" ✗ FAILED", Colors.RED)
if result.stderr:
print(f" Error: {result.stderr[:200]}")
return False
except subprocess.TimeoutExpired:
log(" ✗ TIMEOUT", Colors.RED)
return False
except Exception as e:
log(f" ✗ ERROR: {e}", Colors.RED)
return False
finally:
print()
def load_test_config(example_dir: Path) -> Optional[Dict]:
"""Load test configuration from YAML file."""
config_file = example_dir / "test_config.yaml"
if not config_file.exists():
return None
try:
with open(config_file, 'r') as f:
config = yaml.safe_load(f)
# Validate required fields
required = ['input_schema', 'output_schema', 'transform', 'constraints']
if not all(field in config for field in required):
return None
return config
except Exception as e:
return None
def discover_tests(examples_dir: Path) -> List[Tuple[str, Path, Path, Path, Path, Path]]:
"""Discover all test cases in examples directory."""
tests = []
for example_subdir in sorted(examples_dir.iterdir()):
if not example_subdir.is_dir():
continue
# Skip ex1 (JSLT-based, not for pvs_main)
if example_subdir.name == 'ex1':
continue
# Load test configuration
config = load_test_config(example_subdir)
if not config:
continue
# Build paths from config
transform = example_subdir / config['transform']
constraints = example_subdir / config['constraints']
input_schema = example_subdir / config['input_schema']
output_schema = example_subdir / config['output_schema']
# Verify all files exist
if all(f.exists() for f in [transform, constraints, input_schema, output_schema]):
tests.append((
example_subdir.name,
example_subdir,
transform,
constraints,
input_schema,
output_schema
))
return tests
def main():
"""Main test runner."""
# Parse arguments
parser = argparse.ArgumentParser(description="Run PVS verification tests")
parser.add_argument('--clean', action='store_true',
help='Clean PVS cache files before running tests')
args = parser.parse_args()
# Configuration
script_dir = Path(__file__).parent.resolve()
examples_dir = script_dir / "examples"
# Detect Docker environment
if os.environ.get("FHIRFLY_DOCKER") == "true":
default_pvs = "/home/fhirfly/PVS/pvs"
else:
default_pvs = os.path.expanduser("~/git/PVS/pvs")
pvs_path = Path(os.environ.get("PVS_PATH", default_pvs))
# Print header
log("=" * 50, Colors.BLUE)
log("FHIR-Fly PVS Verification Test Suite", Colors.BLUE)
log("=" * 50, Colors.BLUE)
print(f"PVS Path: {pvs_path}")
print()
# Clean caches if requested
if args.clean:
clean_example_caches(examples_dir)
# Check PVS exists
if not pvs_path.exists():
log(f"Error: PVS not found at {pvs_path}", Colors.RED)
log("Set PVS_PATH environment variable to the correct path", Colors.RED)
return 1
# Discover tests
tests = discover_tests(examples_dir)
if not tests:
log("No tests found!", Colors.YELLOW)
return 1
log(f"Found {len(tests)} test(s)", Colors.BLUE)
print()
# Run tests
results = []
for name, ex_dir, transform, constraints, in_schema, out_schema in tests:
passed = run_test(name, ex_dir, transform, constraints, in_schema, out_schema, pvs_path)
results.append((name, passed))
# Print summary
log("=" * 50, Colors.BLUE)
log("Test Summary", Colors.BLUE)
log("=" * 50, Colors.BLUE)
total = len(results)
passed = sum(1 for _, p in results if p)
failed = total - passed
print(f"Total tests: {total}")
log(f"Passed: {passed}", Colors.GREEN)
log(f"Failed: {failed}", Colors.RED if failed > 0 else Colors.GREEN)
log("=" * 50, Colors.BLUE)
if failed == 0:
log("All tests passed!", Colors.GREEN)
return 0
else:
log("Some tests failed!", Colors.RED)
print("\nFailed tests:")
for name, passed in results:
if not passed:
log(f" - {name}", Colors.RED)
return 1
if __name__ == '__main__':
os.chdir(Path(__file__).parent)
sys.exit(main())