forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-feedback.py
More file actions
executable file
·415 lines (349 loc) · 13.6 KB
/
Copy pathdev-feedback.py
File metadata and controls
executable file
·415 lines (349 loc) · 13.6 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env python3
"""Run one explicitly selected desktop regression test, once or after saves.
This deliberately complements rather than replaces the component's full test
suite. Pick the test filter yourself so a fast inner loop never guesses which
coverage is relevant to a change.
"""
from __future__ import annotations
import argparse
import math
import os
import re
import stat
import subprocess
import sys
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Iterable, Optional, Sequence
SCRIPT_DIR = Path(__file__).resolve().parent
DEFAULT_DESKTOP_ROOT = SCRIPT_DIR.parent
SWIFT_WATCH_INPUTS = (
"Desktop/Package.swift",
"Desktop/Package.resolved",
"Desktop/Sources",
"Desktop/Tests",
"Desktop/ObjCExceptionCatcher",
"Desktop/CWebP",
)
PYTHON_WATCH_INPUTS = (
"../../backend/desktop_backend.py",
"../../backend/routers",
"../../backend/database",
"../../backend/utils",
"../../backend/requirements.txt",
"../../backend/pylock.toml",
)
# `swift test --filter` exits 0 when the filter matches nothing, so the reported
# test counts — not the exit code — decide whether the selected coverage ran.
EXECUTED_TEST_COUNT_PATTERNS = (
re.compile(r"Executed (\d+) tests?[,\s]"),
re.compile(r"Test run with (\d+) tests?"),
)
@dataclass(frozen=True)
class TestCommand:
"""The focused command to run and the directory it must run from."""
language: str
test_filter: str
command: tuple[str, ...]
cwd: Path
Runner = Callable[..., subprocess.CompletedProcess[object]]
Snapshotter = Callable[[Iterable[Path]], object]
Emitter = Callable[[str], None]
Sleeper = Callable[[float], None]
Clock = Callable[[], float]
StopPredicate = Callable[[], bool]
def nonnegative_float(value: str) -> float:
try:
parsed = float(value)
except ValueError as error:
raise argparse.ArgumentTypeError(f"expected a number, got {value!r}") from error
if not math.isfinite(parsed):
raise argparse.ArgumentTypeError(f"expected a finite number, got {value!r}")
if parsed < 0:
raise argparse.ArgumentTypeError(f"expected a non-negative number, got {value!r}")
return parsed
def positive_float(value: str) -> float:
parsed = nonnegative_float(value)
if parsed == 0:
raise argparse.ArgumentTypeError("expected a number greater than zero")
return parsed
def normalized_filter(test_filter: str) -> str:
normalized = test_filter.strip()
if not normalized:
raise ValueError("test filter must not be empty")
return normalized
def test_command_for(desktop_root: Path, language: str, test_filter: str) -> TestCommand:
"""Construct the exact focused command without running it."""
desktop_root = desktop_root.resolve()
selected_filter = normalized_filter(test_filter)
if language == "swift":
return TestCommand(
language=language,
test_filter=selected_filter,
command=(
"xcrun",
"swift",
"test",
"--package-path",
"Desktop",
"--filter",
selected_filter,
),
cwd=desktop_root,
)
if language == "python":
return TestCommand(
language=language,
test_filter=selected_filter,
command=(".venv/bin/python", "-m", "pytest", selected_filter),
cwd=desktop_root.parent.parent / "backend",
)
raise ValueError(f"unsupported test language: {language!r}")
def watch_paths(desktop_root: Path, language: str) -> tuple[Path, ...]:
"""Return only source, test, and package inputs for the selected language."""
relative_inputs = SWIFT_WATCH_INPUTS if language == "swift" else PYTHON_WATCH_INPUTS
if language not in {"swift", "python"}:
raise ValueError(f"unsupported test language: {language!r}")
resolved_root = desktop_root.resolve()
return tuple(resolved_root / relative_path for relative_path in relative_inputs)
def _entry_fingerprint(path: Path) -> tuple[str, str, int, int]:
try:
metadata = path.lstat()
except OSError:
return (str(path), "missing", 0, 0)
if stat.S_ISDIR(metadata.st_mode):
kind = "directory"
elif stat.S_ISREG(metadata.st_mode):
kind = "file"
elif stat.S_ISLNK(metadata.st_mode):
kind = "symlink"
else:
kind = "other"
return (str(path), kind, metadata.st_mtime_ns, metadata.st_size)
def _path_fingerprint(path: Path) -> list[tuple[str, str, int, int]]:
entry = _entry_fingerprint(path)
if entry[1] != "directory":
return [entry]
entries = [entry]
for current_root, directory_names, file_names in os.walk(path, topdown=True, followlinks=False):
directory_names.sort()
file_names.sort()
current = Path(current_root)
for name in directory_names:
entries.append(_entry_fingerprint(current / name))
for name in file_names:
entries.append(_entry_fingerprint(current / name))
return entries
def snapshot_paths(paths: Iterable[Path]) -> tuple[tuple[str, str, int, int], ...]:
"""Fingerprint files recursively without watching build artifacts or the repo."""
entries: list[tuple[str, str, int, int]] = []
for path in paths:
entries.extend(_path_fingerprint(path))
return tuple(entries)
def executed_test_count(output: str) -> Optional[int]:
"""Return the highest executed-test count the runner reported, or None if it reported none."""
counts = [int(count) for pattern in EXECUTED_TEST_COUNT_PATTERNS for count in pattern.findall(output)]
return max(counts) if counts else None
def run_streaming(
command: Sequence[str],
*,
cwd: Path,
stream: object = None,
) -> subprocess.CompletedProcess[str]:
"""Run the test command, echoing its output live while retaining it for inspection."""
destination = sys.stdout if stream is None else stream
captured: list[str] = []
with subprocess.Popen(
tuple(command),
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
) as process:
assert process.stdout is not None
for line in process.stdout:
destination.write(line)
destination.flush()
captured.append(line)
returncode = process.wait()
return subprocess.CompletedProcess(tuple(command), returncode, "".join(captured), "")
def emit_iteration_result(
test_command: TestCommand,
iteration: int,
*,
runner: Runner = run_streaming,
clock: Clock = time.monotonic,
emit: Emitter = print,
) -> int:
"""Run a focused test and report its unambiguous timing outcome."""
emit(f"Iteration {iteration}: running {test_command.language} filter {test_command.test_filter!r}")
started_at = clock()
try:
result = runner(test_command.command, cwd=test_command.cwd)
except OSError as error:
elapsed = clock() - started_at
emit(f"Iteration {iteration}: ERROR ({error}) in {elapsed:.2f}s")
return 127
elapsed = clock() - started_at
if result.returncode == 0 and test_command.language == "swift":
# A green `swift test --filter` proves nothing on its own: it exits 0 both when the
# filter matched nothing and when the reporter never said what ran, so require the count.
executed = executed_test_count(getattr(result, "stdout", None) or "")
if executed is None:
emit(f"Iteration {iteration}: FAIL (no executed-test count reported; coverage unproven) in {elapsed:.2f}s")
return 1
if executed == 0:
emit(f"Iteration {iteration}: FAIL (filter {test_command.test_filter!r} matched 0 tests) in {elapsed:.2f}s")
return 1
if result.returncode == 0:
emit(f"Iteration {iteration}: PASS in {elapsed:.2f}s")
else:
emit(f"Iteration {iteration}: FAIL (exit {result.returncode}) in {elapsed:.2f}s")
return result.returncode
def wait_for_change(
previous_snapshot: object,
paths: Iterable[Path],
*,
poll_interval: float,
snapshotter: Snapshotter,
sleep: Sleeper,
) -> object:
while True:
sleep(poll_interval)
current_snapshot = snapshotter(paths)
if current_snapshot != previous_snapshot:
return current_snapshot
def wait_for_quiet(
first_changed_snapshot: object,
paths: Iterable[Path],
*,
poll_interval: float,
debounce: float,
snapshotter: Snapshotter,
sleep: Sleeper,
clock: Clock,
) -> object:
"""Coalesce editor temp-file writes until inputs have been quiet long enough."""
if debounce == 0:
return first_changed_snapshot
latest_snapshot = first_changed_snapshot
last_change_at = clock()
while True:
sleep(poll_interval)
current_snapshot = snapshotter(paths)
now = clock()
if current_snapshot != latest_snapshot:
latest_snapshot = current_snapshot
last_change_at = now
continue
if now - last_change_at >= debounce:
return latest_snapshot
def run_watch(
test_command: TestCommand,
desktop_root: Path,
*,
poll_interval: float,
debounce: float,
runner: Runner = run_streaming,
snapshotter: Snapshotter = snapshot_paths,
sleep: Sleeper = time.sleep,
clock: Clock = time.monotonic,
emit: Emitter = print,
should_stop: StopPredicate = lambda: False,
) -> int:
"""Run now, then rerun after coherent saves. Failures never end the loop."""
desktop_root = desktop_root.resolve()
paths = watch_paths(desktop_root, test_command.language)
emit(f"Watching {test_command.language} test inputs only:")
for path in paths:
emit(f" {path.relative_to(desktop_root)}")
snapshot = snapshotter(paths)
iteration = 1
last_status = emit_iteration_result(test_command, iteration, runner=runner, clock=clock, emit=emit)
while not should_stop():
changed_snapshot = wait_for_change(
snapshot,
paths,
poll_interval=poll_interval,
snapshotter=snapshotter,
sleep=sleep,
)
emit(f"Change detected; waiting {debounce:.2f}s for writes to settle.")
snapshot = wait_for_quiet(
changed_snapshot,
paths,
poll_interval=poll_interval,
debounce=debounce,
snapshotter=snapshotter,
sleep=sleep,
clock=clock,
)
iteration += 1
last_status = emit_iteration_result(test_command, iteration, runner=runner, clock=clock, emit=emit)
return last_status
def parser() -> argparse.ArgumentParser:
argument_parser = argparse.ArgumentParser(
description="Run one explicit Swift or Python desktop regression test with fast feedback.",
epilog=(
"Examples:\n"
" python3 scripts/dev-feedback.py --once swift 'ChatTests/testSendsMessage'\n"
" python3 scripts/dev-feedback.py --watch swift 'ChatTests/testSendsMessage'\n"
" python3 scripts/dev-feedback.py --watch python 'tests/unit/test_desktop_chat.py'\n\n"
"This is an opt-in inner loop. Run the existing full component suite before a PR."
),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
mode = argument_parser.add_mutually_exclusive_group(required=True)
mode.add_argument("--once", action="store_true", help="run the selected test once")
mode.add_argument("--watch", action="store_true", help="run now and rerun after relevant saves")
argument_parser.add_argument(
"--root",
type=Path,
default=DEFAULT_DESKTOP_ROOT,
help="desktop/macos directory (defaults to this script's parent)",
)
argument_parser.add_argument(
"--poll-interval",
type=positive_float,
default=0.10,
help="seconds between input scans while watching (default: 0.10)",
)
argument_parser.add_argument(
"--debounce",
type=nonnegative_float,
default=0.25,
help="quiet time before rerunning after a save (default: 0.25)",
)
argument_parser.add_argument("language", choices=("swift", "python"), help="focused test runner")
argument_parser.add_argument("test_filter", metavar="FILTER", help="non-empty XCTest or pytest test path")
return argument_parser
def validate_layout(desktop_root: Path, language: str) -> None:
expected_path = desktop_root / "Desktop/Package.swift" if language == "swift" else desktop_root.parent.parent / "backend/desktop_backend.py"
if not expected_path.is_file():
expected_file = "Desktop/Package.swift" if language == "swift" else "backend/desktop_backend.py"
raise ValueError(f"{expected_file} was not found under desktop root {desktop_root}")
def main(argv: Optional[Sequence[str]] = None) -> int:
argument_parser = parser()
arguments = argument_parser.parse_args(argv)
desktop_root = arguments.root.expanduser().resolve()
try:
validate_layout(desktop_root, arguments.language)
test_command = test_command_for(desktop_root, arguments.language, arguments.test_filter)
except ValueError as error:
argument_parser.error(str(error))
if arguments.once:
return emit_iteration_result(test_command, 1)
try:
return run_watch(
test_command,
desktop_root,
poll_interval=arguments.poll_interval,
debounce=arguments.debounce,
)
except KeyboardInterrupt:
print("Watch stopped.")
return 0
if __name__ == "__main__":
raise SystemExit(main())