Files
ComfyAssets-ComfyUI_PromptM…/tests/test_ci_change_class.py
T
Vito c9d2b9c66b ci: test only changes that can affect code (#639) (#640)
* ci: test only changes that can affect code (#639)

Each gating workflow starts with a change class job that sorts the
pull request, merge group or push by the files it touches
(tools/ci/change_class.py): inert (nothing reads them), docs (a Python
test reads them) or code (anything else and every case of doubt).

- inert: only the classifier and the two gates do work. The required
  py3.12 leg starts and skips its steps, because a skipped matrix job
  would never report that context.
- docs: the Python 3.12 suite runs as well; it holds the README tests.
- code: everything runs, as before.

The gates require the classifier to succeed and every job to do what
the class asks; an unknown class fails them. bandit has no gate, so it
runs unless the class is docs or inert. The required contexts are
unchanged. The pre-push hook skips the suite for an inert push.

* test(ci): keep the classifier tests out of a git hook's repository (#639)

Inside the pre-push hook git exports GIT_DIR and GIT_INDEX_FILE for the
repository being pushed. The test's own git calls inherited them and so
ran git init, add and commit against that repository instead of their
temp one. They now drop every GIT_ variable, and the tests run with
those variables pointing at another repository that must stay absent.

* test(ci): import unittest.mock one way

* fix(ci): find the pre-push command through PATH before running it

The pre-push wrapper ran its command with subprocess and a bare name.
On Windows the frontend hook's npm is npm.cmd, which subprocess does
not find without a PATH and PATHEXT lookup, so every non-inert push
failed with FileNotFoundError (Codex review). The wrapper now resolves
the program with shutil.which first, and says so when it is missing.
Reproduced and verified on Windows 11: before, FileNotFoundError; after,
npm --version runs.

* test(inspect): keep trying until a video is seen still starting at loadstart (#641)

A busy WebKit fired the viewer video's loadstart after it already had
HAVE_ENOUGH_DATA (merge-queue run 43732282629, readyState 4), so the
per-attempt readyState === 0 assertion failed although the settled path
behaved correctly. The three #483 tests now count the attempts that saw
the video still starting, run up to twice as many pages until one does,
and fail only if none did.
2026-11-15 17:03:10 +00:00

260 lines
9.7 KiB
Python

"""CI runs tests only for changes that can affect code (#639).
``tools/ci/change_class.py`` sorts a change by the files it touches: ``inert``
(nothing reads the file at run or test time), ``docs`` (a Python test reads
it) or ``code`` (everything else, and every case of doubt).
"""
import os
import subprocess
import sys
import tempfile
import unittest
import unittest.mock
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "tools" / "ci"))
import change_class # noqa: E402
SCRIPT = ROOT / "tools" / "ci" / "change_class.py"
def _isolated_environment(**extra):
"""The environment without git's own variables and the script's inputs.
A git hook exports ``GIT_DIR`` and ``GIT_INDEX_FILE`` for the repository
being pushed. A child ``git`` that inherits them works on that repository
instead of the temp one, so the pre-push hook must never pass them on.
"""
kept = {
key: value
for key, value in os.environ.items()
if not key.startswith("GIT_") and key not in change_class.ENVIRONMENT
}
return {**kept, **extra}
class TestClassify(unittest.TestCase):
def test_files_nothing_reads_are_inert(self):
for paths in (
[".github/FUNDING.yml"],
["SECURITY.md"],
["images/pm-01.png", ".github/ISSUE_TEMPLATE/bug_report.md"],
):
self.assertEqual(change_class.classify(paths), "inert", paths)
def test_documents_a_test_reads_are_docs(self):
for paths in (
["README.md"],
["README-legacy.md", "assets/readme/hero.webp"],
["assets/brand/banner.webp", ".github/FUNDING.yml"],
):
self.assertEqual(change_class.classify(paths), "docs", paths)
def test_anything_else_is_code(self):
for paths in (
["py/api/images.py"],
["README.md", "web/js/workspace/index.mjs"],
[".github/FUNDING.yml", "pyproject.toml"],
# The browser suite compares production against the concept.
["docs/concepts/workspace/app.js"],
# The tooling tests read the tables in it.
["TESTING.md"],
[".github/workflows/test.yml"],
["LICENSE"],
# Look-alikes of listed names are not listed.
["docs/README.md"],
["web/images/x.png"],
["SECURITY.md.py"],
[".github/funding.yml"],
):
self.assertEqual(change_class.classify(paths), "code", paths)
def test_no_files_is_code(self):
self.assertEqual(change_class.classify([]), "code")
def test_every_document_a_test_reads_exists(self):
for name in change_class.DOCS_FILES:
self.assertTrue((ROOT / name).is_file(), name)
for folder in change_class.DOCS_FOLDERS:
self.assertTrue((ROOT / folder).is_dir(), folder)
class TestCommandLine(unittest.TestCase):
"""The script against a real repository with one commit per kind of change."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.repo = Path(self.tmp.name) / "repo"
self.repo.mkdir()
# As in a git hook: these name another repository, which must stay untouched.
self.other = Path(self.tmp.name) / "other.git"
patcher = unittest.mock.patch.dict(
os.environ,
{
"GIT_DIR": str(self.other),
"GIT_INDEX_FILE": str(self.other / "index"),
"GIT_WORK_TREE": str(self.tmp.name),
},
)
patcher.start()
self.addCleanup(patcher.stop)
self.addCleanup(lambda: self.assertFalse(self.other.exists()))
self.git("init", "-q", "-b", "main")
self.commit({"py/a.py": "a\n", "README.md": "r\n"})
self.base = self.head()
def git(self, *args):
return subprocess.run(
["git", "-c", "user.name=t", "-c", "user.email=t@example.com", *args],
cwd=self.repo,
env=_isolated_environment(),
check=True,
capture_output=True,
text=True,
).stdout.strip()
def commit(self, files):
for name, text in files.items():
path = self.repo / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
self.git("add", "-A")
self.git("commit", "-q", "-m", "change")
def head(self):
return self.git("rev-parse", "HEAD")
def run_script(self, *args, **env):
return subprocess.run(
[sys.executable, str(SCRIPT), *args],
cwd=self.repo,
env=_isolated_environment(**env),
capture_output=True,
text=True,
)
def class_of(self, **env):
result = self.run_script(**env)
self.assertEqual(result.returncode, 0, result.stderr)
return result.stdout.strip()
def test_a_pull_request_is_classed_by_its_own_files(self):
self.git("checkout", "-q", "-b", "topic")
self.commit({".github/FUNDING.yml": "x\n"})
topic = self.head()
# main moved on with code meanwhile: that is not this pull request's change.
self.git("checkout", "-q", "main")
self.commit({"py/a.py": "b\n"})
self.assertEqual(
self.class_of(
EVENT_NAME="pull_request", BASE_SHA=self.head(), HEAD_SHA=topic
),
"class=inert",
)
def test_a_merge_group_and_a_push_are_classed_by_their_range(self):
self.commit({"README.md": "r2\n"})
for event in ("merge_group", "push"):
self.assertEqual(
self.class_of(
EVENT_NAME=event, BASE_SHA=self.base, HEAD_SHA=self.head()
),
"class=docs",
event,
)
self.commit({"py/a.py": "c\n"})
self.assertEqual(
self.class_of(
EVENT_NAME="merge_group", BASE_SHA=self.base, HEAD_SHA=self.head()
),
"class=code",
)
def test_a_deleted_or_renamed_file_counts_under_both_names(self):
self.git("mv", "py/a.py", "SECURITY.md")
self.git("commit", "-q", "-m", "rename")
self.assertEqual(
self.class_of(
EVENT_NAME="merge_group", BASE_SHA=self.base, HEAD_SHA=self.head()
),
"class=code",
)
def test_doubt_is_code(self):
self.commit({".github/FUNDING.yml": "x\n"})
head = self.head()
zero = "0" * 40
for env in (
{"EVENT_NAME": "workflow_dispatch", "BASE_SHA": self.base},
{"EVENT_NAME": "schedule", "BASE_SHA": self.base, "HEAD_SHA": head},
{"EVENT_NAME": "push", "BASE_SHA": zero, "HEAD_SHA": head},
{"EVENT_NAME": "push", "BASE_SHA": "", "HEAD_SHA": head},
{"EVENT_NAME": "push", "BASE_SHA": "f" * 40, "HEAD_SHA": head},
{"EVENT_NAME": "push", "BASE_SHA": head, "HEAD_SHA": head},
{},
):
self.assertEqual(self.class_of(**env), "class=code", env)
def test_the_pre_push_command_is_skipped_only_for_an_inert_push(self):
marker = self.repo / "ran"
command = (
"--unless-inert",
sys.executable,
"-c",
"open('ran', 'w').close()",
)
self.commit({".github/FUNDING.yml": "x\n"})
refs = {"PRE_COMMIT_FROM_REF": self.base, "PRE_COMMIT_TO_REF": self.head()}
result = self.run_script(*command, **refs)
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.
self.assertEqual(self.run_script(*command).returncode, 0)
self.assertTrue(marker.exists())
marker.unlink()
self.commit({"README.md": "r3\n"})
refs["PRE_COMMIT_TO_REF"] = self.head()
self.assertEqual(self.run_script(*command, **refs).returncode, 0)
self.assertTrue(marker.exists())
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))
with unittest.mock.patch.dict(os.environ, _isolated_environment(), clear=True):
with (
unittest.mock.patch.object(
change_class.shutil, "which", return_value="C:/node/npm.cmd"
) as which,
unittest.mock.patch.object(change_class.subprocess, "run", found),
):
code = change_class.main(["--unless-inert", "npm", "test"])
which.assert_called_once_with("npm")
found.assert_called_once_with(["C:/node/npm.cmd", "test"], check=False)
self.assertEqual(code, 3)
def test_a_missing_or_absent_pre_push_command_fails_clearly(self):
with unittest.mock.patch.dict(os.environ, _isolated_environment(), clear=True):
with (
unittest.mock.patch.object(
change_class.shutil, "which", return_value=None
),
unittest.mock.patch.object(change_class.subprocess, "run") as run,
):
self.assertEqual(change_class.main(["--unless-inert", "nope"]), 127)
self.assertEqual(change_class.main(["--unless-inert"]), 2)
run.assert_not_called()
def test_the_pre_push_command_keeps_its_exit_code(self):
result = self.run_script(
"--unless-inert", sys.executable, "-c", "raise SystemExit(7)"
)
self.assertEqual(result.returncode, 7)
if __name__ == "__main__":
unittest.main()