From 945730e89a290994438d91d381c9d8d87d604d38 Mon Sep 17 00:00:00 2001 From: Igor Prilepov Date: Sun, 22 Nov 2026 06:48:23 -0800 Subject: [PATCH] fix: verify executable Python packaging dependency --- .github/workflows/ci.yml | 12 +++ runtime/python/tests/test_packaging.py | 9 +- tests/CMakeLists.txt | 11 ++- .../tools/python_packaging_dependency_test.py | 93 +++++++++++++++++++ 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 tests/tools/python_packaging_dependency_test.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15bbc01..a507f6f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,18 @@ jobs: esac; test "$python_version" = "$expected"' + # Packaging dependencies are optional locally, but installed in this + # release-validation image. Do not silently lose their test coverage. + - name: Verify Python packaging test registration + run: >- + docker compose run --rm dev sh -ec + 'ctest --test-dir build/docker-debug --show-only=json-v1 | + python3 -c "import json, sys; + names = {test[\"name\"] for test in json.load(sys.stdin)[\"tests\"]}; + expected = {\"python_packaging_dependency_test\", \"python_packaging_test\", \"public_examples_test\"}; + assert expected <= names, (\"Missing required packaging coverage\", expected - names); + print(\"Registered packaging coverage:\", sorted(expected))"' + - name: Test run: docker compose run --rm dev ctest --preset docker-debug --output-on-failure diff --git a/runtime/python/tests/test_packaging.py b/runtime/python/tests/test_packaging.py index a822a3f..8543a4e 100644 --- a/runtime/python/tests/test_packaging.py +++ b/runtime/python/tests/test_packaging.py @@ -17,9 +17,10 @@ import zipfile -def run(command: List[str], *, env: Optional[Dict[str, str]] = None) -> subprocess.CompletedProcess: +def run(command: List[str], *, env: Optional[Dict[str, str]] = None, + cwd: Optional[Path] = None) -> subprocess.CompletedProcess: try: - return subprocess.run(command, check=True, text=True, env=env, capture_output=True) + return subprocess.run(command, check=True, text=True, env=env, cwd=cwd, capture_output=True) except subprocess.CalledProcessError as error: raise RuntimeError("command failed: " + " ".join(command) + "\n" + error.stdout + error.stderr) from error @@ -107,7 +108,7 @@ def install_from_sdist(python: Path, sdist: Path, root: Path) -> None: archive.extractall(extracted) source_dir = next(extracted.iterdir()) run([sys.executable, "-m", "build", "--no-isolation", "--wheel", - "--outdir", str(wheel_dir), str(source_dir)], env=clean_env()) + "--outdir", str(wheel_dir), str(source_dir)], env=clean_env(), cwd=root) wheel = next(wheel_dir.glob("quarry_runtime_python-*.whl")) install_wheel(python, wheel) @@ -208,7 +209,7 @@ def main() -> None: root = Path(temporary) dist = root / "dist" run([sys.executable, "-m", "build", "--no-isolation", "--wheel", "--sdist", "--outdir", str(dist), - str(args.runtime_source)], env=clean_env()) + str(args.runtime_source)], env=clean_env(), cwd=root) wheel, sdist = assert_artifacts(dist) expected_version = resolve_source_version(sys.executable, args.runtime_source) install_prefix = root / "quarry-install" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6660767..5feffcb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -189,7 +189,11 @@ target_compile_definitions(python_runtime_test PRIVATE if(QUARRY_PYTHON3_EXECUTABLE) execute_process( - COMMAND "${QUARRY_PYTHON3_EXECUTABLE}" -c "import build, setuptools" + # An empty source-tree build/ is importable as a namespace package, + # but cannot provide the python -m build entry point used below. + # Keep user-site dependencies available for the selected interpreter. + COMMAND "${QUARRY_PYTHON3_EXECUTABLE}" -c "import build.__main__, setuptools" + WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" RESULT_VARIABLE QUARRY_PYTHON_PACKAGING_AVAILABLE OUTPUT_QUIET ERROR_QUIET @@ -197,6 +201,11 @@ if(QUARRY_PYTHON3_EXECUTABLE) endif() if(QUARRY_PYTHON3_EXECUTABLE AND QUARRY_PYTHON_PACKAGING_AVAILABLE EQUAL 0) + add_test(NAME python_packaging_dependency_test + COMMAND ${QUARRY_PYTHON3_EXECUTABLE} + "${CMAKE_CURRENT_SOURCE_DIR}/tools/python_packaging_dependency_test.py" + --source "${CMAKE_SOURCE_DIR}" --cmake "${CMAKE_COMMAND}" + --ctest "${CMAKE_CTEST_COMMAND}" --gtest-source "${googletest_SOURCE_DIR}") add_test(NAME python_packaging_test COMMAND ${QUARRY_PYTHON3_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/../runtime/python/tests/test_packaging.py" diff --git a/tests/tools/python_packaging_dependency_test.py b/tests/tools/python_packaging_dependency_test.py new file mode 100644 index 0000000..43acea9 --- /dev/null +++ b/tests/tools/python_packaging_dependency_test.py @@ -0,0 +1,93 @@ +"""Exercise actual packaging-test registration with and without Python build.""" + +import argparse +import importlib.util +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import venv + + +PACKAGING_TESTS = {"python_packaging_test", "public_examples_test"} + + +def run(command, *, cwd, env=None, success=True): + result = subprocess.run(list(map(str, command)), cwd=cwd, env=env, + text=True, capture_output=True, check=False) + assert (result.returncode == 0) == success, result.stdout + result.stderr + return result.stdout + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--source", type=Path, required=True) + parser.add_argument("--cmake", required=True) + parser.add_argument("--ctest", required=True) + parser.add_argument("--gtest-source", default="") + args = parser.parse_args() + source = args.source.resolve() + with tempfile.TemporaryDirectory(prefix="quarry-packaging-dependency-") as temporary: + root = Path(temporary) + missing = root / "missing-build" + venv.EnvBuilder(with_pip=False).create(missing) + python = missing / ("Scripts/python.exe" if os.name == "nt" else "bin/python") + environment = os.environ.copy() + environment.pop("PYTHONPATH", None) + environment.pop("PYTHONHOME", None) + site = Path(run([python, "-c", + "import sysconfig; print(sysconfig.get_path('purelib'))"], + cwd=root, env=environment).strip()) + # Reuse genuine setuptools, including its bundled dependencies, without + # installing/removing anything in the selected interpreter or using pip. + for name in ("setuptools", "_distutils_hack"): + spec = importlib.util.find_spec(name) + assert spec is not None and spec.origin, name + shutil.copytree(Path(spec.origin).parent, site / name, + ignore=shutil.ignore_patterns("__pycache__")) + + # Reproduce both original symptoms in a disposable namespace directory. + shadow = root / "shadow" + (shadow / "build").mkdir(parents=True) + run([python, "-c", "import build, setuptools; assert build.__file__ is None"], + cwd=shadow, env=environment) + run([python, "-m", "build", "--version"], cwd=shadow, + env=environment, success=False) + run([sys.executable, "-c", "import build.__main__, setuptools"], cwd=shadow) + + outside = root / "out-of-source" + (outside / "build").mkdir(parents=True) + for label, interpreter, expected in (("absent", python, set()), + ("installed", sys.executable, PACKAGING_TESTS)): + for location, cwd in (("root", source), ("outside", outside)): + build = root / (label + "-" + location) + # A directory change alone is insufficient: also put a build + # namespace in the probe's fixed binary-directory location. + (build / "tests" / "build").mkdir(parents=True) + command = [args.cmake, "-S", source, "-B", build, + "-DCMAKE_BUILD_TYPE=Debug", + "-DQUARRY_PYTHON3_EXECUTABLE=" + str(interpreter)] + if args.gtest_source: + command.append("-DFETCHCONTENT_SOURCE_DIR_GOOGLETEST=" + args.gtest_source) + output = run(command, cwd=cwd, + env=environment if label == "absent" else None) + listing = json.loads(run([args.ctest, "--test-dir", build, + "--show-only=json-v1"], cwd=cwd)) + tests = {test["name"]: test for test in listing["tests"]} + registered = tests.keys() & PACKAGING_TESTS + assert registered == expected, (label, location, registered, expected, output) + if label == "absent": + assert "Skipping Python packaging and public example tests" in output + else: + for name in PACKAGING_TESTS: + assert tests[name]["command"][0] == str(interpreter), tests[name] + print(label, location, "registration:", sorted(registered), flush=True) + print("Original namespace false positive reproduced; real CMake registration " + "rejects missing build and accepts installed dependencies from both locations.") + + +if __name__ == "__main__": + main()