From 16d2d85f111affc02de35a0943185b4b77a64c7a Mon Sep 17 00:00:00 2001 From: Vito Date: Sun, 22 Nov 2026 11:17:01 -0800 Subject: [PATCH] fix(ci): a docs-only push runs just the tests that read the docs (#679) (#681) The pre-push hook ran the whole Python suite for a README edit. Only tests.test_brand_assets and tests.test_package_contents open docs-class files, so a docs push now runs those two. An inert push still runs nothing and every other push still runs the full suite. --- .pre-commit-config.yaml | 3 ++- TESTING.md | 3 ++- tests/test_ci_change_class.py | 32 ++++++++++++++++++++++++++++++-- tools/ci/change_class.py | 11 +++++++++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1291152e..90875822 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -22,7 +22,8 @@ repos: types: [python] exclude: ^(tests/|reference/) - # Skipped when the pushed commits only change inert files (tools/ci/change_class.py, #639). + # Skipped for an inert push; a docs push runs only the two tests that read the docs + # (tools/ci/change_class.py, #639, #679). - id: tests name: tests entry: python tools/ci/change_class.py --unless-inert python -m tests.run_suite -q diff --git a/TESTING.md b/TESTING.md index ca53d21d..c4a17887 100644 --- a/TESTING.md +++ b/TESTING.md @@ -875,7 +875,8 @@ else by the next code change's gate. Run editing those files. The pre-push hook uses the same script: it skips the Python suite when the -pushed commits are `inert` and runs it otherwise. +pushed commits are `inert`, runs only those two test modules when they are +`docs` (#679), and runs the whole suite otherwise. ## Required merge checks diff --git a/tests/test_ci_change_class.py b/tests/test_ci_change_class.py index f10fda81..59dafe43 100644 --- a/tests/test_ci_change_class.py +++ b/tests/test_ci_change_class.py @@ -288,15 +288,43 @@ class TestCommandLine(unittest.TestCase): self.assertEqual(result.returncode, 0, result.stderr) self.assertFalse(marker.exists()) self.assertIn("inert", result.stdout) - # No range from pre-commit (a manual run) and a docs push both run it. + # No range from pre-commit (a manual run) and a code push both run it. self.assertEqual(self.run_script(*command).returncode, 0) self.assertTrue(marker.exists()) marker.unlink() - self.commit({"README.md": "r3\n"}) + self.commit({"py/x.py": "x = 1\n"}) refs["PRE_COMMIT_TO_REF"] = self.head() self.assertEqual(self.run_script(*command, **refs).returncode, 0) self.assertTrue(marker.exists()) + def test_a_docs_push_runs_only_the_tests_that_read_the_docs(self): + # #679: the full suite took minutes for a README edit. + ran = unittest.mock.Mock(return_value=unittest.mock.Mock(returncode=5)) + with unittest.mock.patch.dict(os.environ, _isolated_environment(), clear=True): + with ( + unittest.mock.patch.object( + change_class, "class_of_range", return_value="docs" + ), + unittest.mock.patch.object(change_class.subprocess, "run", ran), + ): + code = change_class.main(["--unless-inert", "python", "-m", "full"]) + ran.assert_called_once_with( + [ + sys.executable, + "-m", + "unittest", + "tests.test_brand_assets", + "tests.test_package_contents", + ], + check=False, + ) + self.assertEqual(code, 5) + + def test_the_docs_tests_named_by_the_hook_exist(self): + for module in change_class.DOCS_TESTS: + path = ROOT / (module.replace(".", "/") + ".py") + self.assertTrue(path.is_file(), module) + def test_the_pre_push_command_is_found_through_path(self): # On Windows a bare "npm" is npm.cmd: only a PATH lookup with PATHEXT finds it. found = unittest.mock.Mock(return_value=unittest.mock.Mock(returncode=3)) diff --git a/tools/ci/change_class.py b/tools/ci/change_class.py index 96653d7d..af99aec2 100644 --- a/tools/ci/change_class.py +++ b/tools/ci/change_class.py @@ -10,7 +10,7 @@ Four classes. Two come from allowlists: their images). No test job runs in CI (#666). Two Python tests read these files (``tests/test_brand_assets.py``, ``tests/test_package_contents.py``), so - the pre-push hook still runs the suite for them. + the pre-push hook runs those two and not the whole suite (#679). - ``release``: ``pyproject.toml`` is the only file, and its ``version`` line the only line, that changed. CI runs the package-content check only. - ``code``: anything else. A file on no list is code, and so is every case of @@ -32,7 +32,8 @@ Usage: ``change_class.py --unless-inert `` The pre-push hook: runs the command unless pre-commit's pushed range (``PRE_COMMIT_FROM_REF``..``PRE_COMMIT_TO_REF``) is inert, and exits - with the command's code. The command is looked up on ``PATH`` first, so a + with the command's code. For a docs range it runs ``DOCS_TESTS`` in place + of the command. The command is looked up on ``PATH`` first, so a bare ``npm`` finds ``npm.cmd`` on Windows, which ``subprocess`` alone does not do. """ @@ -48,6 +49,8 @@ INERT_FILES = frozenset({".github/FUNDING.yml", "SECURITY.md"}) INERT_FOLDERS = ("images/", ".github/ISSUE_TEMPLATE/") DOCS_FILES = frozenset({"README.md", "README-legacy.md", "CHANGELOG.md"}) DOCS_FOLDERS = ("assets/readme/", "assets/brand/") +# The only tests that open a docs file; a docs push runs these alone (#679). +DOCS_TESTS = ("tests.test_brand_assets", "tests.test_package_contents") VERSION_FILE = "pyproject.toml" VERSION_LINE = re.compile(r'version = "[^"]+"') @@ -167,6 +170,10 @@ def main(argv: list[str]) -> int: if found == "inert": print("Skipped: this push only changes inert files (tools/ci).") return 0 + if found == "docs": + print("This push only changes docs: running the tests that read them.") + command = [sys.executable, "-m", "unittest", *DOCS_TESTS] + return subprocess.run(command, check=False).returncode return run_command(argv[1:]) event = os.environ.get("EVENT_NAME", "") found = "code"