From f3d10afcd7975a34d9b171c398aaa59457297fe5 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Thu, 24 Aug 2023 15:49:40 +0530 Subject: [PATCH 1/6] Add site package paths to collection_paths --- src/ansible_compat/runtime.py | 6 ++++++ test/test_runtime.py | 9 +++++++++ tox.ini | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index b8dd3deb..0f3dd153 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -8,6 +8,7 @@ import os import re import shutil +import site import subprocess import sys import tempfile @@ -205,6 +206,11 @@ def __init__( self.cache_dir = get_cache_dir(self.project_dir) self.config = AnsibleConfig() + # Add the site packages path to the collection paths + self.config.collections_paths.extend( # pylint: disable=E1101 + site.getusersitepackages(), + ) + # Add the sys.path to the collection paths if not isolated self._add_sys_path_to_collection_paths() diff --git a/test/test_runtime.py b/test/test_runtime.py index 63017eea..c10f303a 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -5,6 +5,7 @@ import logging import os import pathlib +import site import subprocess from contextlib import contextmanager from dataclasses import dataclass, fields @@ -928,3 +929,11 @@ def test_galaxy_path(path: str, result: list[str]) -> None: def test_is_url(name: str, result: bool) -> None: """Checks functionality of is_url.""" assert is_url(name) == result + + +def test_runtime_site_package_path() -> None: + """Test site package paths.""" + runtime = Runtime() + site_paths = site.getsitepackages() + for path in site_paths: + assert path in runtime.config.collections_paths diff --git a/tox.ini b/tox.ini index e9613ee7..d4f5afb3 100644 --- a/tox.ini +++ b/tox.ini @@ -71,7 +71,7 @@ setenv = PIP_DISABLE_PIP_VERSION_CHECK = 1 PIP_CONSTRAINT = {toxinidir}/requirements.txt PRE_COMMIT_COLOR = always - PYTEST_REQPASS = 93 + PYTEST_REQPASS = 94 FORCE_COLOR = 1 allowlist_externals = ansible From 686e545d45ca671ff68e5e6489a4dd62a8ef7cae Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Fri, 25 Aug 2023 16:17:37 +0530 Subject: [PATCH 2/6] Update the way sys.path is added to collections_paths --- src/ansible_compat/runtime.py | 15 +++++++-------- test/test_runtime.py | 16 ++-------------- tox.ini | 2 +- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index 0f3dd153..c841a495 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -8,7 +8,6 @@ import os import re import shutil -import site import subprocess import sys import tempfile @@ -206,11 +205,6 @@ def __init__( self.cache_dir = get_cache_dir(self.project_dir) self.config = AnsibleConfig() - # Add the site packages path to the collection paths - self.config.collections_paths.extend( # pylint: disable=E1101 - site.getusersitepackages(), - ) - # Add the sys.path to the collection paths if not isolated self._add_sys_path_to_collection_paths() @@ -243,8 +237,13 @@ def warning( def _add_sys_path_to_collection_paths(self) -> None: """Add the sys.path to the collection paths.""" - if not self.isolated and self.config.collections_scan_sys_path: - self.config.collections_paths.extend(sys.path) # pylint: disable=E1101 + if not self.isolated: + for path in sys.path: + if ( + path not in self.config.collections_paths + and Path(Path(path) / "ansible_collections").is_dir() + ): + self.config.collections_paths.append(path) # pylint: disable=E1101 def load_collections(self) -> None: """Load collection data.""" diff --git a/test/test_runtime.py b/test/test_runtime.py index c10f303a..c3a5ebb7 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -471,12 +471,7 @@ def __str__(self) -> str: @pytest.mark.parametrize( ("param"), - ( - ScanSysPath(isolated=True, scan=True, expected=False), - ScanSysPath(isolated=True, scan=False, expected=False), - ScanSysPath(isolated=False, scan=True, expected=True), - ScanSysPath(isolated=False, scan=False, expected=False), - ), + (ScanSysPath(isolated=False, scan=True, expected=True),), ids=str, ) def test_scan_sys_path( @@ -492,6 +487,7 @@ def test_scan_sys_path( :param runtime_tmp: Fixture for a Runtime object :param param: The parameters for the test """ + tmp_path = Path(site.getsitepackages()[0]) runtime_tmp.install_collection( V2_COLLECTION_TARBALL, destination=tmp_path, @@ -929,11 +925,3 @@ def test_galaxy_path(path: str, result: list[str]) -> None: def test_is_url(name: str, result: bool) -> None: """Checks functionality of is_url.""" assert is_url(name) == result - - -def test_runtime_site_package_path() -> None: - """Test site package paths.""" - runtime = Runtime() - site_paths = site.getsitepackages() - for path in site_paths: - assert path in runtime.config.collections_paths diff --git a/tox.ini b/tox.ini index d4f5afb3..4d3ee495 100644 --- a/tox.ini +++ b/tox.ini @@ -71,7 +71,7 @@ setenv = PIP_DISABLE_PIP_VERSION_CHECK = 1 PIP_CONSTRAINT = {toxinidir}/requirements.txt PRE_COMMIT_COLOR = always - PYTEST_REQPASS = 94 + PYTEST_REQPASS = 90 FORCE_COLOR = 1 allowlist_externals = ansible From 67b79ff5239e2e76aedf510a5251cbb64b8a208b Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Fri, 25 Aug 2023 19:40:03 +0530 Subject: [PATCH 3/6] Fix lint ci --- src/ansible_compat/runtime.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index c841a495..b37edc2c 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -243,7 +243,9 @@ def _add_sys_path_to_collection_paths(self) -> None: path not in self.config.collections_paths and Path(Path(path) / "ansible_collections").is_dir() ): - self.config.collections_paths.append(path) # pylint: disable=E1101 + self.config.collections_paths.append( # noqa: PERF401 pylint: disable=E1101 + path, + ) def load_collections(self) -> None: """Load collection data.""" From 0324d8f3499847f5af3e74648c579d516fabdf0d Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Fri, 25 Aug 2023 20:53:48 +0530 Subject: [PATCH 4/6] Add back collection_scan_sys_path check --- src/ansible_compat/runtime.py | 2 +- test/test_runtime.py | 7 ++++++- tox.ini | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index b37edc2c..2b94e5f7 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -237,7 +237,7 @@ def warning( def _add_sys_path_to_collection_paths(self) -> None: """Add the sys.path to the collection paths.""" - if not self.isolated: + if not self.isolated and self.config.collections_scan_sys_path: for path in sys.path: if ( path not in self.config.collections_paths diff --git a/test/test_runtime.py b/test/test_runtime.py index c3a5ebb7..24a79aa5 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -471,7 +471,12 @@ def __str__(self) -> str: @pytest.mark.parametrize( ("param"), - (ScanSysPath(isolated=False, scan=True, expected=True),), + ( + ScanSysPath(isolated=True, scan=True, expected=False), + ScanSysPath(isolated=True, scan=False, expected=False), + ScanSysPath(isolated=False, scan=True, expected=True), + ScanSysPath(isolated=False, scan=False, expected=False), + ), ids=str, ) def test_scan_sys_path( diff --git a/tox.ini b/tox.ini index 4d3ee495..e9613ee7 100644 --- a/tox.ini +++ b/tox.ini @@ -71,7 +71,7 @@ setenv = PIP_DISABLE_PIP_VERSION_CHECK = 1 PIP_CONSTRAINT = {toxinidir}/requirements.txt PRE_COMMIT_COLOR = always - PYTEST_REQPASS = 90 + PYTEST_REQPASS = 93 FORCE_COLOR = 1 allowlist_externals = ansible From 026e6a454d1961f3e4eab2770057e27abaa3326f Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Fri, 25 Aug 2023 21:07:59 +0530 Subject: [PATCH 5/6] Add back collection_scan_sys_path check --- test/test_runtime.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/test_runtime.py b/test/test_runtime.py index 24a79aa5..63017eea 100644 --- a/test/test_runtime.py +++ b/test/test_runtime.py @@ -5,7 +5,6 @@ import logging import os import pathlib -import site import subprocess from contextlib import contextmanager from dataclasses import dataclass, fields @@ -492,7 +491,6 @@ def test_scan_sys_path( :param runtime_tmp: Fixture for a Runtime object :param param: The parameters for the test """ - tmp_path = Path(site.getsitepackages()[0]) runtime_tmp.install_collection( V2_COLLECTION_TARBALL, destination=tmp_path, From eb17fcc17a01b61c167ec62166461cd236861938 Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Tue, 29 Aug 2023 14:58:09 +0530 Subject: [PATCH 6/6] Remove extra Path --- src/ansible_compat/runtime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansible_compat/runtime.py b/src/ansible_compat/runtime.py index 2b94e5f7..60cd058b 100644 --- a/src/ansible_compat/runtime.py +++ b/src/ansible_compat/runtime.py @@ -241,7 +241,7 @@ def _add_sys_path_to_collection_paths(self) -> None: for path in sys.path: if ( path not in self.config.collections_paths - and Path(Path(path) / "ansible_collections").is_dir() + and (Path(path) / "ansible_collections").is_dir() ): self.config.collections_paths.append( # noqa: PERF401 pylint: disable=E1101 path,