Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions runtime/python/tests/test_packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand Down
11 changes: 10 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,23 @@ 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
)
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"
Expand Down
93 changes: 93 additions & 0 deletions tests/tools/python_packaging_dependency_test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading