-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstdlib_diff.py
More file actions
executable file
·287 lines (243 loc) · 9.05 KB
/
Copy pathstdlib_diff.py
File metadata and controls
executable file
·287 lines (243 loc) · 9.05 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python3
"""Standard-library builtin/function differential harness."""
from __future__ import annotations
import argparse
import json
import os
import subprocess
import sys
import tomllib
from dataclasses import dataclass, field
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / "stdlib"))
from normalize_php_output import normalize # noqa: E402
ROOT = Path(__file__).resolve().parents[1]
EXPECTATIONS = {"pass", "fail", "skip", "known_gap"}
@dataclass
class Fixture:
path: Path
test_id: str
area: str
expect: str = "pass"
known_gap: str | None = None
metadata: dict[str, str] = field(default_factory=dict)
def main() -> int:
args = parse_args()
report = run(args)
summary = report["summary"]
print(
"[ok] standard-library diff report: "
f"total={summary['total']} pass={summary['pass']} fail={summary['fail']} "
f"skip={summary['skip']} known_gap={summary['known_gap']} "
f"path={report['report_path']}"
)
return 1 if summary["fail"] else 0
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--fixtures", default="tests/fixtures/stdlib/_harness")
parser.add_argument("--out", default="target/stdlib/diff")
parser.add_argument("--area", action="append", default=[])
parser.add_argument("--file", action="append", default=[])
parser.add_argument(
"--vm-binary",
default=os.environ.get("PHRUST_STDLIB_VM", ""),
help="Path to a prebuilt php_vm_cli binary; defaults to building once.",
)
return parser.parse_args()
def run(args: argparse.Namespace) -> dict:
out_dir = ROOT / args.out
out_dir.mkdir(parents=True, exist_ok=True)
fixtures_root = ROOT / args.fixtures
known_gaps = load_known_gaps(fixtures_root / "known_gaps.toml")
fixtures = discover_fixtures(fixtures_root, args.area, args.file)
reference = discover_reference_php()
vm_binary = discover_vm_binary(args, fixtures, reference)
results = [
compare_fixture(fixture, reference, vm_binary, known_gaps, out_dir)
for fixture in fixtures
]
summary = summarize(results)
report_path = out_dir / "stdlib-diff-report.json"
report = {
"fixtures_root": str(fixtures_root),
"reference_php": reference,
"vm_binary": vm_binary,
"summary": summary,
"results": results,
"report_path": str(report_path),
}
report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
return report
def discover_vm_binary(
args: argparse.Namespace,
fixtures: list[Fixture],
reference: dict[str, str | bool],
) -> dict[str, str | bool]:
needs_vm = any(fixture.expect == "pass" for fixture in fixtures) and bool(reference["available"])
if not needs_vm:
return {"status": "not-needed", "path": "", "available": False}
if args.vm_binary:
path = Path(args.vm_binary)
return {
"status": "configured",
"path": str(path),
"available": path.exists() and os.access(path, os.X_OK),
}
target_dir = Path(os.environ.get("CARGO_TARGET_DIR", ROOT / "target"))
binary_name = "php-vm.exe" if sys.platform == "win32" else "php-vm"
binary = target_dir / "debug" / binary_name
if not binary.exists() or not os.access(binary, os.X_OK):
build = run_command(["cargo", "build", "-q", "-p", "php_vm_cli", "--bin", "php-vm"])
if build.returncode != 0:
return {
"status": "build-failed",
"path": str(binary),
"available": False,
"message": build.stderr or build.stdout,
}
return {
"status": "built-once",
"path": str(binary),
"available": binary.exists() and os.access(binary, os.X_OK),
}
def discover_reference_php() -> dict[str, str | bool]:
env_php = os.environ.get("REFERENCE_PHP")
if env_php:
path = Path(env_php)
return {
"status": "configured",
"path": env_php,
"available": path.exists() and os.access(path, os.X_OK),
}
pinned = ROOT / "third_party/php-src/sapi/cli/php"
if pinned.exists() and os.access(pinned, os.X_OK):
return {
"status": "pinned-default",
"path": str(pinned),
"available": True,
}
return {
"status": "missing",
"path": "",
"available": False,
"message": (
"Set REFERENCE_PHP=/absolute/path/to/php or run "
"`nix develop -c just build-ref-php` to build "
"third_party/php-src/sapi/cli/php from php-8.5.7."
),
}
def load_known_gaps(path: Path) -> dict[str, str]:
if not path.exists():
return {}
data = tomllib.loads(path.read_text(encoding="utf-8"))
return dict(data.get("known_gaps", {}))
def discover_fixtures(root: Path, areas: list[str], files: list[str]) -> list[Fixture]:
selected = [ROOT / item for item in files]
if not selected:
selected = sorted(path for path in root.rglob("*.php") if path.is_file())
fixtures = [load_fixture(path) for path in selected]
if areas:
fixtures = [fixture for fixture in fixtures if fixture.area in areas]
return fixtures
def load_fixture(path: Path) -> Fixture:
metadata: dict[str, str] = {}
for line in path.read_text(encoding="utf-8", errors="replace").splitlines()[:10]:
text = line.strip()
if not text.startswith("// stdlib-diff:"):
continue
for item in text[len("// stdlib-diff:") :].strip().split():
if "=" not in item:
continue
key, value = item.split("=", 1)
metadata[key] = value.strip('"')
test_id = metadata.get("id")
area = metadata.get("area")
if not test_id or not area:
raise HarnessError(f"{path}: fixture must declare id= and area=")
expect = metadata.get("expect", "pass")
if expect not in EXPECTATIONS:
raise HarnessError(f"{path}: unsupported expect={expect}")
return Fixture(
path=path,
test_id=test_id,
area=area,
expect=expect,
known_gap=metadata.get("known_gap"),
metadata=metadata,
)
def compare_fixture(
fixture: Fixture,
reference: dict[str, str | bool],
vm_binary: dict[str, str | bool],
known_gaps: dict[str, str],
out_dir: Path,
) -> dict:
if fixture.expect == "skip":
return result(fixture, "skip", "fixture marked skip")
if fixture.expect == "known_gap":
if not fixture.known_gap or fixture.known_gap not in known_gaps:
return result(fixture, "fail", "known_gap fixture lacks explicit known-gap ID")
return result(fixture, "known_gap", known_gaps[fixture.known_gap])
if not reference["available"]:
return result(fixture, "skip", str(reference.get("message", "REFERENCE_PHP unavailable")))
if not vm_binary["available"]:
return result(
fixture,
"fail",
str(vm_binary.get("message", "php_vm_cli binary unavailable")),
)
ref = run_command([str(reference["path"]), str(fixture.path)])
rust = run_command([str(vm_binary["path"]), "run", str(fixture.path)])
ref_norm = normalized_run(ref)
rust_norm = normalized_run(rust)
detail = {
"reference": ref_norm,
"rust": rust_norm,
}
detail_path = out_dir / f"{fixture.test_id}.json"
detail_path.write_text(json.dumps(detail, indent=2, sort_keys=True) + "\n", encoding="utf-8")
if ref_norm == rust_norm:
return result(fixture, "pass", "matched reference", detail_path)
return result(fixture, "fail", "normalized output differs", detail_path)
def run_command(command: list[str]) -> subprocess.CompletedProcess[str]:
return subprocess.run(
command,
cwd=ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
)
def normalized_run(run: subprocess.CompletedProcess[str]) -> dict[str, str | int]:
return {
"exit": run.returncode,
"stdout": normalize(run.stdout),
"stderr": normalize(run.stderr),
}
def result(fixture: Fixture, status: str, message: str, detail_path: Path | None = None) -> dict:
entry = {
"id": fixture.test_id,
"area": fixture.area,
"path": str(fixture.path),
"expect": fixture.expect,
"status": status,
"message": message,
}
if fixture.known_gap:
entry["known_gap"] = fixture.known_gap
if detail_path:
entry["detail_path"] = str(detail_path)
return entry
def summarize(results: list[dict]) -> dict[str, int]:
summary = {"total": len(results), "pass": 0, "fail": 0, "skip": 0, "known_gap": 0}
for item in results:
summary[item["status"]] += 1
return summary
class HarnessError(RuntimeError):
pass
if __name__ == "__main__":
try:
raise SystemExit(main())
except HarnessError as error:
print(f"[error] {error}", file=sys.stderr)
raise SystemExit(2)