Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
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-func-test.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add test for ``copy_examples``.

**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__
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
87 changes: 86 additions & 1 deletion src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See LICENSE.rst for license information.
#
##############################################################################

import shutil
from importlib.resources import as_file
from pathlib import Path
from typing import List, Union
Expand Down Expand Up @@ -133,6 +133,91 @@ 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,
force: bool = False,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added new force input param

) -> 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.
force : bool, optional
If ``True``, overwrite existing files. Defaults to ``False``.
"""
self._target_dir = target_dir.resolve() if target_dir else Path.cwd()
self._force = force

if "all" in examples_to_copy:
self._copy_all()
return

for item in examples_to_copy:
if item in self.available_examples():
self._copy_pack(item)
elif self._is_example_name(item):
self._copy_example(item)
else:
raise FileNotFoundError(
f"No examples or packs found for input: '{item}'"
)
del self._target_dir
del self._force
return

def _copy_all(self):
"""Copy all packs and examples."""
for pack_name in self.available_examples():
self._copy_pack(pack_name)

def _copy_pack(self, pack_name):
"""Copy all examples in a single pack."""
examples = self.available_examples().get(pack_name, [])
for ex_name, ex_path in examples:
self._copy_tree_to_target(pack_name, ex_name, ex_path)

def _copy_example(self, example_name):
"""Copy a single example by its name."""
example_found = False
for pack_name, examples in self.available_examples().items():
for ex_name, ex_path in examples:
if ex_name == example_name:
self._copy_tree_to_target(pack_name, ex_name, ex_path)
example_found = True
if not example_found:
raise FileNotFoundError(
f"No examples or packs found for input: '{example_name}'"
)

def _is_example_name(self, name):
"""Return True if the given name matches any known example."""
for pack_name, examples in self.available_examples().items():
for example_name, _ in examples:
if example_name == name:
return True
return False

def _copy_tree_to_target(self, pack_name, example_name, src_path):
"""Helper to handle the actual filesystem copy."""
dest_dir = self._target_dir / pack_name / example_name
if dest_dir.exists():
plog.warning(
f"Example directory(ies): '{dest_dir.stem}' already exist. "
" Current versions of existing files have "
"been left unchanged. To overwrite, please rerun "
"and specify --force."
)
return
dest_dir.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(src_path, dest_dir)

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)
existing_dir = cwd / "existing_target"
existing_dir.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