From f05336717eef87559ab399de7b1459d60c5c9a16 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 23 Nov 2022 14:55:09 -0500 Subject: [PATCH] tests: split up isolated and virtualenv fixtures Signed-off-by: Henry Schreiner --- pyproject.toml | 1 + tests/conftest.py | 14 +++++++++++++- tests/test_pyproject_pep518.py | 24 ++++++++++-------------- tests/test_setuptools_pep518.py | 22 ++++++++++------------ 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 28174a56b..afaa54369 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -120,6 +120,7 @@ markers = [ "integration: Full package build", "setuptools: Tests setuptools integration", "virtualenv: Needs a virtualenv", + "isolated: Needs an isolated virtualenv", ] diff --git a/tests/conftest.py b/tests/conftest.py index ad4386c89..96e342eb0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -144,7 +144,7 @@ def install(self, *args: str) -> None: @pytest.fixture -def virtualenv(tmp_path: Path, pep518_wheelhouse: Path) -> Generator[VEnv, None, None]: +def isolated(tmp_path: Path, pep518_wheelhouse: Path) -> Generator[VEnv, None, None]: path = tmp_path / "venv" try: yield VEnv(path, wheelhouse=pep518_wheelhouse) @@ -152,11 +152,23 @@ def virtualenv(tmp_path: Path, pep518_wheelhouse: Path) -> Generator[VEnv, None, shutil.rmtree(path) +@pytest.fixture +def virtualenv(tmp_path: Path) -> Generator[VEnv, None, None]: + path = tmp_path / "venv" + try: + yield VEnv(path) + finally: + shutil.rmtree(path) + + def pytest_collection_modifyitems(items: list[pytest.Item]) -> None: for item in items: # Ensure all tests using virtualenv are marked as such if "virtualenv" in getattr(item, "fixturenames", ()): item.add_marker(pytest.mark.virtualenv) + if "isolated" in getattr(item, "fixturenames", ()): + item.add_marker(pytest.mark.virtualenv) + item.add_marker(pytest.mark.isolated) def pytest_report_header() -> str: diff --git a/tests/test_pyproject_pep518.py b/tests/test_pyproject_pep518.py index ffe22e9df..117b88f75 100644 --- a/tests/test_pyproject_pep518.py +++ b/tests/test_pyproject_pep518.py @@ -70,12 +70,12 @@ def test_pep518_sdist(): @pytest.mark.parametrize( "build_args", [(), ("--wheel",)], ids=["sdist_to_wheel", "wheel_directly"] ) -def test_pep518_wheel(virtualenv, build_args, monkeypatch): +def test_pep518_wheel(isolated, build_args, monkeypatch): dist = HELLO_PEP518 / "dist" shutil.rmtree(dist, ignore_errors=True) monkeypatch.chdir(HELLO_PEP518) - virtualenv.install("build[virtualenv]") - virtualenv.module( + isolated.install("build[virtualenv]") + isolated.module( "build", "--config-setting=logging.level=DEBUG", *build_args, @@ -95,27 +95,23 @@ def test_pep518_wheel(virtualenv, build_args, monkeypatch): assert so_file.startswith("cmake_example") print("SOFILE:", so_file) - virtualenv.install(wheel) + isolated.install(wheel) - version = virtualenv.execute( - "import cmake_example; print(cmake_example.__version__)" - ) + version = isolated.execute("import cmake_example; print(cmake_example.__version__)") assert version == "0.0.1" - add = virtualenv.execute("import cmake_example; print(cmake_example.add(1, 2))") + add = isolated.execute("import cmake_example; print(cmake_example.add(1, 2))") assert add == "3" @pytest.mark.compile @pytest.mark.configure @pytest.mark.integration -def test_pep518_pip(virtualenv): - virtualenv.install("-v", HELLO_PEP518) +def test_pep518_pip(isolated): + isolated.install("-v", HELLO_PEP518) - version = virtualenv.execute( - "import cmake_example; print(cmake_example.__version__)" - ) + version = isolated.execute("import cmake_example; print(cmake_example.__version__)") assert version == "0.0.1" - add = virtualenv.execute("import cmake_example; print(cmake_example.add(1, 2))") + add = isolated.execute("import cmake_example; print(cmake_example.add(1, 2))") assert add == "3" diff --git a/tests/test_setuptools_pep518.py b/tests/test_setuptools_pep518.py index bb4ea4bae..13bf175f8 100644 --- a/tests/test_setuptools_pep518.py +++ b/tests/test_setuptools_pep518.py @@ -18,12 +18,12 @@ @pytest.mark.skipif( sys.platform.startswith("cygwin"), reason="Cygwin fails here with ld errors" ) -def test_pep518_wheel(monkeypatch, virtualenv): +def test_pep518_wheel(monkeypatch, isolated): dist = HELLO_PEP518 / "dist" shutil.rmtree(dist, ignore_errors=True) monkeypatch.chdir(HELLO_PEP518) - virtualenv.install("build[virtualenv]") - virtualenv.module("build", "--wheel") + isolated.install("build[virtualenv]") + isolated.module("build", "--wheel") (wheel,) = dist.iterdir() assert "cmake_example-0.0.1" in wheel.name assert wheel.suffix == ".whl" @@ -41,14 +41,12 @@ def test_pep518_wheel(monkeypatch, virtualenv): assert so_file.startswith("cmake_example") print("SOFILE:", so_file) - virtualenv.install(wheel) + isolated.install(wheel) - version = virtualenv.execute( - "import cmake_example; print(cmake_example.__version__)" - ) + version = isolated.execute("import cmake_example; print(cmake_example.__version__)") assert version == "0.0.1" - add = virtualenv.execute("import cmake_example; print(cmake_example.add(1, 2))") + add = isolated.execute("import cmake_example; print(cmake_example.add(1, 2))") assert add == "3" @@ -58,15 +56,15 @@ def test_pep518_wheel(monkeypatch, virtualenv): @pytest.mark.skipif( sys.platform.startswith("cygwin"), reason="Cygwin fails here with ld errors" ) -def test_pep518_pip(virtualenv): - virtualenv.install("-v", HELLO_PEP518) +def test_pep518_pip(isolated): + isolated.install("-v", HELLO_PEP518) - version = virtualenv.execute( + version = isolated.execute( "import cmake_example; print(cmake_example.__version__)", ) assert version == "0.0.1" - add = virtualenv.execute( + add = isolated.execute( "import cmake_example; print(cmake_example.add(1, 2))", ) assert add == "3"