Skip to content

Commit

Permalink
Increase test coverage (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Jul 27, 2021
1 parent 4084e7f commit 9521b49
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,9 @@ def require_collection( # noqa: C901
if os.path.exists(collpath):
mpath = os.path.join(collpath, 'MANIFEST.json')
if not os.path.exists(mpath):
_logger.fatal(
"Found collection at '%s' but missing MANIFEST.json, cannot get info.",
collpath,
)
raise InvalidPrerequisiteError()
msg = f"Found collection at '{collpath}' but missing MANIFEST.json, cannot get info."
_logger.fatal(msg)
raise InvalidPrerequisiteError(msg)

with open(mpath, 'r') as f:
manifest = json.loads(f.read())
Expand Down
25 changes: 25 additions & 0 deletions test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def test_runtime_install_role(
runtime.clean()


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.prepare_environment(required_collections={"community.molecule": "0.1.0"})


def test_runtime_install_requirements_missing_file() -> None:
"""Check that missing requirements file is ignored."""
# Do not rely on this behavior, it may be removed in the future
Expand Down Expand Up @@ -360,6 +366,25 @@ def test_require_collection_invalid_name(runtime: Runtime) -> None:
runtime.require_collection("that-is-invalid")


def test_require_collection_invalid_collections_path(runtime: Runtime) -> None:
"""Check that require_collection raise with invalid collections path."""
runtime.config.collections_path = '/that/is/invalid' # type: ignore
with pytest.raises(
InvalidPrerequisiteError, match="Unable to determine ansible collection paths"
):
runtime.require_collection("community.molecule")


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))
dest_path: str = runtime.config.collections_path[0] # type: ignore
dest = os.path.join(dest_path, "ansible_collections", "foo", "bar")
os.makedirs(dest, exist_ok=True)
with pytest.raises(InvalidPrerequisiteError, match="missing MANIFEST.json"):
runtime.require_collection("foo.bar")


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

0 comments on commit 9521b49

Please sign in to comment.