Skip to content

Commit

Permalink
Remove deprecated ignore_files arg in mock_code_factory fixture (#89
Browse files Browse the repository at this point in the history
)
  • Loading branch information
danielhollas authored Dec 6, 2024
1 parent f9dfd1e commit 63a3f44
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 78 deletions.
16 changes: 2 additions & 14 deletions aiida_test_cache/mock_code/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""
Implements the executable for running a mock AiiDA code.
"""
import fnmatch
import os
import shutil
import subprocess
Expand Down Expand Up @@ -68,12 +67,7 @@ def _log(msg: str, error=False) -> None:

# back up results to data directory
os.makedirs(res_dir)
copy_files(
src_dir=Path('.'),
dest_dir=res_dir,
ignore_files=env.ignore_files,
ignore_paths=env.ignore_paths
)
copy_files(src_dir=Path('.'), dest_dir=res_dir, ignore_paths=env.ignore_paths)

else:
# copy outputs from data directory to working directory
Expand All @@ -87,15 +81,11 @@ def _log(msg: str, error=False) -> None:
_log(f"Can not copy '{path.name}'.", error=True)


def copy_files(
src_dir: Path, dest_dir: Path, ignore_files: ty.Iterable[str], ignore_paths: ty.Iterable[str]
) -> None:
def copy_files(src_dir: Path, dest_dir: Path, ignore_paths: ty.Iterable[str]) -> None:
"""Copy files from source to destination directory while ignoring certain files/folders.
:param src_dir: Source directory
:param dest_dir: Destination directory
:param ignore_files: A list of file names (UNIX shell style patterns allowed) which are not copied to the
destination.
:param ignore_paths: A list of paths (UNIX shell style patterns allowed) which are not copied to the destination.
"""
exclude_paths: set = {filepath for path in ignore_paths for filepath in src_dir.glob(path)}
Expand All @@ -115,8 +105,6 @@ def copy_files(
continue

for filename in filenames:
if any(fnmatch.fnmatch(filename, expr) for expr in ignore_files):
continue

if relative_dir / filename in exclude_files:
continue
Expand Down
4 changes: 0 additions & 4 deletions aiida_test_cache/mock_code/_env_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class MockVariables:
test_name: str
data_dir: Path
executable_path: str
ignore_files: ty.Iterable[str]
ignore_paths: ty.Iterable[str]
regenerate_data: bool
fail_on_missing: bool
Expand All @@ -39,7 +38,6 @@ def from_env(cls) -> "MockVariables":
test_name=os.environ[_EnvKeys.TEST_NAME.value],
data_dir=Path(os.environ[_EnvKeys.DATA_DIR.value]),
executable_path=os.environ[_EnvKeys.EXECUTABLE_PATH.value],
ignore_files=os.environ[_EnvKeys.IGNORE_FILES.value].split(":"),
ignore_paths=os.environ[_EnvKeys.IGNORE_PATHS.value].split(":"),
regenerate_data=os.environ[_EnvKeys.REGENERATE_DATA.value] == "True",
fail_on_missing=os.environ[_EnvKeys.FAIL_ON_MISSING.value] == "True",
Expand Down Expand Up @@ -67,7 +65,6 @@ def to_env(self) -> str:
export {_EnvKeys.LABEL.value}="{self.label}"
export {_EnvKeys.DATA_DIR.value}="{self.data_dir}"
export {_EnvKeys.EXECUTABLE_PATH.value}="{self.executable_path}"
export {_EnvKeys.IGNORE_FILES.value}="{':'.join(self.ignore_files)}"
export {_EnvKeys.IGNORE_PATHS.value}="{':'.join(self.ignore_paths)}"
export {_EnvKeys.REGENERATE_DATA.value}={'True' if self.regenerate_data else 'False'}
export {_EnvKeys.FAIL_ON_MISSING.value}={'True' if self.fail_on_missing else 'False'}
Expand All @@ -93,7 +90,6 @@ class _EnvKeys(Enum):
LABEL = "AIIDA_MOCK_LABEL"
DATA_DIR = "AIIDA_MOCK_DATA_DIR"
EXECUTABLE_PATH = "AIIDA_MOCK_EXECUTABLE_PATH"
IGNORE_FILES = "AIIDA_MOCK_IGNORE_FILES"
IGNORE_PATHS = "AIIDA_MOCK_IGNORE_PATHS"
REGENERATE_DATA = "AIIDA_MOCK_REGENERATE_DATA"
FAIL_ON_MISSING = "AIIDA_MOCK_FAIL_ON_MISSING"
Expand Down
21 changes: 2 additions & 19 deletions aiida_test_cache/mock_code/_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import shutil
import typing as ty
import uuid
import warnings

import click
import pytest
Expand Down Expand Up @@ -134,7 +133,6 @@ def _get_mock_code(
label: str,
entry_point: ty.Optional[str] = None,
data_dir_abspath: ty.Union[None, str, pathlib.Path] = None,
ignore_files: ty.Iterable[str] = ('_aiidasubmit.sh', ),
ignore_paths: ty.Iterable[str] = ('_aiidasubmit.sh', ),
executable_name: str = '',
hasher: type[InputHasher] = InputHasher,
Expand All @@ -159,9 +157,6 @@ def _get_mock_code(
data_dir_abspath :
Absolute path of the directory where the code results are
stored.
ignore_files :
A list of file names (UNIX shell style patterns allowed) which are not copied to the results directory
after the code has been executed.
ignore_paths :
A list of paths (UNIX shell style patterns allowed) that are not copied to the results directory
after the code has been executed.
Expand All @@ -174,21 +169,10 @@ def _get_mock_code(
If 'generate', add new key (label) to config dictionary.
_regenerate_test_data :
If True, regenerate test data instead of reusing.
.. deprecated:: 0.1.0
Keyword `ignore_files` is deprecated and will be removed in `v1.0`. Use `ignore_paths` instead.
"""
if ignore_files != ('_aiidasubmit.sh', ):
warnings.warn(
'keyword `ignore_files` is deprecated and will be removed in `v1.0`. Use `ignore_paths` instead.',
DeprecationWarning,
stacklevel=2
)

# It's easy to forget the final comma and pass a string, e.g. `ignore_paths = ('_aiidasubmit.sh')`
for arg in (ignore_paths, ignore_files):
assert isinstance(arg, collections.abc.Iterable) and not isinstance(arg, str), \
f"'ignore_files' and 'ignore_paths' arguments must be tuples or lists, found {type(arg)}"
assert isinstance(ignore_paths, collections.abc.Iterable) and not isinstance(ignore_paths, str), \
f"'ignore_paths' argument must be tuple or list, found {type(ignore_paths)}"

if entry_point is None:
entry_point = label
Expand Down Expand Up @@ -255,7 +239,6 @@ def _get_mock_code(
test_name=request.node.name,
data_dir=data_dir_pl,
executable_path=code_executable_path,
ignore_files=ignore_files,
ignore_paths=ignore_paths,
regenerate_data=_regenerate_test_data,
fail_on_missing=_fail_on_missing,
Expand Down
2 changes: 1 addition & 1 deletion docs/source/user_guide/mock_code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ First, we want to define a fixture for our mocked code in the ``conftest.py``:
data_dir_abspath=DATA_DIR,
entry_point='diff',
# files *not* to copy into the data directory
ignore_files=('_aiidasubmit.sh', 'file*')
ignore_paths=('_aiidasubmit.sh', 'file*')
)
Second, we need to tell the mock executable where to find the *actual* ``diff`` executable by creating a ``.aiida-test-cache-config.yml`` file in the top level of our plugin.
Expand Down
44 changes: 4 additions & 40 deletions tests/mock_code/test_ignore_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,12 @@ def test_ignore_paths(run_directory, tmp_path_factory):
storage_directory = tmp_path_factory.mktemp('storage')

# ignore everything
copy_files(
src_dir=run_directory, dest_dir=storage_directory, ignore_files=(), ignore_paths=('*', )
)
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_paths=('*', ))
assert not (storage_directory / '_aiidasubmit.sh').is_file()

# ignore subfolder
copy_files(
src_dir=run_directory, dest_dir=storage_directory, ignore_files=(), ignore_paths=('my/', )
)
copy_files(
src_dir=run_directory,
dest_dir=storage_directory,
ignore_files=(),
ignore_paths=('my/subfolder', )
)
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_paths=('my/', ))
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_paths=('my/subfolder', ))
assert (storage_directory / '_aiidasubmit.sh').is_file()
assert (storage_directory / 'file2.txt').is_file()
assert not (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()
Expand All @@ -60,37 +51,10 @@ def test_ignore_paths(run_directory, tmp_path_factory):
copy_files(
src_dir=run_directory,
dest_dir=storage_directory,
ignore_files=(),
ignore_paths=('my/subfolder/file3.txt', )
)
assert not (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()

# all should be there
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_files=(), ignore_paths=())
assert (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()


def test_ignore_files(run_directory, tmp_path_factory):
"""Test that ignore_files works as expected."""
storage_directory = tmp_path_factory.mktemp('storage')

# ignore everything
copy_files(
src_dir=run_directory, dest_dir=storage_directory, ignore_files=('*'), ignore_paths=()
)
assert not (storage_directory / '_aiidasubmit.sh').is_file()
assert not (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()

# ignore only specific file (from subfolder)
copy_files(
src_dir=run_directory,
dest_dir=storage_directory,
ignore_files=('file3.txt', ),
ignore_paths=()
)
assert not (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()
assert (storage_directory / 'file2.txt').is_file()

# all should be there
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_files=(), ignore_paths=())
copy_files(src_dir=run_directory, dest_dir=storage_directory, ignore_paths=())
assert (storage_directory / 'my' / 'subfolder' / 'file3.txt').is_file()

0 comments on commit 63a3f44

Please sign in to comment.