From ab931b7c40a749acd757db45c4462f21bd77b422 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 25 Mar 2024 01:13:59 +1100 Subject: [PATCH 1/4] Change unnecessary generator-based fixture to regular fixture --- ropetest/contrib/autoimport/conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ropetest/contrib/autoimport/conftest.py b/ropetest/contrib/autoimport/conftest.py index 29bcba4c..040c4af8 100644 --- a/ropetest/contrib/autoimport/conftest.py +++ b/ropetest/contrib/autoimport/conftest.py @@ -8,19 +8,19 @@ @pytest.fixture def mod1(project): mod1 = testutils.create_module(project, "mod1") - yield mod1 + return mod1 @pytest.fixture def mod1_path(mod1): - yield pathlib.Path(mod1.real_path) + return pathlib.Path(mod1.real_path) @pytest.fixture def typing_path(): import typing - yield pathlib.Path(typing.__file__) + return pathlib.Path(typing.__file__) @pytest.fixture @@ -42,4 +42,4 @@ def example_external_package_path(external_fixturepkg): def compiled_lib(): import _sqlite3 - yield "_sqlite3", pathlib.Path(_sqlite3.__file__) + return "_sqlite3", pathlib.Path(_sqlite3.__file__) From 0d8fa6dafc99b5bc485ad3f671af519dcb82026e Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 25 Mar 2024 01:14:58 +1100 Subject: [PATCH 2/4] Isolate tests that uses external_fixturepkg into a venv This avoids modifying the test runtime environment itself. --- ropetest/conftest.py | 54 ++++++++++++++++++++++--- ropetest/contrib/autoimport/conftest.py | 21 +++++----- ropetest/contrib/autoimporttest.py | 3 +- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/ropetest/conftest.py b/ropetest/conftest.py index 47515d31..b0f15269 100644 --- a/ropetest/conftest.py +++ b/ropetest/conftest.py @@ -1,6 +1,8 @@ +import os import pathlib import sys from subprocess import check_call +from venv import EnvBuilder import pytest @@ -8,9 +10,46 @@ from ropetest import testutils +@pytest.fixture(scope="session") +def session_venv(tmpdir_factory): + path = tmpdir_factory.mktemp("venv") + venv_path = pathlib.Path(path) + + builder = EnvBuilder(with_pip=True) + builder.create(venv_path) + + yield venv_path + + +@pytest.fixture(scope="session") +def session_venv_pyvenv_cfg(session_venv): + cfg = session_venv / "pyvenv.cfg" + return dict(line.split(" = ") for line in cfg.read_text().splitlines()) + + +@pytest.fixture(scope="session") +def session_venv_site_packages(session_venv, session_venv_pyvenv_cfg): + major, minor, patch = session_venv_pyvenv_cfg["version"].split(".") + return session_venv / f"lib/python{major}.{minor}/site-packages" + + +@pytest.fixture(scope='session') +def session_venv_python_executable(session_venv): + # Get the path to the Python executable inside the venv + if os.name == 'nt': + python_executable = session_venv / 'Scripts' / 'python.exe' + else: + python_executable = session_venv / 'bin' / 'python' + + # Yield the Python executable path + yield python_executable + + @pytest.fixture -def project(): - project = testutils.sample_project() +def project(session_venv, session_venv_site_packages): + project = testutils.sample_project( + python_path=[str(session_venv_site_packages)], + ) yield project testutils.remove_project(project) @@ -21,8 +60,11 @@ def project_path(project): @pytest.fixture -def project2(): - project = testutils.sample_project("sample_project2") +def project2(session_venv): + project = testutils.sample_project( + "sample_project2", + python_path=[str(session_venv_site_packages)], + ) yield project testutils.remove_project(project) @@ -50,9 +92,9 @@ def mod2(project, pkg1) -> resources.Folder: @pytest.fixture(scope="session") -def external_fixturepkg(): +def external_fixturepkg(session_venv, session_venv_python_executable): check_call([ - sys.executable, + session_venv_python_executable, "-m", "pip", "install", diff --git a/ropetest/contrib/autoimport/conftest.py b/ropetest/contrib/autoimport/conftest.py index 040c4af8..9dca8ccf 100644 --- a/ropetest/contrib/autoimport/conftest.py +++ b/ropetest/contrib/autoimport/conftest.py @@ -24,18 +24,21 @@ def typing_path(): @pytest.fixture -def example_external_package_module_path(external_fixturepkg): - from external_fixturepkg import mod1 - yield pathlib.Path(mod1.__file__) +def example_external_package_module_path( + session_venv, + external_fixturepkg, + session_venv_site_packages, +): + return session_venv_site_packages / "external_fixturepkg/mod1.py" @pytest.fixture -def example_external_package_path(external_fixturepkg): - import external_fixturepkg - - # Uses __init__.py so we need the parent - - yield pathlib.Path(external_fixturepkg.__file__).parent +def example_external_package_path( + session_venv, + external_fixturepkg, + session_venv_site_packages, +): + return session_venv_site_packages / "external_fixturepkg" @pytest.fixture diff --git a/ropetest/contrib/autoimporttest.py b/ropetest/contrib/autoimporttest.py index d74135ff..d42ec14f 100644 --- a/ropetest/contrib/autoimporttest.py +++ b/ropetest/contrib/autoimporttest.py @@ -177,8 +177,7 @@ def test_skipping_directories_not_accessible_because_of_permission_error(self): self.assertGreater(len(self.importer._dump_all()), 0) -def test_search_submodule(external_fixturepkg): - project = testutils.sample_project(extension_modules=["sys"]) +def test_search_submodule(project, external_fixturepkg): importer = autoimport.AutoImport(project, observe=False) importer.update_module("external_fixturepkg") import_statement = ("from external_fixturepkg import mod1", "mod1") From 67edcd72a211230f75928b090dc6bfcb7cd501f6 Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 25 Mar 2024 01:25:01 +1100 Subject: [PATCH 3/4] Fix venv Lib path for Windows --- ropetest/conftest.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ropetest/conftest.py b/ropetest/conftest.py index b0f15269..30af57e8 100644 --- a/ropetest/conftest.py +++ b/ropetest/conftest.py @@ -29,8 +29,11 @@ def session_venv_pyvenv_cfg(session_venv): @pytest.fixture(scope="session") def session_venv_site_packages(session_venv, session_venv_pyvenv_cfg): - major, minor, patch = session_venv_pyvenv_cfg["version"].split(".") - return session_venv / f"lib/python{major}.{minor}/site-packages" + if os.name == 'nt': + return session_venv / f"Lib/site-packages" + else: + major, minor, patch = session_venv_pyvenv_cfg["version"].split(".") + return session_venv / f"lib/python{major}.{minor}/site-packages" @pytest.fixture(scope='session') From 6bbef1eac92760b8363e69c6523dc1f94c29281f Mon Sep 17 00:00:00 2001 From: Lie Ryan Date: Mon, 25 Mar 2024 01:27:16 +1100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22f570a9..d0c39d22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # **Upcoming release** +- #781, #783 Isolate tests that uses external_fixturepkg into a venv - #751 Check for ast.Attributes when finding occurrences in fstrings (@sandratsy) - #777, #698 add validation to refuse Rename refactoring to a python keyword (@lieryan) - #730 Match on module aliases for autoimport suggestions (@MrBago)