Skip to content

Commit

Permalink
Isolate tests that uses external_fixturepkg into a venv
Browse files Browse the repository at this point in the history
This avoids installing to the test venv itself.
  • Loading branch information
lieryan committed Mar 24, 2024
1 parent ab931b7 commit 849cd23
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
54 changes: 48 additions & 6 deletions ropetest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
import os
import pathlib
import sys
from subprocess import check_call
from venv import EnvBuilder

import pytest

from rope.base import resources
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'

Check warning on line 40 in ropetest/conftest.py

View check run for this annotation

Codecov / codecov/patch

ropetest/conftest.py#L40

Added line #L40 was not covered by tests
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)

Expand All @@ -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)

Expand Down Expand Up @@ -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",
Expand Down
21 changes: 12 additions & 9 deletions ropetest/contrib/autoimport/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions ropetest/contrib/autoimporttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 849cd23

Please sign in to comment.