Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eb85d97
test for copy_examples
cadenmyers13 Oct 13, 2025
918c559
news
cadenmyers13 Oct 13, 2025
6a26c9e
rm comment
cadenmyers13 Oct 13, 2025
3ab2efb
rm old tests
cadenmyers13 Oct 13, 2025
678d0b9
add user_input as param for copy_examples
cadenmyers13 Oct 14, 2025
15fd82a
add user inputs to test
cadenmyers13 Oct 14, 2025
d57e7fa
add cwd and user_target at same level as cases in tmp dir
cadenmyers13 Oct 14, 2025
79fa679
modify test for new tmp dir structure
cadenmyers13 Oct 14, 2025
a1fcf3b
fix testing matrix
cadenmyers13 Oct 14, 2025
fc1d0b0
add test for bad copy cases
cadenmyers13 Oct 14, 2025
a50c442
rm docstring
cadenmyers13 Oct 14, 2025
a04de5a
Add FileExistError test case
cadenmyers13 Oct 14, 2025
aa453b7
reverse logic for target_dir
cadenmyers13 Oct 14, 2025
e75a339
add additional dirs to tmp dir
cadenmyers13 Oct 15, 2025
f750453
move copy_examples inside PacksManager
cadenmyers13 Oct 15, 2025
49e745e
move copy_examples tests inside pm test
cadenmyers13 Oct 15, 2025
14fac59
rm tests from test_cli
cadenmyers13 Oct 15, 2025
909da70
add bad_target test
cadenmyers13 Oct 15, 2025
e66eb5f
rm duplicate copy_examples() in cli.py
cadenmyers13 Oct 16, 2025
42553c1
update docstring and param name
cadenmyers13 Oct 16, 2025
ef792d2
wrap str as list, add 4) to bad input test
cadenmyers13 Oct 16, 2025
4dc1538
rm param name from func signature
cadenmyers13 Oct 17, 2025
5820dd3
change scope from session to function
cadenmyers13 Oct 17, 2025
84e54a8
rglob examples after they are copied
cadenmyers13 Oct 17, 2025
6c38885
write text to the files and make check that the copied files have the…
cadenmyers13 Oct 17, 2025
3405e17
check exception message rather than the exception type
cadenmyers13 Oct 17, 2025
b8e4336
copy_examples returns None
cadenmyers13 Oct 17, 2025
1324185
sort list of paths, rm bad target test
cadenmyers13 Oct 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/copy-func2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add test for ``copy_examples`` function.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
18 changes: 1 addition & 17 deletions src/diffpy/cmi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
from pathlib import Path
from shutil import copytree
from typing import Dict, List, Optional, Tuple
from typing import List, Optional, Tuple

from diffpy.cmi import __version__, get_package_dir
from diffpy.cmi.conda import env_info
Expand All @@ -25,22 +25,6 @@
from diffpy.cmi.profilesmanager import ProfilesManager


def copy_examples(
examples_dict: Dict[str, List[Tuple[str, Path]]], target_dir: Path = None
) -> None:
"""Copy an example into the the target or current working directory.

Parameters
----------
examples_dict : dict
Dictionary mapping pack name -> list of (example, path) tuples.
target_dir : pathlib.Path, optional
Target directory to copy examples into. Defaults to current
working directory.
"""
return


# Examples
def _get_examples_dir() -> Path:
"""Return the absolute path to the installed examples directory.
Expand Down
18 changes: 18 additions & 0 deletions src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ def available_examples(self) -> dict[str, List[tuple[str, Path]]]:
)
return examples_dict

def copy_examples(
self,
examples_to_copy: List[str],
target_dir: Path = None,
) -> None:
"""Copy examples or packs into the target or current working
directory.

Parameters
----------
examples_to_copy : list of str
User-specified pack(s), example(s), or "all" to copy all.
target_dir : pathlib.Path, optional
Target directory to copy examples into. Defaults to current
working directory.
"""
return

def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path:
"""Resolve a pack identifier to an absolute .txt path.

Expand Down
44 changes: 34 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ def tmp_examples(tmp_path_factory):
yield tmp_examples


@pytest.fixture(scope="session")
@pytest.fixture(scope="function")
def example_cases(tmp_path_factory):
"""Copy the entire examples tree into a temp directory once per test
session.

Returns the path to that copy.
"""
root_temp_dir = tmp_path_factory.mktemp("temp")
cwd = root_temp_dir / "cwd"
cwd.mkdir(parents=True, exist_ok=True)
user_target = root_temp_dir / "user_target"
user_target.mkdir(parents=True, exist_ok=True)

# case 1: pack with no examples
case1ex_dir = root_temp_dir / "case1" / "docs" / "examples"
Expand All @@ -44,30 +48,35 @@ def example_cases(tmp_path_factory):
case2ex_dir / "full_pack" / "ex1" / "solution" / "diffpy-cmi"
) # full_pack, ex1
case2a.mkdir(parents=True, exist_ok=True)
(case2a / "script1.py").touch()
(case2a / "script1.py").write_text(f"# {case2a.name} script1\n")

case2b = (
case2ex_dir / "full_pack" / "ex2" / "random" / "path"
) # full_pack, ex2
case2b.mkdir(parents=True, exist_ok=True)
(case2b / "script1.py").touch()
(case2b / "script2.py").touch()
(case2b / "script1.py").write_text(f"# {case2b.name} script1\n")
(case2b / "script2.py").write_text(f"# {case2b.name} script2\n")

case2req_dir = root_temp_dir / "case2" / "requirements" / "packs"
case2req_dir.mkdir(parents=True, exist_ok=True)

# Case 3: multiple packs with multiple examples
case3ex_dir = root_temp_dir / "case3" / "docs" / "examples"
case3a = case3ex_dir / "packA" / "ex1" # packA, ex1
case3a.mkdir(parents=True, exist_ok=True)
(case3a / "script1.py").touch()
(case3a / "script1.py").write_text(f"# {case3a.name} script1\n")

case3b = case3ex_dir / "packA" / "ex2" / "solutions" # packA, ex2
case3b.mkdir(parents=True, exist_ok=True)
(case3b / "script2.py").touch()
(case3b / "script2.py").write_text(f"# {case3b.name} script2\n")

case3c = (
case3ex_dir / "packB" / "ex3" / "more" / "random" / "path"
) # packB, ex3
case3c.mkdir(parents=True, exist_ok=True)
(case3c / "script3.py").touch()
(case3c / "script4.py").touch()
(case3c / "script3.py").write_text(f"# {case3c.name} script3\n")
(case3c / "script4.py").write_text(f"# {case3c.name} script4\n")

case3req_dir = root_temp_dir / "case3" / "requirements" / "packs"
case3req_dir.mkdir(parents=True, exist_ok=True)

Expand All @@ -80,12 +89,27 @@ def example_cases(tmp_path_factory):

# Case 5: multiple packs with the same example names
case5ex_dir = root_temp_dir / "case5" / "docs" / "examples"

case5a = case5ex_dir / "packA" / "ex1" / "path1" # packA, ex1
case5a.mkdir(parents=True, exist_ok=True)
(case5a / "script1.py").touch()
(case5a / "script1.py").write_text(f"# {case5a.name} script1\n")

case5b = case5ex_dir / "packB" / "ex1" / "path2" # packB, ex1
case5b.mkdir(parents=True, exist_ok=True)
(case5b / "script2.py").touch()
(case5b / "script2.py").write_text(f"# {case5b.name} script2\n")

case5c = case5ex_dir / "packA" / "ex2" # packA, ex2
case5c.mkdir(parents=True, exist_ok=True)
(case5c / "script3.py").write_text(f"# {case5c.name} script3\n")

case5d = case5ex_dir / "packB" / "ex3"
case5d.mkdir(parents=True, exist_ok=True)
(case5d / "script4.py").write_text(f"# {case5d.name} script4\n")

case5e = case5ex_dir / "packB" / "ex4"
case5e.mkdir(parents=True, exist_ok=True)
(case5e / "script5.py").write_text(f"# {case5e.name} script5\n")

case5req_dir = root_temp_dir / "case5" / "requirements" / "packs"
case5req_dir.mkdir(parents=True, exist_ok=True)

Expand Down
65 changes: 0 additions & 65 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,65 +0,0 @@
import os
from pathlib import Path

import pytest

from diffpy.cmi import cli


def test_map_pack_to_examples_structure():
"""Test that map_pack_to_examples returns the right shape of
data."""
actual = cli.map_pack_to_examples()
assert isinstance(actual, dict)
for pack, exdirs in actual.items():
assert isinstance(pack, str)
assert isinstance(exdirs, list)
for ex in exdirs:
assert isinstance(ex, str)
# Check for known packs
assert "core" in actual.keys()
assert "pdf" in actual.keys()
# Check for known examples
assert ["linefit"] in actual.values()


@pytest.mark.parametrize(
"input_valid_str",
[
"core/linefit",
"pdf/ch03NiModelling",
],
)
def test_copy_example_success(tmp_path, input_valid_str):
"""Given a valid example format (<pack>/<ex>), test that its copied
to the temp dir."""
os.chdir(tmp_path)
actual = cli.copy_example(input_valid_str)
expected = tmp_path / Path(input_valid_str).name
assert expected.exists() and expected.is_dir()
assert actual == expected


def test_copy_example_fnferror():
"""Test that FileNotFoundError is raised when the example does not
exist."""
with pytest.raises(FileNotFoundError):
cli.copy_example("pack/example1")


@pytest.mark.parametrize(
"input_bad_str",
[
"", # empty string
"/", # missing pack and example
"corelinefit", # missing slash
"linefit", # missing pack and slash
"core/", # missing example
"/linefit", # missing pack
"core/linefit/extra", # too many slashes
],
)
def test_copy_example_valueerror(input_bad_str):
"""Test that ValueError is raised when the format is invalid."""
with pytest.raises(ValueError):
cli.copy_example(input_bad_str)
Loading
Loading