Skip to content

Commit

Permalink
Add sys.path to collection paths (#318)
Browse files Browse the repository at this point in the history
* Add sys.path to collection paths

* Undo move

* Test count

* Pylint is confused

* Flag raised missing

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cidrblock and pre-commit-ci[bot] authored Aug 21, 2023
1 parent 01f0448 commit baca1c6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import shutil
import subprocess
import sys
import tempfile
import warnings
from collections import OrderedDict
Expand Down Expand Up @@ -204,6 +205,9 @@ def __init__(
self.cache_dir = get_cache_dir(self.project_dir)
self.config = AnsibleConfig()

# Add the sys.path to the collection paths if not isolated
self._add_sys_path_to_collection_paths()

if not self.version_in_range(lower=min_required_version):
msg = f"Found incompatible version of ansible runtime {self.version}, instead of {min_required_version} or newer."
raise RuntimeError(msg)
Expand Down Expand Up @@ -231,6 +235,11 @@ def warning(
# Monkey patch ansible warning in order to use warnings module.
Display.warning = 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

def load_collections(self) -> None:
"""Load collection data."""
self.collections = OrderedDict()
Expand Down
67 changes: 67 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import subprocess
from contextlib import contextmanager
from dataclasses import dataclass, fields
from pathlib import Path
from shutil import rmtree
from typing import TYPE_CHECKING, Any
Expand All @@ -29,6 +30,8 @@
from _pytest.monkeypatch import MonkeyPatch
from pytest_mock import MockerFixture

V2_COLLECTION_TARBALL = Path("examples/reqs_v2/community-molecule-0.1.0.tar.gz")


def test_runtime_version(runtime: Runtime) -> None:
"""Tests version property."""
Expand Down Expand Up @@ -444,6 +447,70 @@ def test_require_collection(runtime_tmp: Runtime) -> None:
runtime_tmp.require_collection("community.molecule", "0.1.0")


@dataclass
class ScanSysPath:
"""Parameters for scan tests."""

isolated: bool
scan: bool
expected: bool

def __str__(self) -> str:
"""Return a string representation of the object."""
parts = [
f"{field.name}{str(getattr(self, field.name))[0]}" for field in fields(self)
]
return "-".join(parts)


@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),
),
ids=str,
)
def test_scan_sys_path(
monkeypatch: MonkeyPatch,
tmp_path: Path,
runtime_tmp: Runtime,
param: ScanSysPath,
) -> None:
"""Confirm sys path is scanned for collections.
:param monkeypatch: Fixture for monkeypatching
:param tmp_path: Fixture for a temp directory
:param runtime_tmp: Fixture for a Runtime object
:param param: The parameters for the test
"""
runtime_tmp.install_collection(
V2_COLLECTION_TARBALL,
destination=tmp_path,
)
runtime_tmp.config.collections_paths.remove(str(tmp_path))

# Set the runtime to the test parameters
runtime_tmp.isolated = param.isolated
runtime_tmp.config.collections_scan_sys_path = param.scan
monkeypatch.syspath_prepend(str(tmp_path))
runtime_tmp._add_sys_path_to_collection_paths()

try:
runtime_tmp.require_collection(
name="community.molecule",
version="0.1.0",
install=False,
)
raised_missing = False
except InvalidPrerequisiteError:
raised_missing = True

assert param.expected != raised_missing


@pytest.mark.parametrize(
("name", "version", "install"),
(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ setenv =
PIP_DISABLE_PIP_VERSION_CHECK = 1
PIP_CONSTRAINT = {toxinidir}/requirements.txt
PRE_COMMIT_COLOR = always
PYTEST_REQPASS = 85
PYTEST_REQPASS = 89
FORCE_COLOR = 1
allowlist_externals =
ansible
Expand Down

0 comments on commit baca1c6

Please sign in to comment.