Skip to content

Commit 47456c8

Browse files
authored
Merge pull request #91 from cadenmyers13/print-profiles-test
tes: `print_profiles` test
2 parents 296c941 + 12fc7d3 commit 47456c8

File tree

5 files changed

+119
-12
lines changed

5 files changed

+119
-12
lines changed

news/print-profiles-test.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Add test for ``print_profiles``.
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/cmi/profilesmanager.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,26 @@
2626
presence_check,
2727
)
2828
from diffpy.cmi.log import plog
29-
from diffpy.cmi.packsmanager import PacksManager
29+
from diffpy.cmi.packsmanager import PacksManager, get_package_dir
3030

3131
__all__ = ["Profile", "ProfilesManager"]
3232

3333

34+
def _installed_profiles_dir(root_path=None) -> Path:
35+
"""Locate requirements/profiles/ for the installed package."""
36+
with get_package_dir(root_path) as pkgdir:
37+
pkg = Path(pkgdir).resolve()
38+
for c in (
39+
pkg / "requirements" / "profiles",
40+
pkg.parents[2] / "requirements" / "profiles",
41+
):
42+
if c.is_dir():
43+
return c
44+
raise FileNotFoundError(
45+
"Could not locate requirements/profiles. Check your installation."
46+
)
47+
48+
3449
@dataclass
3550
class Profile:
3651
"""Container for a resolved profile.
@@ -78,9 +93,14 @@ class ProfilesManager:
7893
Defaults to `requirements/profiles` under the installed package.
7994
"""
8095

81-
def __init__(self, packs_mgr: Optional[PacksManager] = None) -> None:
82-
self.packs_mgr = packs_mgr or PacksManager()
83-
self.profiles_dir = self.packs_mgr.packs_dir.parent / "profiles"
96+
def __init__(
97+
self,
98+
packs_mgr: Optional[PacksManager] = None,
99+
root_path=None,
100+
) -> None:
101+
102+
self.packs_mgr = packs_mgr or PacksManager(root_path=root_path)
103+
self.profiles_dir = _installed_profiles_dir(root_path)
84104

85105
# Resolution & loading
86106
def _resolve_profile_file(self, identifier: Union[str, Path]) -> Path:

tests/conftest.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,30 @@ def example_cases(tmp_path_factory):
9393
case5e.mkdir(parents=True, exist_ok=True)
9494
(case5e / "script5.py").write_text(f"# {case5e.name} script5\n")
9595

96-
case5req_dir = root_temp_dir / "case5" / "requirements" / "packs"
97-
case5req_dir.mkdir(parents=True, exist_ok=True)
96+
case5reqs_dir = root_temp_dir / "case5" / "requirements"
97+
case5packs_dir = case5reqs_dir / "packs"
98+
case5packs_dir.mkdir(parents=True, exist_ok=True)
99+
(case5packs_dir / "packA.txt").write_text("requests")
100+
(case5packs_dir / "packB.txt").write_text("attrs")
101+
102+
case5profiles_dir = case5reqs_dir / "profiles"
103+
case5profiles_dir.mkdir(parents=True, exist_ok=True)
104+
profileAyml = """\
105+
packs:
106+
- packA
107+
108+
extras:
109+
- ipykernel
110+
"""
111+
profileByml = """\
112+
packs:
113+
- packB
98114
99-
fake_env = root_temp_dir / "case5" / "fake_env"
100-
fake_env.mkdir(parents=True, exist_ok=True)
101-
(case5req_dir / "packA.txt").write_text("requests")
102-
(case5req_dir / "packB.txt").write_text("attrs")
115+
extras:
116+
- notebook
117+
"""
118+
(case5profiles_dir / "profileA.yml").write_text(profileAyml)
119+
(case5profiles_dir / "profileB.yml").write_text(profileByml)
103120

104121
yield root_temp_dir
105122

tests/test_packsmanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ def test_copy_examples_force(example_cases, expected_paths, force):
346346

347347

348348
install_params = [
349-
( # input: install requirements for packA
350-
# expected: print_info output showing packA installed but not packB
349+
( # input: packs to install
350+
# expected: output showing packA installed but not packB
351351
("packA",),
352352
"""Installed Packs:
353353
----------------

tests/test_profilesmanager.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
import yaml
3+
4+
from diffpy.cmi.profilesmanager import ProfilesManager
5+
6+
install_params = [
7+
( # input: profiles to install
8+
# expected: print_profile output showing profileA
9+
# installed but not profileB
10+
("profileA",),
11+
"""Installed Profiles:
12+
-------------------
13+
profileA
14+
15+
Available Profiles:
16+
-------------------
17+
profileB
18+
""",
19+
),
20+
]
21+
22+
23+
@pytest.mark.parametrize("profiles_to_install,expected", install_params)
24+
def test_print_profiles(
25+
profiles_to_install, expected, example_cases, capsys, mocker
26+
):
27+
case5dir = example_cases / "case5"
28+
req_dir = case5dir / "requirements" / "profiles"
29+
30+
installed_reqs = []
31+
for profile in profiles_to_install:
32+
req_file = req_dir / f"{profile}.yml"
33+
content = yaml.safe_load(req_file.read_text())
34+
for pack in content.get("packs", []):
35+
installed_reqs.append(pack)
36+
37+
def fake_check_profile(identifier):
38+
return identifier == "profileA"
39+
40+
mocker.patch.object(
41+
ProfilesManager, "check_profile", side_effect=fake_check_profile
42+
)
43+
pfm = ProfilesManager(root_path=case5dir)
44+
pfm.print_profiles()
45+
captured = capsys.readouterr()
46+
actual = captured.out
47+
assert actual.strip() == expected.strip()

0 commit comments

Comments
 (0)