diff --git a/news/printing-info.rst b/news/printing-info.rst new file mode 100644 index 0000000..7d48461 --- /dev/null +++ b/news/printing-info.rst @@ -0,0 +1,23 @@ +**Added:** + +* Added ``print_info`` function. + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/cmi/packsmanager.py b/src/diffpy/cmi/packsmanager.py index 8c2f431..4c9d08e 100644 --- a/src/diffpy/cmi/packsmanager.py +++ b/src/diffpy/cmi/packsmanager.py @@ -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}") diff --git a/tests/conftest.py b/tests/conftest.py index 263a20a..af72bd5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_packsmanager.py b/tests/test_packsmanager.py index 7f02b1a..10bf5ab 100644 --- a/tests/test_packsmanager.py +++ b/tests/test_packsmanager.py @@ -1,5 +1,6 @@ import os import re +import subprocess from pathlib import Path import pytest @@ -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()