From ab35d757fdf38c2afaeff4a841c12f82c6d9d418 Mon Sep 17 00:00:00 2001 From: Caden Myers Date: Mon, 17 Nov 2025 14:25:45 -0500 Subject: [PATCH 1/4] monkeypatch _is_installed function --- tests/test_packsmanager.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/test_packsmanager.py b/tests/test_packsmanager.py index c0adf49..2bf6beb 100644 --- a/tests/test_packsmanager.py +++ b/tests/test_packsmanager.py @@ -1,10 +1,10 @@ import os import re -import subprocess from pathlib import Path import pytest +from diffpy.cmi import installer from diffpy.cmi.packsmanager import PacksManager @@ -372,21 +372,24 @@ def test_copy_examples_force(example_cases, expected_paths, force): @pytest.mark.parametrize("packs_to_install,expected", install_params) def test_print_packs_and_examples( - packs_to_install, expected, example_cases, capsys, conda_env + packs_to_install, expected, example_cases, capsys, monkeypatch ): - env_dir_str = Path(conda_env).as_posix() - shell = os.name == "nt" - req_dir = example_cases / "case5" / "requirements" / "packs" + case5dir = example_cases / "case5" + req_dir = case5dir / "requirements" / "packs" + + installed_reqs = [] for pack in packs_to_install: - req_file = (req_dir / f"{pack}.txt").as_posix() - subprocess.run( - ["conda", "install", "-y", "--file", req_file, "-p", env_dir_str], - check=True, - capture_output=True, - text=True, - shell=shell, - ) - pm = PacksManager(root_path=example_cases / "case5") + req_file = req_dir / f"{pack}.txt" + for line in req_file.read_text().splitlines(): + line = line.strip() + installed_reqs.append(line) + + def mock_is_installed(name: str) -> bool: + return name in installed_reqs + + monkeypatch.setattr(installer, "_is_installed", mock_is_installed) + + pm = PacksManager(root_path=case5dir) pm.print_packs() pm.print_examples() captured = capsys.readouterr() From 0022c62f4198d6744cb893ca14540955498c6362 Mon Sep 17 00:00:00 2001 From: Caden Myers Date: Mon, 17 Nov 2025 14:27:18 -0500 Subject: [PATCH 2/4] news --- news/fix-print-test.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 news/fix-print-test.rst diff --git a/news/fix-print-test.rst b/news/fix-print-test.rst new file mode 100644 index 0000000..27fc85d --- /dev/null +++ b/news/fix-print-test.rst @@ -0,0 +1,23 @@ +**Added:** + +* No news needed. + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* From fe89f35d4fa0a688087a31bd9835d0ddbec395b6 Mon Sep 17 00:00:00 2001 From: Caden Myers Date: Mon, 17 Nov 2025 14:28:30 -0500 Subject: [PATCH 3/4] rm conda_env fixture --- tests/conftest.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 0f51a62..af72bd5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,31 +1,10 @@ import json -import os -import subprocess from pathlib import Path import matplotlib import pytest -@pytest.fixture(scope="function") -def conda_env(tmp_path): - env_dir = tmp_path / "fake_env" - env_dir_str = env_dir.as_posix() - shell = os.name == "nt" - subprocess.run( - ["conda", "create", "-y", "-p", env_dir_str], - check=True, - capture_output=True, - shell=shell, - ) - yield env_dir_str - subprocess.run( - ["conda", "env", "remove", "-p", env_dir_str, "-y"], - check=True, - shell=shell, - ) - - @pytest.fixture(scope="function") def example_cases(tmp_path_factory): """Copy the entire examples tree into a temp directory once per test From cd1eb204cd6520f291189f38a1beaff0de351a60 Mon Sep 17 00:00:00 2001 From: Caden Myers Date: Mon, 17 Nov 2025 15:52:14 -0500 Subject: [PATCH 4/4] use mocker instead of monkeypatch --- tests/test_packsmanager.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/test_packsmanager.py b/tests/test_packsmanager.py index 2bf6beb..a870479 100644 --- a/tests/test_packsmanager.py +++ b/tests/test_packsmanager.py @@ -372,7 +372,7 @@ def test_copy_examples_force(example_cases, expected_paths, force): @pytest.mark.parametrize("packs_to_install,expected", install_params) def test_print_packs_and_examples( - packs_to_install, expected, example_cases, capsys, monkeypatch + packs_to_install, expected, example_cases, capsys, mocker ): case5dir = example_cases / "case5" req_dir = case5dir / "requirements" / "packs" @@ -382,13 +382,15 @@ def test_print_packs_and_examples( req_file = req_dir / f"{pack}.txt" for line in req_file.read_text().splitlines(): line = line.strip() - installed_reqs.append(line) + if line and not line.startswith("#"): + installed_reqs.append(line) def mock_is_installed(name: str) -> bool: return name in installed_reqs - monkeypatch.setattr(installer, "_is_installed", mock_is_installed) - + mocker.patch.object( + installer, "_is_installed", side_effect=mock_is_installed + ) pm = PacksManager(root_path=case5dir) pm.print_packs() pm.print_examples()