Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage #38

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

@ssbarnea ssbarnea Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webknjaz (line 381) apparently even with our type hints added to Config class, mypy still believes that any Config.attribute is of type object.

I am not pleased about this dirty trick because it means that this would be expected from consumers of this library too. Any idea on how to avoid that?

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