From abbfebff3e381f37a5f518704f3010ab661605fb Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Thu, 12 Nov 2026 15:36:46 +0100 Subject: [PATCH] feat: run the CI suite as parallel processes validate.yml and pr_checks.py run scripts/run_tests.py with four jobs, the runner's cores: 24 minutes here for the suite that took about 40 in one process. A failed module's tracebacks are printed in the summary, as a runner keeps no log file. The two scripts added today follow the rules the suite holds every script to (no abbreviated option, run_cli, every option documented), and the two isolation tests drop the per-module scratch variable to check the folder a module gets alone. --- .github/workflows/validate.yml | 5 ++++- CONTRIBUTING.md | 2 +- scripts/find_embedded.py | 8 ++++++-- scripts/pr_checks.py | 2 +- scripts/run_tests.py | 27 +++++++++++++++++++++------ tests/test_audit_regressions.py | 12 ++++++++---- tests/test_contributing_text.py | 6 +++--- tests/test_doc_surfaces.py | 12 ++++++------ tests/test_workflow_paths.py | 2 +- wiki/testing-guide.md | 10 +++++++++- wiki/tools.md | 4 +++- 11 files changed, 63 insertions(+), 27 deletions(-) diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 1086791af..a4156743b 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -332,9 +332,12 @@ jobs: # No test downloads a release asset: 252 MB files fetched into the # shared cache filled the runner's disk and changed what later tests # resolved. The download tests mock the network and clear it. + # One process per module, four at a time on the runner's four cores, + # each with its own scratch folder: a module that leaves a variable + # set no longer changes the ones after it. env: RETROBIOS_OFFLINE: "1" - run: python -m unittest discover -s tests -t . -v + run: python scripts/run_tests.py --jobs 4 - name: Run the full-screen interface tests on Python 3.8 if: ${{ !cancelled() }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ea629d319..f48f7baad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,7 +91,7 @@ python scripts/refresh_data_dirs.py # data directories the python scripts/generate_site.py # the pages the site tests read npm ci --prefix website # the site's packages, Node 24 npm run build --prefix website # the built site -python -m unittest discover -s tests -t . # the suite +python scripts/run_tests.py --jobs 4 # the suite, one process per module python3.8 -m unittest tests.test_install_tui # the full screen on the installer's floor python scripts/validate_site.py --site-dir website/dist # links and page budgets ``` diff --git a/scripts/find_embedded.py b/scripts/find_embedded.py index 476a83638..562370efe 100644 --- a/scripts/find_embedded.py +++ b/scripts/find_embedded.py @@ -86,7 +86,8 @@ def sources(paths: list[Path]) -> Iterator[tuple[str, bytes]]: def main() -> int: - parser = argparse.ArgumentParser(description=__doc__.split("\n\n", 1)[0]) + parser = argparse.ArgumentParser(allow_abbrev=False, + description=__doc__.split("\n\n", 1)[0]) parser.add_argument("paths", nargs="+", type=Path, help="files, folders or ZIPs to search") parser.add_argument("--crc32", required=True, help="crc32 of the wanted file, in hex") parser.add_argument("--size", required=True, type=int, help="size of the wanted file, in bytes") @@ -125,4 +126,7 @@ def main() -> int: if __name__ == "__main__": - sys.exit(main()) + sys.path.insert(0, str(Path(__file__).resolve().parent)) + from common import run_cli + + run_cli(main) diff --git a/scripts/pr_checks.py b/scripts/pr_checks.py index 4a4b838e2..5855469a3 100644 --- a/scripts/pr_checks.py +++ b/scripts/pr_checks.py @@ -60,7 +60,7 @@ STEPS: tuple[Step, ...] = ( # filled it and every later command failed with ENOSPC. Step("npm run build --prefix website", "the built site", needs="npm", provider="Node 24", env={"NODE_DISABLE_COMPILE_CACHE": "1"}), - Step("python -m unittest discover -s tests -t .", "the suite", + Step("python scripts/run_tests.py --jobs 4", "the suite, one process per module", env={"RETROBIOS_OFFLINE": "1"}), Step("python3.8 -m unittest tests.test_install_tui", "the full screen on the installer's floor", needs="python3.8", provider="a Python 3.8 with pyte", diff --git a/scripts/run_tests.py b/scripts/run_tests.py index 746fa3415..ad574c8ed 100644 --- a/scripts/run_tests.py +++ b/scripts/run_tests.py @@ -6,12 +6,12 @@ Usage: python scripts/run_tests.py --jobs 1 # one at a time, as discover runs python scripts/run_tests.py tests.test_install tests.test_e2e -`python -m unittest discover` runs 8,500 tests in one process: about 40 -minutes here and 85 on the CI runner, and a module that left a variable or a -flag set changed the modules after it. Here each module gets a fresh process +unittest's discovery runs 8,500 tests in one process: about 40 minutes +here and 85 on the CI runner, and a module that left a variable or a flag +set changed the modules after it. Here each module gets a fresh process and its own scratch folder (RETROBIOS_TEST_SCRATCH, read by tests/__init__), the longest first so the last ones to start are short. Durations are kept in -tmp/test-durations.json, from the previous run; a module never timed is +tmp/test-durations.json, from the previous run, and a module never timed is weighed by its size. The host computes wrong and Python segfaults under load: a module killed by @@ -41,6 +41,8 @@ DURATIONS = ROOT / "tmp" / "test-durations.json" # third of the cores at once made them fail for no other reason. DEFAULT_JOBS = max(1, min(6, (os.cpu_count() or 2) // 3)) +# Lines of a failed module's tracebacks printed in the summary. +FAILURE_LINES = 200 _RAN = re.compile(r"^Ran (\d+) tests? in ([\d.]+)s$", re.M) _COUNTS = re.compile(r"(failures|errors|skipped|expected failures|unexpected successes)=(\d+)") @@ -117,7 +119,8 @@ def run_module(module: str, env: dict[str, str], logs: Path) -> Result: def main() -> int: - parser = argparse.ArgumentParser(description=__doc__.split("\n\n", 1)[0]) + parser = argparse.ArgumentParser(allow_abbrev=False, + description=__doc__.split("\n\n", 1)[0]) parser.add_argument("modules", nargs="*", help="modules to run (tests.test_x), all by default") parser.add_argument("--jobs", "-j", type=int, default=DEFAULT_JOBS, help=f"modules at once (default {DEFAULT_JOBS})") @@ -169,9 +172,21 @@ def main() -> int: print(f"FAILED {result.module}: {result.failures} failures, {result.errors} errors " f"(code {result.code if result.crashed_then is None else result.crashed_then}), " f"log {result.log.relative_to(ROOT) if result.log else '-'}") + # A CI runner shows the output and keeps no file: what failed, and + # why, is said here, the tracebacks unittest wrote, at most a page. + if result.log is not None: + text = result.log.read_text(encoding="utf-8", errors="replace") + start = text.find("\n" + "=" * 70) + shown = (text[start:] if start >= 0 else text).strip().splitlines() + for line in shown[:FAILURE_LINES]: + print(f" {line}") + if len(shown) > FAILURE_LINES: + print(f" ... {len(shown) - FAILURE_LINES} more lines in the log") print("OK" if not failed else f"FAILED ({len(failed)} modules)") return 1 if failed else 0 if __name__ == "__main__": - sys.exit(main()) + from common import run_cli + + run_cli(main) diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py index 30fdd4598..0f9ac840e 100644 --- a/tests/test_audit_regressions.py +++ b/tests/test_audit_regressions.py @@ -241,11 +241,14 @@ class AModuleRunAloneHasItsScratchDirectory(unittest.TestCase): (clone / "tests" / "__init__.py").write_bytes( (ROOT / "tests" / "__init__.py").read_bytes() ) + # The folder a module gets alone, not the one scripts/run_tests.py + # names for each module it runs beside others. + env = {k: v for k, v in os.environ.items() if k != "RETROBIOS_TEST_SCRATCH"} proc = subprocess.run( [sys.executable, "-I", "-c", "import sys; sys.path.insert(0, '.'); import tests, tempfile, os;" " print(tempfile.gettempdir()); print(os.environ['TMPDIR'])"], - cwd=clone, capture_output=True, text=True, timeout=60, check=False, + cwd=clone, env=env, capture_output=True, text=True, timeout=60, check=False, ) self.assertEqual(proc.returncode, 0, proc.stderr) self.assertTrue((clone / "tmp" / "tests").is_dir()) @@ -278,7 +281,8 @@ class EveryTestModuleImportsTheIsolation(unittest.TestCase): def test_discover_without_a_top_level_runs_isolated(self): env = {k: v for k, v in os.environ.items() - if k not in ("RETROBIOS_OFFLINE", "TMPDIR", "RETROBIOS_CACHE")} + if k not in ("RETROBIOS_OFFLINE", "TMPDIR", "RETROBIOS_CACHE", + "RETROBIOS_TEST_SCRATCH")} proc = subprocess.run( [sys.executable, "-c", "import os, sys, tempfile, unittest\n" @@ -402,7 +406,7 @@ class WorkflowRegressions(unittest.TestCase): name for name, body in workflow["jobs"].items() if any( - "unittest discover" in str(step.get("run", "")) + "scripts/run_tests.py" in str(step.get("run", "")) for step in body.get("steps", []) ) ), @@ -4794,7 +4798,7 @@ class TheTestJobHasTheDataTrees(unittest.TestCase): )["jobs"]["run-tests"]["steps"] runs = [step.get("run", "") for step in steps] fetch = next(i for i, run in enumerate(runs) if "refresh_data_dirs.py" in run) - suite = next(i for i, run in enumerate(runs) if "unittest discover" in run) + suite = next(i for i, run in enumerate(runs) if "scripts/run_tests.py" in run) self.assertLess(fetch, suite) diff --git a/tests/test_contributing_text.py b/tests/test_contributing_text.py index 8e435f8b5..8049a11e3 100644 --- a/tests/test_contributing_text.py +++ b/tests/test_contributing_text.py @@ -68,7 +68,7 @@ class BeforeOpeningAPullRequest(unittest.TestCase): section = text.split("## Before opening a PR", 1)[1].split("\n## ", 1)[0] block = "".join(_re.findall(r"```bash\n(.*?)```", section, _re.S)) for command in ("validate_pr.py --changed", "validate_schemas.py --source-only", - "unittest discover"): + "run_tests.py"): self.assertIn(command, block) self.assertNotIn("pipeline.py", block) workflow = (REPO_ROOT / ".github" / "workflows" / "validate.yml").read_text(encoding="utf-8") @@ -268,13 +268,13 @@ class OneCommandRunsTheChecks(unittest.TestCase): workflow = (REPO_ROOT / ".github" / "workflows" / "validate.yml").read_text(encoding="utf-8") order = [workflow.index(key) for key in ( "refresh_data_dirs.py", "run: python scripts/generate_site.py", - "npm run build --prefix website", "python -m unittest discover", + "npm run build --prefix website", "python scripts/run_tests.py", "validate_site.py")] self.assertEqual(order, sorted(order)) commands = [step.command for step in pr_checks.STEPS] positions = [next(i for i, c in enumerate(commands) if key in c) for key in ( "refresh_data_dirs.py", "generate_site.py", "npm run build", - "unittest discover", "validate_site.py")] + "run_tests.py", "validate_site.py")] self.assertEqual(positions, sorted(positions)) def test_the_guide_gives_the_runner_and_what_it_runs(self): diff --git a/tests/test_doc_surfaces.py b/tests/test_doc_surfaces.py index 31bfff25a..4286c03da 100644 --- a/tests/test_doc_surfaces.py +++ b/tests/test_doc_surfaces.py @@ -397,14 +397,14 @@ class ContributorChecksMatchCi(unittest.TestCase): block = _section(text, "## Before opening a PR") self.assertIn("python scripts/refresh_data_dirs.py", block) self.assertLess( - block.index("refresh_data_dirs.py"), block.index("unittest discover") + block.index("refresh_data_dirs.py"), block.index("run_tests.py") ) def test_ci_does_the_same(self): workflow = _read(".github/workflows/validate.yml") self.assertLess( workflow.index("python scripts/refresh_data_dirs.py"), - workflow.index("python -m unittest discover"), + workflow.index("python scripts/run_tests.py"), ) def test_the_pr_checklist_generates_the_site_before_the_tests(self): @@ -413,7 +413,7 @@ class ContributorChecksMatchCi(unittest.TestCase): workflow = _read(".github/workflows/validate.yml") self.assertLess( workflow.index("run: python scripts/generate_site.py"), - workflow.index("python -m unittest discover"), + workflow.index("python scripts/run_tests.py"), ) for text in ( contributing_text(lambda page: page, "# C", "here"), @@ -422,7 +422,7 @@ class ContributorChecksMatchCi(unittest.TestCase): block = _section(text, "## Before opening a PR") self.assertIn("python scripts/generate_site.py", block) self.assertLess( - block.index("generate_site.py"), block.index("unittest discover") + block.index("generate_site.py"), block.index("run_tests.py") ) @@ -626,7 +626,7 @@ class ContributorChecksFetchThePlatformsFiles(unittest.TestCase): workflow = _read(".github/workflows/validate.yml") self.assertLess( workflow.index("python scripts/export_native.py --all --refresh-cache"), - workflow.index("python -m unittest discover"), + workflow.index("python scripts/run_tests.py"), ) for text in ( contributing_text(lambda page: page, "# C", "here"), @@ -635,7 +635,7 @@ class ContributorChecksFetchThePlatformsFiles(unittest.TestCase): block = _section(text, "## Before opening a PR") self.assertIn("python scripts/export_native.py --all --refresh-cache", block) self.assertLess( - block.index("export_native.py"), block.index("unittest discover") + block.index("export_native.py"), block.index("run_tests.py") ) def test_the_testing_guide_names_the_command(self): diff --git a/tests/test_workflow_paths.py b/tests/test_workflow_paths.py index d97d44fe3..069954f3e 100644 --- a/tests/test_workflow_paths.py +++ b/tests/test_workflow_paths.py @@ -147,7 +147,7 @@ class ThePullRequestBuildsTheSite(unittest.TestCase): generate = next( i for i, run in enumerate(runs) if "scripts/generate_site.py" in run ) - suite = next(i for i, run in enumerate(runs) if "unittest discover" in run) + suite = next(i for i, run in enumerate(runs) if "scripts/run_tests.py" in run) self.assertLess(generate, suite) # The built pages too: about fifty tests read website/dist. build = next(i for i, run in enumerate(runs) if "npm run build" in run) diff --git a/wiki/testing-guide.md b/wiki/testing-guide.md index 67bd46fe1..916e96f7d 100644 --- a/wiki/testing-guide.md +++ b/wiki/testing-guide.md @@ -23,6 +23,14 @@ Run the full suite: python -m unittest discover -s tests -t . -v ``` +Or run it as parallel processes, one per module, each with its own +scratch folder. A module a host crash kills runs again alone at the end: + +```bash +python scripts/run_tests.py # a few modules at a time +python scripts/run_tests.py --jobs 1 # one at a time +``` + Run a single module, or several: ```bash @@ -69,7 +77,7 @@ Every module in `tests/`, by the area it guards. | Pack probe | the cores a platform runs for a system, staging grown to a fixed point, runs mapped to pack and manifest entries, the line and its schema, the probe lock per run, that no page shows its lines. One class builds a real pack narrowed to PlayStation and lays it out both ways, from the archive and with `install.py --dest`, and skips without the collection | `test_pack_probe` | | Automation runner, disk cleanup, asset sync and bit-flip repair | fake jobs, git repositories with a bare origin and session worktrees, a committed file with one bit flipped, canned CI runs in place of `gh` | `test_autorun` | | Truth and native export | synthetic + cached originals | `test_export_counts`, `test_export_native_layer`, `test_export_report`, `test_exporters`, `test_truth_data_dir_readers`, `test_truth_diff`, `test_truth_layout` | -| Scrapers, upstream and provenance | inline sources, synthetic | `test_batocera_md5_list`, `test_check_freshness`, `test_cited_lines`, `test_contributing_text`, `test_coverage_cache`, `test_data_dir_refs`, `test_dataclass_defaults`, `test_dedup`, `test_doc_pages`, `test_doc_surfaces`, `test_elec_scrapers`, `test_fbneo_parser`, `test_find_pinned_revision`, `test_hash_merge`, `test_launchers`, `test_launchers_amberelec`, `test_launchers_emudeck`, `test_launchers_emuelec`, `test_launchers_knulli`, `test_launchers_recalbox`, `test_launchers_retrobat`, `test_launchers_retrodeck`, `test_launchers_retropie`, `test_launchers_rocknix`, `test_mame_devices`, `test_mame_parser`, `test_mame_ref_audit`, `test_optional_dependencies`, `test_pin_build`, `test_profile_sync`, `test_prose_style`, `test_provenance`, `test_refresh_data_dirs`, `test_refresh_failures`, `test_refresh_stale`, `test_retired_files`, `test_retrodeck_scraper_errors`, `test_romset_recipes`, `test_schema_semantic_gate`, `test_scraper_contract`, `test_site_agents`, `test_site_arcade_boards`, `test_site_availability`, `test_site_bootstrap`, `test_site_built_gate`, `test_site_components`, `test_site_console_folders`, `test_site_content_data`, `test_site_copy`, `test_site_cores`, `test_site_css`, `test_site_demo`, `test_site_density_cards`, `test_site_emulator_page`, `test_site_emulators`, `test_site_entry`, `test_site_entry_tables`, `test_site_exports`, `test_site_freshness`, `test_site_funnel`, `test_site_images`, `test_site_inputs`, `test_site_insights`, `test_site_integration`, `test_site_nav`, `test_site_page_text`, `test_site_picker`, `test_site_platform_density`, `test_site_platform_hashes`, `test_site_privacy`, `test_site_pronouns`, `test_site_reference`, `test_site_render`, `test_site_search`, `test_site_steps`, `test_site_system_links`, `test_site_system_names`, `test_site_system_sections`, `test_site_systems`, `test_site_validation`, `test_site_weight`, `test_site_wiki`, `test_site_write`, `test_source_ref_targets`, `test_upstream`, `test_upstream_index`, `test_validate_pr`, `test_validate_pr_flow`, `test_validate_pr_hashes`, `test_version_basis`, `test_workflow_paths` | +| Scrapers, upstream and provenance | inline sources, synthetic | `test_auto_fetch_lookup`, `test_batocera_md5_list`, `test_find_embedded`, `test_check_freshness`, `test_cited_lines`, `test_contributing_text`, `test_coverage_cache`, `test_data_dir_refs`, `test_dataclass_defaults`, `test_dedup`, `test_doc_pages`, `test_doc_surfaces`, `test_elec_scrapers`, `test_fbneo_parser`, `test_find_pinned_revision`, `test_hash_merge`, `test_launchers`, `test_launchers_amberelec`, `test_launchers_emudeck`, `test_launchers_emuelec`, `test_launchers_knulli`, `test_launchers_recalbox`, `test_launchers_retrobat`, `test_launchers_retrodeck`, `test_launchers_retropie`, `test_launchers_rocknix`, `test_mame_devices`, `test_mame_parser`, `test_mame_ref_audit`, `test_optional_dependencies`, `test_pin_build`, `test_profile_sync`, `test_prose_style`, `test_provenance`, `test_refresh_data_dirs`, `test_refresh_failures`, `test_refresh_stale`, `test_retired_files`, `test_retrodeck_scraper_errors`, `test_romset_recipes`, `test_schema_semantic_gate`, `test_scraper_contract`, `test_site_agents`, `test_site_arcade_boards`, `test_site_availability`, `test_site_bootstrap`, `test_site_built_gate`, `test_site_components`, `test_site_console_folders`, `test_site_content_data`, `test_site_copy`, `test_site_cores`, `test_site_css`, `test_site_demo`, `test_site_density_cards`, `test_site_emulator_page`, `test_site_emulators`, `test_site_entry`, `test_site_entry_tables`, `test_site_exports`, `test_site_freshness`, `test_site_funnel`, `test_site_images`, `test_site_inputs`, `test_site_insights`, `test_site_integration`, `test_site_nav`, `test_site_page_text`, `test_site_picker`, `test_site_platform_density`, `test_site_platform_hashes`, `test_site_privacy`, `test_site_pronouns`, `test_site_reference`, `test_site_render`, `test_site_search`, `test_site_steps`, `test_site_system_links`, `test_site_system_names`, `test_site_system_sections`, `test_site_systems`, `test_site_validation`, `test_site_weight`, `test_site_wiki`, `test_site_write`, `test_source_ref_targets`, `test_upstream`, `test_upstream_index`, `test_validate_pr`, `test_validate_pr_flow`, `test_validate_pr_hashes`, `test_version_basis`, `test_workflow_paths` | | Games on the card | small DATs, listxml and setup sources, a fake fetcher, fixture install lists, and the committed data against its schemas | `test_arcade_parents`, `test_game_catalogs`, `test_game_headers`, `test_game_regions`, `test_games_index`, `test_romfolders_card`, `test_romfolders_es` | ## Test architecture diff --git a/wiki/tools.md b/wiki/tools.md index da056c143..6468a6563 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -1397,7 +1397,9 @@ Further options, by script: | Script | Options | |--------|---------| -| `auto_fetch.py` | `--create-issues` prints a GitHub issue body per platform for the files still missing, with their full hashes. It posts nothing | +| `auto_fetch.py` | `--create-issues` prints a GitHub issue body per platform for the files still missing, with their full hashes. It posts nothing. `--sha1` looks a file up by its hash alone, in the database, Software Heritage and archive.org, and writes what it finds to `tmp/acquire/` | +| `find_embedded.py` | `--crc32` and `--size` name the file looked for inside other files (every offset, ZIP members read), `--sha1` confirms a hit, `--extract` writes a confirmed one to `tmp/acquire/` | +| `run_tests.py` | `--jobs` sets how many test modules run at once (default a third of the cores, at most 6) | | `check_buildbot_system.py` | `--update` refreshes the entries found stale, `--json`, `--registry` | | `check_freshness.py` | `--cache-dir`, `--native-cache-dir` (originals of the native export) and `--truth-dir` move the caches it reads | | `check_release.py` | `--tag`, `--sums dist/SHA256SUMS.txt`, `--record` (default `release.json`), `--json` |