Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
17e3747
rm api from cli.py
cadenmyers13 Oct 20, 2025
82f7394
add copy_examples api to packsmanager
cadenmyers13 Oct 20, 2025
d973c83
add tests for copy_examples
cadenmyers13 Oct 20, 2025
375606c
add additional dirs to temp structure
cadenmyers13 Oct 20, 2025
711c24e
rm old tests
cadenmyers13 Oct 20, 2025
987c3e3
add news
cadenmyers13 Oct 20, 2025
9e65a27
glob only files and not dirs
cadenmyers13 Oct 20, 2025
5354831
fix target dir location to be under cwd and change name for new test …
cadenmyers13 Oct 22, 2025
47d87fb
Add additional test case, fix how copy location is tested
cadenmyers13 Oct 22, 2025
0a490b9
copy_examples function
cadenmyers13 Oct 22, 2025
6e2695d
rm error message function
cadenmyers13 Oct 23, 2025
e96988f
add test for forced overwrite of copied files
cadenmyers13 Oct 23, 2025
abfba68
Add 'force' param to copy_examples
cadenmyers13 Oct 23, 2025
d4cddf5
fix copy_tree to handle warnings and errors properly
cadenmyers13 Oct 27, 2025
38881b8
Merge branch 'main' of github.com:diffpy/diffpy.cmi into copy-func-test
cadenmyers13 Oct 28, 2025
f4fe605
pin workflow to python 3.13
cadenmyers13 Oct 30, 2025
07cbcd7
add test for force and non-force to make sure files are copied correctly
cadenmyers13 Oct 31, 2025
078b13c
update copy_tree_to_target to handle different copying cases
cadenmyers13 Oct 31, 2025
e7444d7
capture warning output in print statement
cadenmyers13 Oct 31, 2025
c792c0b
add rich to conda.txt
cadenmyers13 Oct 31, 2025
7d7a2a3
add UC comments
cadenmyers13 Oct 31, 2025
52a6752
use ANSI color code for now
cadenmyers13 Oct 31, 2025
611bdf5
rm rich from reqs
cadenmyers13 Oct 31, 2025
d6f34e0
Rm colors from print
cadenmyers13 Oct 31, 2025
087851b
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Oct 31, 2025
87c1f43
rm color from test
cadenmyers13 Nov 1, 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
Prev Previous commit
Next Next commit
add tests for copy_examples
  • Loading branch information
cadenmyers13 committed Oct 20, 2025
commit d973c830cdcdd6b954c66497e42f63d66b38483a
180 changes: 180 additions & 0 deletions tests/test_packsmanager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import os
import re
from pathlib import Path

import pytest

from diffpy.cmi.packsmanager import PacksManager
Expand Down Expand Up @@ -63,9 +67,12 @@ def paths_and_names_match(expected, actual, root):
{
"packA": [
("ex1", "case5/docs/examples/packA/ex1"),
("ex2", "case5/docs/examples/packA/ex2"),
],
"packB": [
("ex1", "case5/docs/examples/packB/ex1"),
("ex3", "case5/docs/examples/packB/ex3"),
("ex4", "case5/docs/examples/packB/ex4"),
],
},
),
Expand All @@ -92,3 +99,176 @@ def test_tmp_file_structure(input, expected, example_cases):
assert path.is_file()
else:
assert path.is_dir()


copy_params = [
# Test various use cases to copy_examples on case5
# 1) copy one example (ambiguous)
# 2) copy list of examples from same pack (ambiguous)
# 3) copy list of examples from different pack (ambiguous)
# 4) copy one example (unambiguous)
# 5) copy list of examples from same pack (unambiguous)
# 6) copy list of examples from different pack (unambiguous)
# 7) copy all examples from a pack
# 8) copy all examples from list of packs
# 9) copy all examples from all packs
( # 1) copy one example, (ambiguous)
["ex1"],
[
Path("packA/ex1/path1/script1.py"),
Path("packB/ex1/path2/script2.py"),
],
),
( # 2) copy list of examples from same pack (ambiguous)
["ex1", "ex2"],
[
Path("packA/ex1/path1/script1.py"),
Path("packB/ex1/path2/script2.py"),
Path("packA/ex2/script3.py"),
],
),
( # 3) copy list of examples from different packs (ambiguous)
["ex1", "ex1"],
[
Path("packA/ex1/path1/script1.py"),
Path("packB/ex1/path2/script2.py"),
],
),
( # 4) copy one example (unambiguous)
["ex2"],
[
Path("packA/ex2/script3.py"),
],
),
( # 5) copy list of examples from same pack (unambiguous)
["ex3", "ex4"],
[
Path("packB/ex3/script4.py"),
Path("packB/ex4/script5.py"),
],
),
( # 6) copy list of examples from different packs (unambiguous)
["ex2", "ex3"],
[
Path("packA/ex2/script3.py"),
Path("packB/ex3/script4.py"),
],
),
( # 7) copy all examples from a pack
["packA"],
[
Path("packA/ex1/path1/script1.py"),
Path("packA/ex2/script3.py"),
],
),
( # 8) copy all examples from list of packs
["packA", "packB"],
[
Path("packA/ex1/path1/script1.py"),
Path("packA/ex2/script3.py"),
Path("packB/ex1/path2/script2.py"),
Path("packB/ex3/script4.py"),
Path("packB/ex4/script5.py"),
],
),
( # 9) copy all examples from all packs
["all"],
[
Path("packA/ex1/path1/script1.py"),
Path("packA/ex2/script3.py"),
Path("packB/ex1/path2/script2.py"),
Path("packB/ex3/script4.py"),
Path("packB/ex4/script5.py"),
],
),
]


# input: list of str - cli input(s) to copy_examples
# expected_paths: list of Path - expected relative paths to copied examples
@pytest.mark.parametrize("input,expected_paths", copy_params)
def test_copy_examples(input, expected_paths, example_cases):
examples_dir = example_cases / "case5"
pm = PacksManager(root_path=examples_dir)
target_dir = example_cases / "user_target"
pm.copy_examples(input, target_dir=target_dir)
actual = sorted(list(target_dir.rglob("*")))
expected = sorted([target_dir / path for path in expected_paths])
assert actual == expected
for path in expected_paths:
copied_path = target_dir / path
original_path = examples_dir / path
if copied_path.is_file() and original_path.is_file():
assert copied_path.read_text() == original_path.read_text()


# Test default and targeted copy_example location on case5
# input: str or None - path arg to copy_examples
# expected: Path - expected relative path to copied example
@pytest.mark.parametrize(
"input,expected_path",
[
(None, Path("cwd/packA/ex1/path1/script1.py")),
("user_target", Path("user_target/packA/ex1/path1/script1.py")),
],
)
def test_copy_examples_location(input, expected_path, example_cases):
examples_dir = example_cases / "case5"
os.chdir(example_cases / "cwd")
pm = PacksManager(root_path=examples_dir)
paths = pm.copy_examples(["packA"], target_dir=input)
actual = paths[0]
expected = example_cases / expected_path
assert actual == expected


# Test bad inputs to copy_examples on case3
# These include:
# 1) Input not found (example or pack)
# 2) Mixed good and bad inputs
# 3) Path to directory already exists
# 4) No input provided
@pytest.mark.parametrize(
"bad_inputs,expected,path",
[
(
# 1) Input not found (example or pack).
# Expected: Raise an error with the message.
["bad_example"],
"No examples or packs found for input: 'bad_example'",
None,
),
(
# 2) Mixed good example and bad input.
# Expected: Raise an error with the message.
["ex1", "bad_example"],
"No examples or packs found for input: 'bad_example'",
None,
),
(
# 3) Mixed good pack and bad input.
# Expected: Raise an error with the message.
["packA", "bad_example"],
"No examples or packs found for input: 'bad_example'",
None,
),
(
# 4) Path to directory already exists.
# Expected: Raise a warning with the message.
["ex1"],
(
"Example directory(ies): 'ex1' already exist. "
" Current versions of existing files have "
"been left unchanged. To overwrite, please rerun "
"and specify --force."
),
Path("docs/examples/"),
),
],
)
def test_copy_examples_bad(bad_inputs, expected, path, example_cases):
examples_dir = example_cases / "case3"
pm = PacksManager(root_path=examples_dir)
target_dir = None if path is None else examples_dir / path
with pytest.raises(Exception, match=re.escape(expected)):
pm.copy_examples(bad_inputs, target_dir=target_dir)