From 577c3a67125b7e246333949a692ef2fd74d1b990 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Sat, 10 Oct 2026 07:27:45 +0800 Subject: [PATCH 1/2] test: resolve local test helpers in subprocesses --- tests/__init__.py | 1 + tests/test_test_package_resolution.py | 34 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_test_package_resolution.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..229772d2 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""Local test helpers imported by subprocess-based integration tests.""" diff --git a/tests/test_test_package_resolution.py b/tests/test_test_package_resolution.py new file mode 100644 index 00000000..ff7a1d0c --- /dev/null +++ b/tests/test_test_package_resolution.py @@ -0,0 +1,34 @@ +"""Detached test workers must import this repo's helpers ahead of installed packages.""" + +import os +from pathlib import Path +import subprocess +import sys + + +ROOT = Path(__file__).resolve().parents[1] + + +def test_local_tests_package_wins_over_an_installed_tests_package(tmp_path): + fake_site = tmp_path / "site-packages" + conflicting = fake_site / "tests" + conflicting.mkdir(parents=True) + (conflicting / "__init__.py").write_text("# simulated installed package\n") + + env = os.environ.copy() + env["PYTHONPATH"] = str(fake_site) + result = subprocess.run( + [ + sys.executable, + "-c", + "import tests.test_evolvespec; import tests; print(tests.__file__)", + ], + cwd=ROOT, + env=env, + text=True, + capture_output=True, + check=False, + ) + + assert result.returncode == 0, result.stdout + result.stderr + assert str(ROOT / "tests" / "__init__.py") in result.stdout From 7ab7cca35b64a04e259be823f3b1228831dafa35 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Sat, 10 Oct 2026 07:30:47 +0800 Subject: [PATCH 2/2] test: preserve test helper imports with explicit packages --- pyproject.toml | 7 ++++--- scripts/__init__.py | 1 + tests/test_test_package_resolution.py | 26 +++++++++++++++----------- 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 scripts/__init__.py diff --git a/pyproject.toml b/pyproject.toml index 337f7d20..320e9312 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,7 @@ agentdescent = ["integrations/*.md", "integrations/*.json"] [tool.pytest.ini_options] testpaths = ["tests"] addopts = "-q" -# Put the repo root on sys.path so `import agentdescent` / `import examples` -# resolve under a bare `pytest` invocation (CI), not just `python -m pytest`. -pythonpath = ["."] +# Put both the repo root and tests directory on sys.path. The root resolves +# package imports; tests keeps legacy top-level test helpers importable after +# making `tests` an explicit package for subprocess-based refs. +pythonpath = [".", "tests"] diff --git a/scripts/__init__.py b/scripts/__init__.py new file mode 100644 index 00000000..d2b633e4 --- /dev/null +++ b/scripts/__init__.py @@ -0,0 +1 @@ +"""Repository scripts imported by tests and development tools.""" diff --git a/tests/test_test_package_resolution.py b/tests/test_test_package_resolution.py index ff7a1d0c..c4e9caef 100644 --- a/tests/test_test_package_resolution.py +++ b/tests/test_test_package_resolution.py @@ -1,4 +1,4 @@ -"""Detached test workers must import this repo's helpers ahead of installed packages.""" +"""Local test helpers must win over unrelated installed packages.""" import os from pathlib import Path @@ -9,20 +9,23 @@ ROOT = Path(__file__).resolve().parents[1] -def test_local_tests_package_wins_over_an_installed_tests_package(tmp_path): +def test_repository_test_and_script_packages_win_over_installed_packages(tmp_path): fake_site = tmp_path / "site-packages" - conflicting = fake_site / "tests" - conflicting.mkdir(parents=True) - (conflicting / "__init__.py").write_text("# simulated installed package\n") + for name in ("tests", "scripts"): + conflicting = fake_site / name + conflicting.mkdir(parents=True) + (conflicting / "__init__.py").write_text( + f"# simulated installed {name} package\n" + ) env = os.environ.copy() - env["PYTHONPATH"] = str(fake_site) + env["PYTHONPATH"] = os.pathsep.join((str(fake_site), str(ROOT / "tests"))) + code = ( + "import tests.test_evolvespec, scripts.audit_workloads, faults, audit_ppi_cases; " + "import tests, scripts; print(tests.__file__); print(scripts.__file__)" + ) result = subprocess.run( - [ - sys.executable, - "-c", - "import tests.test_evolvespec; import tests; print(tests.__file__)", - ], + [sys.executable, "-c", code], cwd=ROOT, env=env, text=True, @@ -32,3 +35,4 @@ def test_local_tests_package_wins_over_an_installed_tests_package(tmp_path): assert result.returncode == 0, result.stdout + result.stderr assert str(ROOT / "tests" / "__init__.py") in result.stdout + assert str(ROOT / "scripts" / "__init__.py") in result.stdout