Skip to content

Commit 85268c2

Browse files
authored
Complete tests for installer (#241)
1 parent 33b3d34 commit 85268c2

File tree

6 files changed

+792
-63
lines changed

6 files changed

+792
-63
lines changed

src/ansible_dev_environment/subcommands/installer.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def _install_galaxy_collections(
149149
150150
Args:
151151
collections: The collection objects.
152+
153+
Raises:
154+
SystemExit: If the collection installation fails.
152155
"""
153156
collections_str = " ".join(
154157
[f"'{collection.original}'" for collection in collections],
@@ -187,27 +190,19 @@ def _install_galaxy_collections(
187190
except subprocess.CalledProcessError as exc:
188191
err = f"Failed to install collection: {exc}\n{exc.stderr}"
189192
self._output.critical(err)
190-
return
193+
raise SystemError(err) from exc # pragma: no cover # critical exits
191194
installed = self.RE_GALAXY_INSTALLED.findall(proc.stdout)
192195
msg = f"Installed collections include: {oxford_join(installed)}"
193196
self._output.note(msg)
194197

195198
def _install_galaxy_requirements(self: Installer) -> None:
196199
"""Install the collections using requirements.yml."""
197-
if self._config.args.requirement and not self._config.args.cpi:
198-
msg = f"Installing collections from requirements file: {self._config.args.requirement}"
199-
self._output.info(msg)
200-
collections = collections_from_requirements(
201-
file=self._config.args.requirement,
202-
)
203-
elif self._config.args.cpi:
204-
msg = "Source installing collections from requirements file source-requirement.yml"
205-
self._output.info(msg)
206-
collections = collections_from_requirements(
207-
file=self._config.args.requirement,
208-
)
209-
else:
210-
collections = []
200+
method = "Pre-installing" if self._config.args.cpi else "Installing"
201+
msg = f"{method} collections from requirements file: {self._config.args.requirement}"
202+
203+
self._output.info(msg)
204+
205+
collections = collections_from_requirements(file=self._config.args.requirement)
211206

212207
for collection in collections:
213208
cnamespace = collection["name"].split(".")[0]
@@ -312,7 +307,7 @@ def _find_files_using_ls(
312307

313308
def _copy_repo_files(
314309
self: Installer,
315-
local_repo_path: Path | None,
310+
local_repo_path: Path,
316311
destination_path: Path,
317312
) -> None:
318313
"""Copy collection files tracked in git to the build directory.
@@ -321,12 +316,10 @@ def _copy_repo_files(
321316
local_repo_path: The collection local path.
322317
destination_path: The build destination path.
323318
324-
"""
325-
if local_repo_path is None:
326-
msg = "Invalid repo path, no files to copy"
327-
self._output.debug(msg)
328-
return
319+
Raises:
320+
SystemExit: If no files are found.
329321
322+
"""
330323
# Get tracked files from git ls-files command
331324
found_using, files_stdout = self._find_files_using_git_ls_files(
332325
local_repo_path=local_repo_path,
@@ -340,7 +333,7 @@ def _copy_repo_files(
340333
if not files_stdout:
341334
msg = "No files found with either 'git ls-files' or 'ls"
342335
self._output.critical(msg)
343-
return
336+
raise SystemExit(msg) # pragma: no cover # critical exits
344337

345338
msg = f"File list generated with '{found_using}'"
346339
self._output.info(msg)
@@ -380,6 +373,7 @@ def _install_local_collection(
380373
381374
Raises:
382375
RuntimeError: If tarball is not found or if more than one tarball is found.
376+
SystemExit: If the collection installation fails.
383377
"""
384378
msg = f"Installing local collection from: {collection.build_dir}"
385379
self._output.info(msg)
@@ -464,7 +458,7 @@ def _install_local_collection(
464458
except subprocess.CalledProcessError as exc:
465459
err = f"Failed to install collection: {exc} {exc.stderr}"
466460
self._output.critical(err)
467-
return
461+
raise SystemError(err) from exc # pragma: no cover # critical exits
468462

469463
# ansible-galaxy collection install does not include the galaxy.yml for version
470464
# nor does it create an info file that can be used to determine the version.
@@ -490,15 +484,10 @@ def _swap_editable_collection(self: Installer, collection: Collection) -> None:
490484
Args:
491485
collection: The collection object.
492486
493-
Raises:
494-
RuntimeError: If the collection path is not set.
495487
"""
496488
msg = f"Swapping {collection.name} with {collection.path}"
497489
self._output.info(msg)
498490

499-
if collection.path is None:
500-
msg = "Collection path not set"
501-
raise RuntimeError(msg)
502491
msg = f"Removing installed {collection.site_pkg_path}"
503492
self._output.debug(msg)
504493
if collection.site_pkg_path.exists():

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import json
2020
import os
2121
import shutil
22+
import tarfile
2223
import tempfile
2324
import warnings
2425

@@ -137,6 +138,34 @@ def fixture_session_dir() -> Generator[Path, None, None]:
137138
shutil.rmtree(temp_dir)
138139

139140

141+
@pytest.fixture()
142+
def installable_local_collection(tmp_path: Path) -> Path:
143+
"""Provide a local collection that can be installed.
144+
145+
Args:
146+
tmp_path: Temporary directory.
147+
148+
Returns:
149+
The path to the local collection.
150+
"""
151+
src_dir = tmp_path / "ansible.posix"
152+
tar_file_path = next(GALAXY_CACHE.glob("ansible-posix*"))
153+
with tarfile.open(tar_file_path, "r") as tar:
154+
try:
155+
tar.extractall(src_dir, filter="data")
156+
except TypeError:
157+
tar.extractall(src_dir) # noqa: S202
158+
galaxy_contents = {
159+
"authors": "author",
160+
"name": "posix",
161+
"namespace": "ansible",
162+
"readme": "readme",
163+
"version": "1.0.0",
164+
}
165+
yaml.dump(galaxy_contents, (src_dir / "galaxy.yml").open("w"))
166+
return src_dir
167+
168+
140169
@pytest.fixture(scope="session")
141170
def session_venv(session_dir: Path, monkey_session: pytest.MonkeyPatch) -> Config:
142171
"""Create a temporary venv for the session.

tests/unit/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def output(tmp_path: Path) -> Output:
2929
log_level="notset",
3030
log_append="false",
3131
term_features=TermFeatures(color=False, links=False),
32-
verbosity=0,
32+
verbosity=3,
3333
)
3434

3535

0 commit comments

Comments
 (0)