Skip to content

Commit

Permalink
Make Runtime.project_dir use Path (#244)
Browse files Browse the repository at this point in the history
* Make Runtime.project_dir use Path

* 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
ssbarnea and pre-commit-ci[bot] committed Apr 28, 2023
1 parent eb779e7 commit 3f1eb4c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Runtime:
# pylint: disable=too-many-arguments
def __init__(
self,
project_dir: Optional[str] = None,
project_dir: Optional[Path] = None,
isolated: bool = False,
min_required_version: Optional[str] = None,
require_module: bool = False,
Expand Down Expand Up @@ -101,7 +101,7 @@ def __init__(
self.environ["PYTHONWARNINGS"] = "ignore:Blowfish has been deprecated"

if isolated:
self.cache_dir = get_cache_dir(self.project_dir)
self.cache_dir = get_cache_dir(str(self.project_dir))
self.config = AnsibleConfig()

if not self.version_in_range(lower=min_required_version):
Expand Down Expand Up @@ -462,7 +462,7 @@ def prepare_environment(
else:
# no collection, try to recognize and install a standalone role
self._install_galaxy_role(
self.project_dir,
str(self.project_dir),
role_name_check=role_name_check,
ignore_errors=True,
)
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ def runtime_tmp(
tmp_path: pathlib.Path, scope: str = "session"
) -> Generator[Runtime, None, None]:
"""Isolated runtime fixture using a temp directory."""
instance = Runtime(project_dir=str(tmp_path), isolated=True)
instance = Runtime(project_dir=tmp_path, isolated=True)
yield instance
instance.clean()
24 changes: 14 additions & 10 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_runtime_install_role(
"""Checks that we can install roles."""
caplog.set_level(logging.INFO)
project_dir = Path(__file__).parent / "roles" / folder
runtime = Runtime(isolated=isolated, project_dir=str(project_dir))
runtime = Runtime(isolated=isolated, project_dir=project_dir)
runtime.prepare_environment(install_local=True)
# check that role appears as installed now
result = runtime.exec(["ansible-galaxy", "list"])
Expand All @@ -161,7 +161,7 @@ def test_runtime_install_role(

def test_prepare_environment_with_collections(tmp_path: pathlib.Path) -> None:
"""Check that collections are correctly installed."""
runtime = Runtime(isolated=True, project_dir=str(tmp_path))
runtime = Runtime(isolated=True, project_dir=tmp_path)
runtime.prepare_environment(required_collections={"community.molecule": "0.1.0"})


Expand Down Expand Up @@ -397,7 +397,7 @@ def test_require_collection_invalid_collections_path(runtime: Runtime) -> None:

def test_require_collection_preexisting_broken(tmp_path: pathlib.Path) -> None:
"""Check that require_collection raise with broken pre-existing collection."""
runtime = Runtime(isolated=True, project_dir=str(tmp_path))
runtime = Runtime(isolated=True, project_dir=tmp_path)
dest_path: str = runtime.config.collections_paths[0]
dest = pathlib.Path(dest_path) / "ansible_collections" / "foo" / "bar"
dest.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -459,14 +459,16 @@ def test_install_galaxy_role(runtime_tmp: Runtime) -> None:
pathlib.Path(f"{runtime_tmp.project_dir}/meta").mkdir()
pathlib.Path(f"{runtime_tmp.project_dir}/meta/main.yml").touch()
# this should only raise a warning
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=1)
runtime_tmp._install_galaxy_role(str(runtime_tmp.project_dir), role_name_check=1)
# this should test the bypass role name check path
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=2)
runtime_tmp._install_galaxy_role(str(runtime_tmp.project_dir), role_name_check=2)
# this should raise an error
with pytest.raises(
InvalidPrerequisiteError, match="does not follow current galaxy requirements"
):
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=0)
runtime_tmp._install_galaxy_role(
str(runtime_tmp.project_dir), role_name_check=0
)


def test_install_galaxy_role_unlink(
Expand All @@ -485,7 +487,7 @@ def test_install_galaxy_role_unlink(
""",
encoding="utf-8",
)
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir)
runtime_tmp._install_galaxy_role(str(runtime_tmp.project_dir))
assert "symlink to current repository" in caplog.text


Expand All @@ -502,7 +504,9 @@ def test_install_galaxy_role_bad_namespace(runtime_tmp: Runtime) -> None:
)
# this should raise an error regardless the role_name_check value
with pytest.raises(AnsibleCompatError, match="Role namespace must be string, not"):
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=1)
runtime_tmp._install_galaxy_role(
str(runtime_tmp.project_dir), role_name_check=1
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -531,7 +535,7 @@ def test_install_galaxy_role_name_role_name_check_equals_to_1(
encoding="utf-8",
)

runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=1)
runtime_tmp._install_galaxy_role(str(runtime_tmp.project_dir), role_name_check=1)
assert "Computed fully qualified role name of " in caplog.text


Expand All @@ -546,7 +550,7 @@ def test_install_galaxy_role_no_checks(runtime_tmp: Runtime) -> None:
namespace: acme
"""
)
runtime_tmp._install_galaxy_role(runtime_tmp.project_dir, role_name_check=2)
runtime_tmp._install_galaxy_role(str(runtime_tmp.project_dir), role_name_check=2)
result = runtime_tmp.exec(["ansible-galaxy", "list"])
assert "- acme.foo," in result.stdout
assert result.returncode == 0, result
Expand Down

0 comments on commit 3f1eb4c

Please sign in to comment.