Skip to content
Merged
23 changes: 23 additions & 0 deletions news/printing-info.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added ``print_info`` function.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
22 changes: 22 additions & 0 deletions src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,25 @@ def install_pack(self, identifier: str | Path) -> None:
plog.info("Pack '%s' installation complete.", path.stem)
else:
plog.error("Pack '%s' installation failed.", path.stem)

def print_info(self) -> None:
"""Print information about available packs and examples."""
uninstalled_packs = []
installed_packs = []
for pack in self.available_packs():
if self.check_pack(pack):
installed_packs.append(pack)
else:
uninstalled_packs.append(pack)
print("Installed Packs:")
for pack in installed_packs:
print(f" {pack}")
print("\nAvailable Packs to Install:")
for pack in uninstalled_packs:
print(f" {pack}")
print("\nExamples:")
examples_dict = self.available_examples()
for pack, examples in examples_dict.items():
print(f" {pack}:")
for ex_name, _ in examples:
print(f" - {ex_name}")
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def example_cases(tmp_path_factory):
case5req_dir = root_temp_dir / "case5" / "requirements" / "packs"
case5req_dir.mkdir(parents=True, exist_ok=True)

fake_env = root_temp_dir / "case5" / "fake_env"
fake_env.mkdir(parents=True, exist_ok=True)
(case5req_dir / "packA.txt").write_text("requests")
(case5req_dir / "packB.txt").write_text("attrs")

yield root_temp_dir


Expand Down
57 changes: 57 additions & 0 deletions tests/test_packsmanager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import subprocess
from pathlib import Path

import pytest
Expand Down Expand Up @@ -342,3 +343,59 @@ def test_copy_examples_force(example_cases, expected_paths, force):
original_path = examples_dir / path
if copied_path.is_file() and original_path.is_file():
assert copied_path.read_text() == original_path.read_text()


install_params = [
( # input: install requirements for packA
# expected: print_info output showing packA installed but not packB
("packA",),
"""Installed Packs:
packA

Available Packs to Install:
packB

Examples:
packA:
- ex1
- ex2
packB:
- ex1
- ex3
- ex4""",
),
]


@pytest.mark.parametrize("packs_to_install,expected", install_params)
def test_print_info(packs_to_install, expected, example_cases, capsys):
case5dir = example_cases / "case5"
env_dir = case5dir / "fake_env"
req_dir = case5dir / "requirements" / "packs"
subprocess.run(
["conda", "create", "-y", "-p", str(env_dir)],
check=True,
capture_output=True,
text=True,
)
for pack in packs_to_install:
req_file = req_dir / f"{pack}.txt"
subprocess.run(
[
"conda",
"install",
"-y",
"--file",
str(req_file),
"-p",
str(env_dir),
],
check=True,
capture_output=True,
text=True,
)
pm = PacksManager(root_path=case5dir)
pm.print_info()
captured = capsys.readouterr()
actual = captured.out
assert actual.strip() == expected.strip()
Loading