lirk 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lirk/__init__.py ADDED
File without changes
lirk/__main__.py ADDED
@@ -0,0 +1,4 @@
1
+ from lirk.cli import main
2
+
3
+ if __name__ == "__main__":
4
+ raise SystemExit(main())
lirk/actions.py ADDED
@@ -0,0 +1,660 @@
1
+ """Build and test actions.
2
+
3
+ Per the process-model constraints this project exists to satisfy: one
4
+ direct subprocess.run() call per test file, output and exit code
5
+ captured straight from that call (no separate results-file step), no
6
+ new process group/session, no pseudo-terminal, no shell=True.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import ast
12
+ import os
13
+ import re
14
+ import subprocess
15
+ import sys
16
+ from collections.abc import Iterable, Mapping, Sequence, Set
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path
19
+
20
+ from lirk.targets import Target, pytest_inputs
21
+
22
+ # Chosen so it doesn't trip on the slowest known test target (~12s,
23
+ # backgammon:main_test). subprocess.run's timeout only kills the
24
+ # direct child, not any grandchild a main_test.py may have spawned
25
+ # itself (e.g. a main.py it drives) -- killing the whole tree needs a
26
+ # process group, which this project's process model forbids. A
27
+ # partially-cleaned-up timeout is accepted as strictly better than an
28
+ # unbounded hang.
29
+ TEST_TIMEOUT_SECONDS = 600
30
+
31
+ # unittest's closing summary line, e.g. "Ran 3 tests in 0.004s". Anchored
32
+ # to a line start, and only the *last* occurrence is consulted: a test
33
+ # that itself drives unittest (lirk's own suite does) can echo an inner
34
+ # run's summary into its stderr before its own.
35
+ _RAN_LINE = re.compile(r"^Ran (\d+) tests? in ", re.MULTILINE)
36
+
37
+
38
+ def _ran_zero_tests(stderr: str) -> bool:
39
+ matches = _RAN_LINE.findall(stderr)
40
+ return bool(matches) and matches[-1] == "0"
41
+
42
+
43
+ # pytest's closing summary, e.g. "==== 1 skipped in 0.01s ====" or
44
+ # "==== 2 passed, 1 skipped in 0.30s ====".
45
+ _PYTEST_SUMMARY = re.compile(r"^=+ (.*) in [\d.]+s\b.*=+\s*$", re.MULTILINE)
46
+
47
+
48
+ def _pytest_skipped_module(stdout: str) -> bool:
49
+ """pytest collected nothing because the module skipped itself.
50
+
51
+ True only when the final summary reports skips and nothing else ran:
52
+ "1 skipped", not "no tests ran".
53
+ """
54
+ summaries = _PYTEST_SUMMARY.findall(stdout)
55
+ if not summaries:
56
+ return False
57
+ parts = {p.strip().split(" ", 1)[-1] for p in summaries[-1].split(",")}
58
+ return "skipped" in parts and parts <= {"skipped", "warning", "warnings", "deselected"}
59
+
60
+
61
+ def _pytest_missing(stderr: str) -> bool:
62
+ """`python -m pytest` failed because pytest isn't importable.
63
+
64
+ Worth naming: the raw "No module named pytest" is easy to misread as
65
+ the test's own import failing, and the fix (install pytest for the
66
+ interpreter lirk runs under) is nothing to do with the test.
67
+ """
68
+ return "No module named pytest" in stderr
69
+
70
+
71
+ def _as_text(captured: str | bytes | None) -> str:
72
+ """Partial output from a TimeoutExpired, as str.
73
+
74
+ subprocess.run raises TimeoutExpired carrying the *raw bytes* read so
75
+ far, even when called with text=True -- the decode only happens on
76
+ the normal return path. Joining that with the str output of other
77
+ srcs raised TypeError, so a timed-out test that had printed anything
78
+ crashed lirk instead of being reported.
79
+ """
80
+ if captured is None:
81
+ return ""
82
+ if isinstance(captured, bytes):
83
+ return captured.decode("utf-8", errors="replace")
84
+ return captured
85
+
86
+
87
+ @dataclass
88
+ class ActionResult:
89
+ label: str
90
+ ok: bool
91
+ message: str
92
+ stdout: str = ""
93
+ stderr: str = ""
94
+ # Things worth telling the user that don't fail the target.
95
+ notes: list[str] = field(default_factory=list)
96
+
97
+
98
+ @dataclass(frozen=True)
99
+ class ImportEnv:
100
+ """Repo context the import check needs, assembled by the caller.
101
+
102
+ Passed in rather than derived here so this module stays free of the
103
+ graph layer: `owners` is a repo-wide index and `allowed` is
104
+ per-target, and only the CLI has both.
105
+
106
+ - `owners`: resolved absolute path of every declared src -> the
107
+ labels of every target declaring it (usually exactly one).
108
+ - `allowed`: the labels this target may import from -- its whole
109
+ transitive dep closure, including itself.
110
+ - `resolved`: a memo of import lookups, (dotted name, one base
111
+ directory) -> resolved file or None. Share one dict across every
112
+ target in a run: resolving an import means stat-ing candidate
113
+ files, and the same lookups recur target after target -- at 1,000
114
+ targets the repeated stats were half of a cold build. Keyed per
115
+ base directory, not per list of bases, so a lookup against the
116
+ repo root is answered once for the whole run rather than once per
117
+ package. Scoped to one run, never kept between runs, since files
118
+ can change between them.
119
+ """
120
+
121
+ owners: Mapping[Path, frozenset[str]]
122
+ allowed: Set[str]
123
+ resolved: dict[tuple[str, Path], Path | None] = field(
124
+ default_factory=dict, compare=False
125
+ )
126
+
127
+
128
+ def missing_files(target: Target, root: Path) -> list[str]:
129
+ """Declared srcs/data files (relative to the target's package) that
130
+ don't exist. Checked up front by the CLI, before fingerprinting,
131
+ so a missing file is reported as a clean per-target failure rather
132
+ than an unguarded traceback out of the cache layer.
133
+
134
+ A `data` entry may name a directory, in which case it exists if the
135
+ directory does; `srcs` must still be files, since every src is
136
+ parsed as Python."""
137
+ pkg_dir = root / target.package
138
+ missing = [s for s in target.srcs if not (pkg_dir / s).is_file()]
139
+ missing += [
140
+ d
141
+ for d in target.data
142
+ if not (pkg_dir / d).is_file() and not (pkg_dir / d).is_dir()
143
+ ]
144
+ return missing
145
+
146
+
147
+ def owner_index(
148
+ targets: Mapping[str, Target], root: Path
149
+ ) -> dict[Path, frozenset[str]]:
150
+ """Resolved path of every declared src -> the labels declaring it.
151
+
152
+ Built once per run and shared by every target's ImportEnv. A src
153
+ declared by more than one target maps to all of them, and an import
154
+ of it is allowed if *any* declaring target is in the importer's
155
+ closure: each of them fingerprints the file, so any one in the
156
+ closure is enough to make an edit invalidate the importer. Keeping
157
+ only one owner (as this once did) made the verdict depend on which
158
+ declaration happened to be indexed last -- a correct `deps` entry on
159
+ the other declaring target was reported as an undeclared import.
160
+ """
161
+ index: dict[Path, set[str]] = {}
162
+ for label, target in targets.items():
163
+ for src in target.srcs:
164
+ index.setdefault((root / target.package / src).resolve(), set()).add(label)
165
+ return {path: frozenset(labels) for path, labels in index.items()}
166
+
167
+
168
+ def _imported_modules(
169
+ tree: ast.AST, src_path: Path
170
+ ) -> list[tuple[str, Path | None, str]]:
171
+ """Every module an AST imports or runs, as (dotted name, base
172
+ directory, verb) -- the verb is "imports" or "runs", for messages.
173
+
174
+ The base is None for an absolute import (resolve it against sys.path
175
+ as the runner sets it up) and an explicit directory for a relative
176
+ one.
177
+
178
+ `from pkg import name` yields both `pkg` and `pkg.name`: `name` may
179
+ be a submodule -- which is an edge into another package -- or an
180
+ ordinary attribute, which simply won't resolve to a file and is
181
+ ignored.
182
+
183
+ Modules *run* rather than imported count too, because they are just
184
+ as much an input: a literal `"-m", "pkg.mod"` pair in a list or
185
+ tuple (the argv of `subprocess.run([sys.executable, "-m", ...])`),
186
+ and `runpy.run_module("pkg.mod")`. That is the documented way to
187
+ test an entry point, and without this its test imported nothing
188
+ from the app: no edge was required, `lirk fix` added none, and
189
+ editing the entry point left the test cached and green (found by
190
+ the slingshot repo). A string that resolves to no repo file is
191
+ ignored like any other unresolvable import, so false positives
192
+ cost nothing.
193
+ """
194
+ found: list[tuple[str, Path | None, str]] = []
195
+ for node in ast.walk(tree):
196
+ if isinstance(node, (ast.List, ast.Tuple)):
197
+ elts = node.elts
198
+ for flag, value in zip(elts, elts[1:]):
199
+ if (
200
+ isinstance(flag, ast.Constant) and flag.value == "-m"
201
+ and isinstance(value, ast.Constant) and isinstance(value.value, str)
202
+ ):
203
+ found.append((value.value, None, "runs"))
204
+ # `-m pkg` executes pkg/__main__.py as well as
205
+ # pkg/__init__.py; both are inputs. Resolves to
206
+ # nothing for a plain module, so harmless there.
207
+ found.append((f"{value.value}.__main__", None, "runs"))
208
+ elif (
209
+ isinstance(node, ast.Call)
210
+ and isinstance(node.func, (ast.Attribute, ast.Name))
211
+ and (node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id)
212
+ == "run_module"
213
+ and node.args
214
+ and isinstance(node.args[0], ast.Constant)
215
+ and isinstance(node.args[0].value, str)
216
+ ):
217
+ found.append((node.args[0].value, None, "runs"))
218
+ found.append((f"{node.args[0].value}.__main__", None, "runs"))
219
+ elif isinstance(node, ast.Import):
220
+ found.extend((alias.name, None, "imports") for alias in node.names)
221
+ elif isinstance(node, ast.ImportFrom):
222
+ base: Path | None = None
223
+ if node.level:
224
+ # `.` is the src's own directory, `..` its parent, etc.
225
+ base = src_path.parent
226
+ for _ in range(node.level - 1):
227
+ base = base.parent
228
+ prefix = node.module or ""
229
+ if prefix:
230
+ found.append((prefix, base, "imports"))
231
+ for alias in node.names:
232
+ dotted = f"{prefix}.{alias.name}" if prefix else alias.name
233
+ found.append((dotted, base, "imports"))
234
+ return found
235
+
236
+
237
+ def _resolve_module(dotted: str, bases: Sequence[Path]) -> Path | None:
238
+ """The file a dotted module name resolves to under `bases`, or None.
239
+
240
+ Mirrors how the runner actually sets up imports (see run_test): the
241
+ target's own package directory is `sys.path[0]`, and the repo root
242
+ is on PYTHONPATH. Anything resolving outside those -- the stdlib,
243
+ site-packages -- returns None and is not lirk's business.
244
+
245
+ Resolves the full dotted path to one file, rather than also
246
+ collecting the `__init__.py` of each package along the way. Those
247
+ ancestors are inputs too in principle, but in a repo that declares
248
+ its packages at all they belong to the same target as the module
249
+ itself, and chasing them turns one clear error into several.
250
+ """
251
+ parts = dotted.split(".")
252
+ if not all(parts):
253
+ return None
254
+ for base in bases:
255
+ candidate = base.joinpath(*parts)
256
+ module = candidate.with_suffix(".py")
257
+ if module.is_file():
258
+ return module
259
+ init = candidate / "__init__.py"
260
+ if init.is_file():
261
+ return init
262
+ return None
263
+
264
+
265
+ @dataclass(frozen=True)
266
+ class ImportProblem:
267
+ """One import reaching outside a target's dep closure.
268
+
269
+ `owners` is empty when no target declares the imported file (`path`,
270
+ repo-relative); otherwise it names every target that does, none of
271
+ which is in the importer's closure.
272
+ """
273
+
274
+ src: str
275
+ dotted: str
276
+ path: str
277
+ owners: frozenset[str]
278
+ verb: str = "imports" # or "runs", for a module run with -m
279
+
280
+ def message(self) -> str:
281
+ if not self.owners:
282
+ return f"{self.src} {self.verb} '{self.dotted}' -- no target declares {self.path}"
283
+ return (
284
+ f"{self.src} {self.verb} '{self.dotted}' "
285
+ f"({', '.join(sorted(self.owners))}), not in deps"
286
+ )
287
+
288
+
289
+ def undeclared_imports(
290
+ target: Target,
291
+ root: Path,
292
+ env: ImportEnv,
293
+ parsed: Iterable[tuple[str, ast.AST]],
294
+ ) -> list[str]:
295
+ """Imports reaching outside the dep closure, as messages; see
296
+ import_problems for the rule and its reasoning."""
297
+ return [p.message() for p in import_problems(target, root, env, parsed)]
298
+
299
+
300
+ def import_problems(
301
+ target: Target,
302
+ root: Path,
303
+ env: ImportEnv,
304
+ parsed: Iterable[tuple[str, ast.AST]],
305
+ ) -> list[ImportProblem]:
306
+ """Imports reaching a target outside this one's declared dep closure.
307
+
308
+ This is what stops `deps` from being decoration. Because targets run
309
+ with the repo root importable, Python resolves a cross-package
310
+ import whether or not the edge is declared -- so an undeclared edge
311
+ is not merely undocumented, it is *absent from the fingerprint*, and
312
+ editing the imported package invalidates nothing. The result is a
313
+ cached PASS that `--force` turns into a FAIL. See docs/dev/TASKS.md H1.
314
+
315
+ Checked against the transitive closure, not direct deps only: the
316
+ stale-input problem is closed as soon as the dependency is folded
317
+ into the fingerprint, which the closure does. Requiring a direct
318
+ edge is a stricter hygiene rule (Bazel's "strict deps") that buys no
319
+ additional correctness here, and would reject lirk's own BUILD
320
+ files.
321
+
322
+ An import resolving to a repo file that *no* target declares is
323
+ reported too, for the same reason and with a different message: an
324
+ undeclared file is fingerprinted by nobody, so it is the same stale
325
+ input with no owner to name. Rejecting rather than fingerprinting it
326
+ implicitly keeps the graph fully explicit, and gets transitivity for
327
+ free -- once the file must be declared, whatever *it* imports falls
328
+ under the rule above (docs/dev/TASKS.md H2).
329
+
330
+ Only the module the dotted path resolves to is checked, not the
331
+ `__init__.py` of each package along the way (see _resolve_module).
332
+ An undeclared *ancestor* package init is therefore still an
333
+ unfingerprinted input; it resolved to nothing across all three real
334
+ repos, and collecting ancestors would turn one clear error into
335
+ several.
336
+ """
337
+ pkg_dir = root / target.package
338
+ root_resolved = root.resolve()
339
+ problems: list[ImportProblem] = []
340
+ seen: set[tuple[str, str]] = set()
341
+
342
+ for src, tree in parsed:
343
+ src_path = (pkg_dir / src).resolve()
344
+ for dotted, base, verb in _imported_modules(tree, src_path):
345
+ bases = (base,) if base is not None else (pkg_dir, root)
346
+ found = None
347
+ for one in bases:
348
+ key = (dotted, one)
349
+ if key not in env.resolved:
350
+ hit = _resolve_module(dotted, [one])
351
+ env.resolved[key] = hit.resolve() if hit is not None else None
352
+ found = env.resolved[key]
353
+ if found is not None:
354
+ break
355
+ if found is None:
356
+ continue
357
+ if not found.is_relative_to(root_resolved):
358
+ continue
359
+ owners = env.owners.get(found, frozenset())
360
+ if owners & env.allowed:
361
+ continue
362
+ if (src, dotted) in seen:
363
+ continue
364
+ seen.add((src, dotted))
365
+ rel = found.relative_to(root_resolved).as_posix()
366
+ problems.append(ImportProblem(src, dotted, rel, owners, verb))
367
+
368
+ return problems
369
+
370
+
371
+ def validate_target(
372
+ target: Target, root: Path, env: ImportEnv | None = None
373
+ ) -> ActionResult:
374
+ """'Build' a target: confirm its declared source files exist, parse
375
+ as syntactically valid Python, and -- when `env` is supplied --
376
+ import only from the targets this one declares. No compilation step
377
+ beyond that (v1 has no bytecode/artifact output).
378
+
379
+ `env` is None only when a target is validated with no repo context
380
+ around it (unit tests do this); the CLI always supplies one, so the
381
+ import check is not optional in a real run.
382
+ """
383
+ missing = missing_files(target, root)
384
+ if missing:
385
+ return ActionResult(
386
+ target.label, False, f"missing source file(s): {', '.join(missing)}"
387
+ )
388
+
389
+ # A pytest target's conftest.py files are parsed and import-checked
390
+ # like srcs: pytest imports them before any test, and they are
391
+ # fingerprinted implicitly (cache.compute_fingerprints), so an import
392
+ # they make outside the closure is the same stale input as one a src
393
+ # makes. Named relative to the package ("../conftest.py") so the
394
+ # message points at the right file and undeclared_imports resolves
395
+ # it from the package directory like any src.
396
+ parsed, error = _parse_sources(target, sources_to_parse(target, root))
397
+ if error is not None:
398
+ return error
399
+
400
+ if env is not None:
401
+ # Reuses the trees parsed above rather than re-reading the srcs:
402
+ # syntax validity is a precondition of the import check anyway,
403
+ # so a second parse would only be a second chance to disagree.
404
+ undeclared = undeclared_imports(target, root, env, parsed)
405
+ if undeclared:
406
+ return ActionResult(
407
+ target.label, False, "; ".join(undeclared)
408
+ )
409
+
410
+ return ActionResult(target.label, True, "ok", notes=path_loads(parsed))
411
+
412
+
413
+ # Calls that load Python code from a file path rather than by import.
414
+ # Their argument is almost always computed (`HERE / "runner.py"`), so the
415
+ # file can't be resolved statically -- but the call itself can be seen.
416
+ _PATH_LOADERS = {
417
+ "spec_from_file_location", "SourceFileLoader", "SourcelessFileLoader",
418
+ "run_path", "load_source",
419
+ }
420
+
421
+
422
+ def path_loads(parsed: Iterable[tuple[str, ast.AST]]) -> list[str]:
423
+ """A note for each src that loads code by file path.
424
+
425
+ lirk can't follow such a load, so its target may depend on code that
426
+ no `deps` entry names -- and then editing that code leaves dependent
427
+ tests cached and green. Found in terminal-projects, where
428
+ furminal/runner.py loads region-explorer by path: a stale PASS that
429
+ `lirk verify` caught and plain `lirk test` couldn't. A note rather
430
+ than a failure, because a correctly declared target looks the same;
431
+ it is printed only when the target is rebuilt, i.e. when the file
432
+ changes, not on every run.
433
+ """
434
+ notes = []
435
+ for src, tree in parsed:
436
+ calls = sorted({
437
+ name
438
+ for node in ast.walk(tree)
439
+ if isinstance(node, ast.Call)
440
+ for name in [
441
+ node.func.attr if isinstance(node.func, ast.Attribute)
442
+ else node.func.id if isinstance(node.func, ast.Name)
443
+ else None
444
+ ]
445
+ if name in _PATH_LOADERS
446
+ })
447
+ if calls:
448
+ notes.append(
449
+ f"{src} loads code by file path ({', '.join(calls)}); lirk can't "
450
+ "follow that, so make sure this target's deps include whatever "
451
+ "it loads (`lirk verify --mutate` can check)"
452
+ )
453
+ return notes
454
+
455
+
456
+ def parse_target(
457
+ target: Target, root: Path
458
+ ) -> tuple[list[tuple[str, ast.AST]], ActionResult | None]:
459
+ """Parse everything validate_target would, without the import check:
460
+ the (name, tree) pairs, or the failure that stopped parsing."""
461
+ missing = missing_files(target, root)
462
+ if missing:
463
+ return [], ActionResult(
464
+ target.label, False, f"missing source file(s): {', '.join(missing)}"
465
+ )
466
+ return _parse_sources(target, sources_to_parse(target, root))
467
+
468
+
469
+ def sources_to_parse(target: Target, root: Path) -> list[tuple[str, Path]]:
470
+ """(name, path) of everything validate_target parses for `target`:
471
+ its srcs, plus a pytest target's conftest modules (see there)."""
472
+ pkg_dir = root / target.package
473
+ to_parse = [(src, pkg_dir / src) for src in target.srcs]
474
+ to_parse += [
475
+ (os.path.relpath(path, pkg_dir.resolve()).replace(os.sep, "/"), path)
476
+ for path in pytest_inputs(target, root)
477
+ if path.name == "conftest.py"
478
+ ]
479
+ return to_parse
480
+
481
+
482
+ def _parse_sources(
483
+ target: Target, to_parse: Sequence[tuple[str, Path]]
484
+ ) -> tuple[list[tuple[str, ast.AST]], ActionResult | None]:
485
+ """Parse each (name, path); the trees, or the first failure."""
486
+ parsed: list[tuple[str, ast.AST]] = []
487
+ for src, src_path in to_parse:
488
+ try:
489
+ # Decoded as UTF-8 explicitly, not via the locale encoding:
490
+ # Python source is UTF-8 by default (PEP 3120), and a
491
+ # locale-dependent read makes this check platform-dependent
492
+ # -- a single-byte locale like cp1252 decodes arbitrary
493
+ # bytes, so a binary file reaches ast.parse and reports a
494
+ # confusing syntax error instead of "not readable".
495
+ tree = ast.parse(
496
+ src_path.read_text(encoding="utf-8"), filename=str(src_path)
497
+ )
498
+ except SyntaxError as e:
499
+ return parsed, ActionResult(
500
+ target.label, False, f"{src}: syntax error: {e}"
501
+ )
502
+ except (UnicodeDecodeError, ValueError) as e:
503
+ return parsed, ActionResult(
504
+ target.label, False, f"{src}: not readable as Python source: {e}"
505
+ )
506
+ parsed.append((src, tree))
507
+ return parsed, None
508
+
509
+
510
+ def run_test(target: Target, root: Path, env: ImportEnv | None = None) -> ActionResult:
511
+ """Run a test target's source files via `python3 -m unittest`, or
512
+ `python3 -m pytest` for a target declaring `runner = "pytest"`.
513
+
514
+ Each src gets the target's own `timeout` if it declares one, else
515
+ TEST_TIMEOUT_SECONDS. Read at call time rather than bound as a
516
+ default argument, so a patched module constant is honoured."""
517
+ validation = validate_target(target, root, env)
518
+ if not validation.ok:
519
+ return validation
520
+
521
+ if not target.srcs:
522
+ # Belt-and-braces: _parse_target already rejects a `test`
523
+ # target with no srcs, so this shouldn't be reachable, but a
524
+ # target with zero srcs must never silently report success.
525
+ return ActionResult(target.label, False, "no srcs to run")
526
+
527
+ pkg_dir = root / target.package
528
+ stdout_parts = []
529
+ stderr_parts = []
530
+ failed_modules: list[str] = []
531
+ timed_out: list[str] = []
532
+ ran_nothing: list[str] = []
533
+ missing_runner: list[str] = []
534
+ skipped_whole: list[str] = []
535
+
536
+ timeout = target.timeout if target.timeout is not None else TEST_TIMEOUT_SECONDS
537
+
538
+ child_env = os.environ.copy()
539
+ root_str = str(root.resolve())
540
+ existing_pythonpath = child_env.get("PYTHONPATH")
541
+ child_env["PYTHONPATH"] = (
542
+ f"{root_str}{os.pathsep}{existing_pythonpath}"
543
+ if existing_pythonpath
544
+ else root_str
545
+ )
546
+
547
+ for src in target.srcs:
548
+ # Dotted path relative to the package, not just the stem: a src
549
+ # in a subdirectory ("sub/test_nested.py") must run as
550
+ # `sub.test_nested`, since `-m unittest test_nested` with
551
+ # cwd=pkg_dir cannot find it. Resolves without __init__.py --
552
+ # sub/ is importable as a namespace package (PEP 420).
553
+ # Using the stem alone also silently collided two srcs with the
554
+ # same filename in different subdirectories onto one module.
555
+ module = Path(src).with_suffix("").as_posix().replace("/", ".")
556
+ if target.runner == "pytest":
557
+ # pytest takes the file path rather than a dotted module.
558
+ # `-p no:cacheprovider` stops it writing .pytest_cache into
559
+ # the package: lirk keeps its own cache, and a tool that
560
+ # writes into the source tree as a side effect of a test run
561
+ # is how inputs start changing underneath their fingerprint.
562
+ #
563
+ # --rootdir and --confcutdir pin pytest to the repo root. Left
564
+ # to itself, pytest run from the package directory takes that
565
+ # directory as rootdir and stops loading conftest.py files
566
+ # there -- so a root conftest lirk fingerprints (pytest_inputs)
567
+ # was silently never loaded. Found by the slingshot repo, where
568
+ # a seeding guard in the root conftest was inactive under lirk
569
+ # while its tests passed. confcutdir also stops pytest loading
570
+ # a conftest from *above* the repo, which lirk does not see.
571
+ root_str = str(root.resolve())
572
+ argv = [
573
+ sys.executable, "-m", "pytest", "-p", "no:cacheprovider",
574
+ f"--rootdir={root_str}", f"--confcutdir={root_str}", src,
575
+ ]
576
+ else:
577
+ argv = [sys.executable, "-m", "unittest", module]
578
+ try:
579
+ proc = subprocess.run(
580
+ argv,
581
+ cwd=pkg_dir,
582
+ capture_output=True,
583
+ text=True,
584
+ env=child_env,
585
+ stdin=subprocess.DEVNULL,
586
+ timeout=timeout,
587
+ )
588
+ except subprocess.TimeoutExpired as e:
589
+ # Recorded and carried on, not returned: bailing out here
590
+ # let one hung src hide every src after it, which is the
591
+ # asymmetry accumulating failures was meant to remove. A
592
+ # hung test often does mean the whole target is wedged, but
593
+ # that is a guess, and guessing costs the other results.
594
+ stdout_parts.append(_as_text(e.stdout))
595
+ stderr_parts.append(_as_text(e.stderr))
596
+ failed_modules.append(module)
597
+ timed_out.append(module)
598
+ continue
599
+ stdout_parts.append(proc.stdout)
600
+ stderr_parts.append(proc.stderr)
601
+ # A module with no tests exits 5 (NO TESTS RAN) on 3.12+, but
602
+ # exits 0 on 3.11 -- which pyproject.toml still supports -- so
603
+ # a green exit code alone reports a false PASS for a test file
604
+ # that tests nothing. The "Ran 0 tests" summary line is written
605
+ # on every version, so it is the portable signal.
606
+ #
607
+ # Checked independently of the exit code rather than as a
608
+ # fallback for a zero one, so the reason is named on 3.12 too,
609
+ # where the exit code already catches it but explains nothing.
610
+ # It can only ever add to the failure, never excuse one.
611
+ if target.runner == "pytest":
612
+ # pytest's own, version-stable signal: exit 5 means nothing
613
+ # was collected. Unlike unittest on 3.11, it never exits 0
614
+ # for an empty module, so the exit code is enough -- except
615
+ # that a module which skipped *itself* (a module-level
616
+ # `pytest.importorskip("numpy")`) exits 5 too. That file has
617
+ # tests and chose not to run them here, which is a pass, just
618
+ # like a test skipped at function level; pytest says so in
619
+ # its summary ("1 skipped"), where an empty file says "no
620
+ # tests ran". The Python-environment fingerprint reruns it
621
+ # once the missing package is installed, so no stale PASS.
622
+ if proc.returncode == 5 and _pytest_skipped_module(proc.stdout):
623
+ skipped_whole.append(module)
624
+ continue
625
+ ran_no_tests = proc.returncode == 5
626
+ if _pytest_missing(proc.stderr):
627
+ missing_runner.append(module)
628
+ else:
629
+ ran_no_tests = _ran_zero_tests(proc.stderr)
630
+ if proc.returncode != 0 or ran_no_tests:
631
+ failed_modules.append(module)
632
+ if ran_no_tests:
633
+ ran_nothing.append(module)
634
+
635
+ if failed_modules:
636
+ detail = (
637
+ f"{len(failed_modules)} of {len(target.srcs)} src files failed: "
638
+ f"{', '.join(failed_modules)}"
639
+ )
640
+ if timed_out:
641
+ detail += (
642
+ f" -- timed out after {timeout}s: "
643
+ f"{', '.join(timed_out)}"
644
+ )
645
+ if ran_nothing:
646
+ detail += f" -- contained no tests: {', '.join(ran_nothing)}"
647
+ if missing_runner:
648
+ detail += f" -- pytest is not installed for {sys.executable}"
649
+ return ActionResult(
650
+ target.label,
651
+ False,
652
+ detail,
653
+ "\n".join(stdout_parts),
654
+ "\n".join(stderr_parts),
655
+ )
656
+
657
+ return ActionResult(
658
+ target.label, True, "passed", "\n".join(stdout_parts), "\n".join(stderr_parts),
659
+ notes=validation.notes,
660
+ )