Skip to content

The agent that grows with you

fix(tests): let run_tests_parallel take the explicit file list from a file

liuhao10248828e356f778
diff --git a/scripts/run_tests_parallel.py b/scripts/run_tests_parallel.py
index 9f7622daaf7..5fbc91de55b 100755
--- a/scripts/run_tests_parallel.py
+++ b/scripts/run_tests_parallel.py
@@ -162,6 +162,29 @@ def _split_pathspec(value: str) -> List[str]:
# this runner never executes them, by construction. The summary calls that
# out explicitly so a local run isn't misread as covering macOS/Windows
# behaviour, and names the CI lane where those tests actually execute.
+
+
+def _read_files_from(spec: str) -> List[str]:
+ """Read an explicit test-file list from *spec* - a path, or ``-`` for stdin.
+
+ One path per line, blank lines ignored. This is the file-backed
+ counterpart of ``--files`` for lists that exceed the kernel's
+ per-argument cap (``MAX_ARG_STRLEN``, 128 KiB on Linux): the
+ whole-suite list is already ~210 KB, so passing it as one ``--files``
+ argv element dies with ``E2BIG`` in ``execve`` before the runner's
+ first line can even run.
+ """
+ if spec == "-":
+ text = sys.stdin.read()
+ else:
+ try:
+ text = Path(spec).read_text(encoding="utf-8")
+ except OSError as exc:
+ print(f"error: --files-from: cannot read {spec!r}: {exc}", file=sys.stderr)
+ sys.exit(2)
+ return [line.strip() for line in text.splitlines() if line.strip()]
+
+
_OS_MARKERS = {
"linux_only": ("linux", "the main Linux CI lane"),
"macos_only": ("darwin", "the tests-os CI lane (macos-latest)"),
@@ -997,7 +1020,20 @@ def main() -> int:
"Explicit colon-separated list of test files to run (on "
"Windows, ';' also separates and drive letters are kept "
"intact). Bypasses discovery entirely — used by CI matrix "
- "jobs that receive their file list from the generate job."
+ "jobs that receive their file list from the generate job. "
+ "A whole-suite list (~210 KB) exceeds the kernel's "
+ "per-argument cap (MAX_ARG_STRLEN, 128 KiB) and dies with "
+ "E2BIG before this script starts — pass it via --files-from."
+ ),
+ )
+ parser.add_argument(
+ "--files-from",
+ metavar="PATH",
+ help=(
+ "Read the explicit list of test files from PATH (one per "
+ "line, blank lines ignored), or from stdin when PATH is '-'. "
+ "File-backed counterpart of --files for lists that exceed "
+ "the per-argv-element cap; mutually exclusive with --files."
),
)
parser.add_argument(
@@ -1029,6 +1065,7 @@ def main() -> int:
OUR_FLAGS = {
"-h", "--help", "-j", "--jobs", "--paths", "--include-integration",
"--file-timeout", "--file-retries", "--slice", "--generate-slices", "--files",
+ "--files-from",
}
# pytest short flags that consume the NEXT token as their value.
PYTEST_VALUE_FLAGS = {"-k", "-m", "-p", "-o", "-c", "-r", "-W"}
@@ -1047,6 +1084,13 @@ def main() -> int:
return False
argv = sys.argv[1:]
+ # argparse treats a bare "-" as a positional, so "--files-from -"
+ # would die with "expected one argument" before our code ever runs.
+ # Normalize to the "="-joined form, which argparse accepts.
+ if "--files-from" in argv:
+ i = argv.index("--files-from")
+ if i + 1 < len(argv) and argv[i + 1] == "-":
+ argv[i : i + 2] = ["--files-from=-"]
if "--" in argv:
sep = argv.index("--")
before, explicit_passthrough = argv[:sep], argv[sep + 1 :]
@@ -1138,10 +1182,19 @@ def main() -> int:
repo_root = Path(__file__).resolve().parent.parent
- # --files: explicit file list from the CI generate job — skip discovery.
+ # --files / --files-from: explicit file list (argv or file-backed) from
+ # the CI generate job — skip discovery.
+ if args.files and args.files_from:
+ print(
+ "error: --files and --files-from are mutually exclusive", file=sys.stderr
+ )
+ sys.exit(2)
if args.files:
files = [repo_root / f for f in _split_pathspec(args.files)]
roots = []
+ elif args.files_from:
+ files = [repo_root / f for f in _read_files_from(args.files_from)]
+ roots = []
else:
# Resolve discovery roots: positional path args override --paths if any
# were supplied, otherwise --paths (which itself defaults to 'tests').
diff --git a/tests/scripts/test_run_tests_parallel.py b/tests/scripts/test_run_tests_parallel.py
index 56195b77f03..85fd309f865 100644
--- a/tests/scripts/test_run_tests_parallel.py
+++ b/tests/scripts/test_run_tests_parallel.py
@@ -232,7 +232,8 @@ def test_grandchild_leak_is_killed_by_runner(tmp_path: Path) -> None:
#
# The runner routes any token starting with ``-`` that isn't one of its own
# options (``-j``/``--jobs``, ``--paths``, ``--slice``, ``--file-timeout``,
-# ``--generate-slices``, ``--files``, ``--include-integration``) straight
+# ``--generate-slices``, ``--files``, ``--include-integration``,
+# ``--files-from``) straight
# through to each per-file pytest invocation — no ``--`` separator required.
# Before this, a bare ``-q`` errored out with "unrecognized arguments",
# forcing a retry on every run. These tests are behavior contracts, not
@@ -551,3 +552,79 @@ def test_interpreter_crash_is_reported_as_a_crash_not_as_no_tests_ran(tmp_path:
assert "SIGSEGV" in proc.stdout
assert "where no tests ran" not in proc.stdout
assert "NO TESTS RAN" not in proc.stdout
+
+
+# ── --files-from: file-backed explicit file lists ───────────────────────────
+#
+# --files carries the whole list as ONE argv element, and Linux caps a
+# single argument at MAX_ARG_STRLEN (128 KiB) — a much smaller limit than
+# ARG_MAX. The whole-suite list (~210 KB) dies with E2BIG in execve before
+# the runner's first line runs. --files-from takes the same explicit list
+# from a file (or stdin via '-'), one path per line, so the list is bounded
+# by the filesystem instead of one argv element.
+
+
+def test_files_from_runs_exactly_the_listed_files(tmp_path: Path) -> None:
+ """A newline-separated list file bypasses discovery like --files."""
+ probe_dir = _make_probe_dir(tmp_path)
+ extra = tmp_path / "probe_extra"
+ extra.mkdir()
+ (extra / "test_extra.py").write_text("def test_extra():\n assert True\n")
+
+ list_file = tmp_path / "files.txt"
+ list_file.write_text(
+ f"{probe_dir / 'test_flagprobe.py'}\n\n{extra / 'test_extra.py'}\n",
+ encoding="utf-8",
+ )
+
+ # --paths points at the probe dir too; an explicit list must win and
+ # NOT discover the extra file by accident.
+ proc = _run_runner(probe_dir, "--files-from", str(list_file), "-q")
+ assert proc.returncode == 0, proc.stdout
+ assert "Running 2 test files" in proc.stdout, proc.stdout
+ assert "3 passed" not in proc.stdout, proc.stdout
+
+
+def test_files_from_dash_reads_the_list_from_stdin(tmp_path: Path) -> None:
+ """--files-from - reads the list from stdin."""
+ probe_dir = _make_probe_dir(tmp_path)
+
+ repo_root = Path(__file__).resolve().parent.parent.parent
+ runner = repo_root / "scripts" / "run_tests_parallel.py"
+ proc = subprocess.run(
+ [sys.executable, str(runner), "--files-from", "-",
+ "-j", "1", "--file-timeout", "30", "-q"],
+ input=f"{probe_dir / 'test_flagprobe.py'}\n",
+ cwd=repo_root, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
+ encoding="utf-8", errors="replace", timeout=60,
+ )
+ assert proc.returncode == 0, proc.stdout
+ assert "Running 1 test files" in proc.stdout, proc.stdout
+ assert "✓2" in proc.stdout or "2 passed" in proc.stdout, proc.stdout
+
+
+def test_files_and_files_from_are_mutually_exclusive(tmp_path: Path) -> None:
+ """Both explicit-list flags at once is a usage error, not a silent pick."""
+ probe_dir = _make_probe_dir(tmp_path)
+ list_file = tmp_path / "files.txt"
+ list_file.write_text(f"{probe_dir / 'test_flagprobe.py'}\n", encoding="utf-8")
+
+ proc = _run_runner(
+ probe_dir, "--files", str(probe_dir / "test_flagprobe.py"),
+ "--files-from", str(list_file),
+ )
+ assert proc.returncode == 2, proc.stdout
+ assert "mutually exclusive" in proc.stdout, proc.stdout
+ assert "Running" not in proc.stdout, proc.stdout
+
+
+def test_files_from_missing_file_errors_cleanly(tmp_path: Path) -> None:
+ """An unreadable list file fails with one clear line, not a traceback."""
+ probe_dir = _make_probe_dir(tmp_path)
+ missing = tmp_path / "no-such-list.txt"
+
+ proc = _run_runner(probe_dir, "--files-from", str(missing))
+ assert proc.returncode == 2, proc.stdout
+ assert f"error: --files-from: cannot read {str(missing)!r}" in proc.stdout
+ assert "Traceback" not in proc.stdout, proc.stdout
+ assert "Running" not in proc.stdout, proc.stdout