Skip to content

Commit

Permalink
Merge pull request #783 from python-rope/lieryan-781-venv-isolate
Browse files Browse the repository at this point in the history
Isolate tests that uses external_fixturepkg into a venv
  • Loading branch information
lieryan authored Mar 24, 2024
2 parents 1489d32 + aa0ffa6 commit e261463
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
57 changes: 51 additions & 6 deletions ropetest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
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):
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')
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)

Expand All @@ -21,8 +63,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 +95,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
29 changes: 16 additions & 13 deletions ropetest/contrib/autoimport/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,41 @@
@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
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
def compiled_lib():
import _sqlite3

yield "_sqlite3", pathlib.Path(_sqlite3.__file__)
return "_sqlite3", pathlib.Path(_sqlite3.__file__)
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 e261463

Please sign in to comment.