Skip to content

Commit

Permalink
fix(tests): ensure only mocked system site is used
Browse files Browse the repository at this point in the history
Previously, on systems where there existed multiple system-site
locations the `test_system_site_packages` test case failed due to it
incorrectly assuming only one site needed removal when patching the
environment's `sys_path`. This change ensures all paths outside the
`tmp_path` is ignored.
  • Loading branch information
abn committed Jan 5, 2025
1 parent d6881e3 commit 095a97d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tests/repositories/test_installed_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,11 @@ def test_system_site_packages(
shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
orig_sys_path = env.sys_path
if with_system_site_packages:
# on some environments, there could be multiple system-site, filter out those and inject our test site
mocker.patch(
"poetry.utils.env.virtual_env.VirtualEnv.sys_path",
orig_sys_path[:-1] + [str(site_path)],
[p for p in orig_sys_path if p.startswith(str(tmp_path))]
+ [str(site_path)],
)
mocker.patch(
"poetry.utils.env.generic_env.GenericEnv.get_paths",
Expand All @@ -449,9 +451,12 @@ def test_system_site_packages(
installed_repository = InstalledRepository.load(env)

expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
expected_packages = {"standard"} | expected_system_site_packages

if platform.system() == "FreeBSD":
expected_packages.add("sqlite3")
expected_system_site_packages.add("sqlite3")

expected_packages = {"standard"} | expected_system_site_packages

assert {p.name for p in installed_repository.packages} == expected_packages

Check failure on line 460 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.9) / pytest

tests/repositories/test_installed_repository.py#L460

tests.repositories.test_installed_repository.test_system_site_packages[True]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw1/test_system_site_packages_True0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x2c4094284e80>
poetry = <poetry.poetry.Poetry object at 0x2c4092d800a0>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw1/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = True

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
>       assert {p.name for p in installed_repository.packages} == expected_packages
E       AssertionError: assert {'cleo', 'standard'} == {'cleo', 'sql...', 'standard'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E           {
E               'cleo',
E         -     'sqlite3',
E               'standard',
E           }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:460: AssertionError

Check failure on line 460 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.11) / pytest

tests/repositories/test_installed_repository.py#L460

tests.repositories.test_installed_repository.test_system_site_packages[True]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw1/test_system_site_packages_True0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x25005d2b0e90>
poetry = <poetry.poetry.Poetry object at 0x25005bc6d1d0>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw1/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = True

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
>       assert {p.name for p in installed_repository.packages} == expected_packages
E       AssertionError: assert {'cleo', 'standard'} == {'cleo', 'sql...', 'standard'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E           {
E               'cleo',
E         -     'sqlite3',
E               'standard',
E           }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:460: AssertionError

Check failure on line 460 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.10) / pytest

tests/repositories/test_installed_repository.py#L460

tests.repositories.test_installed_repository.test_system_site_packages[True]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/test_system_site_packages_True0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x2da461602fe0>
poetry = <poetry.poetry.Poetry object at 0x2da4616825c0>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = True

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
>       assert {p.name for p in installed_repository.packages} == expected_packages
E       AssertionError: assert {'cleo', 'standard'} == {'cleo', 'sql...', 'standard'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E           {
E               'cleo',
E         -     'sqlite3',
E               'standard',
E           }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:460: AssertionError
assert {

Check failure on line 461 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.9) / pytest

tests/repositories/test_installed_repository.py#L461

tests.repositories.test_installed_repository.test_system_site_packages[False]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/test_system_site_packages_Fals0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x34b469accd00>
poetry = <poetry.poetry.Poetry object at 0x34b46990ec70>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = False

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
        assert {p.name for p in installed_repository.packages} == expected_packages
>       assert {
            p.name for p in installed_repository.system_site_packages
        } == expected_system_site_packages
E       AssertionError: assert set() == {'sqlite3'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E         + set()
E         - {
E         -     'sqlite3',
E         - }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:461: AssertionError

Check failure on line 461 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.11) / pytest

tests/repositories/test_installed_repository.py#L461

tests.repositories.test_installed_repository.test_system_site_packages[False]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/test_system_site_packages_Fals0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x49115495150>
poetry = <poetry.poetry.Poetry object at 0x49115335b10>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = False

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
        assert {p.name for p in installed_repository.packages} == expected_packages
>       assert {
            p.name for p in installed_repository.system_site_packages
        } == expected_system_site_packages
E       AssertionError: assert set() == {'sqlite3'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E         + set()
E         - {
E         -     'sqlite3',
E         - }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:461: AssertionError

Check failure on line 461 in tests/repositories/test_installed_repository.py

View check run for this annotation

Cirrus CI / Tests / FreeBSD (Python 3.10) / pytest

tests/repositories/test_installed_repository.py#L461

tests.repositories.test_installed_repository.test_system_site_packages[False]
Raw output
tmp_path = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/test_system_site_packages_Fals0')
mocker = <pytest_mock.plugin.MockerFixture object at 0x2da46148fa00>
poetry = <poetry.poetry.Poetry object at 0x2da4619912d0>
site_purelib = PosixPath('/tmp/pytest-of-root/pytest-0/popen-gw0/session-working-directory0/installed/lib/python3.7/site-packages')
with_system_site_packages = False

    @pytest.mark.parametrize("with_system_site_packages", [False, True])
    def test_system_site_packages(
        tmp_path: Path,
        mocker: MockerFixture,
        poetry: Poetry,
        site_purelib: Path,
        with_system_site_packages: bool,
    ) -> None:
        venv_path = tmp_path / "venv"
        site_path = tmp_path / "site"
        cleo_dist_info = "cleo-0.7.6.dist-info"
        shutil.copytree(site_purelib / cleo_dist_info, site_path / cleo_dist_info)
    
        EnvManager(poetry).build_venv(
            path=venv_path, flags={"system-site-packages": with_system_site_packages}
        )
        env = VirtualEnv(venv_path)
        standard_dist_info = "standard-1.2.3.dist-info"
        shutil.copytree(site_purelib / standard_dist_info, env.purelib / standard_dist_info)
        orig_sys_path = env.sys_path
        if with_system_site_packages:
            # on some environments, there could be multiple system-site, filter out those and inject our test site
            mocker.patch(
                "poetry.utils.env.virtual_env.VirtualEnv.sys_path",
                [p for p in orig_sys_path if p.startswith(str(tmp_path))]
                + [str(site_path)],
            )
        mocker.patch(
            "poetry.utils.env.generic_env.GenericEnv.get_paths",
            return_value={"purelib": str(site_path)},
        )
    
        installed_repository = InstalledRepository.load(env)
    
        expected_system_site_packages = {"cleo"} if with_system_site_packages else set()
    
        if platform.system() == "FreeBSD":
            expected_system_site_packages.add("sqlite3")
    
        expected_packages = {"standard"} | expected_system_site_packages
    
        assert {p.name for p in installed_repository.packages} == expected_packages
>       assert {
            p.name for p in installed_repository.system_site_packages
        } == expected_system_site_packages
E       AssertionError: assert set() == {'sqlite3'}
E         
E         Extra items in the right set:
E         'sqlite3'
E         
E         Full diff:
E         + set()
E         - {
E         -     'sqlite3',
E         - }

/tmp/cirrus-ci-build/tests/repositories/test_installed_repository.py:461: AssertionError
p.name for p in installed_repository.system_site_packages
Expand Down

0 comments on commit 095a97d

Please sign in to comment.