agent: Add filter_files() function to dir_utils
This commit is contained in:
parent
bfc86041ab
commit
cf2cdc4ab8
|
@ -1,6 +1,10 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import Callable, List
|
||||||
|
|
||||||
|
|
||||||
def get_all_files_in_directory(dir_path: Path) -> List[Path]:
|
def get_all_files_in_directory(dir_path: Path) -> List[Path]:
|
||||||
return [f for f in dir_path.iterdir() if f.is_file()]
|
return [f for f in dir_path.iterdir() if f.is_file()]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_files(files: List[Path], file_filter: Callable[[Path], bool]):
|
||||||
|
return [f for f in files if file_filter(f)]
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
from infection_monkey.utils.dir_utils import get_all_files_in_directory
|
from infection_monkey.utils.dir_utils import (
|
||||||
|
filter_files,
|
||||||
|
get_all_files_in_directory,
|
||||||
|
)
|
||||||
|
|
||||||
FILE_1 = "file.jpg.zip"
|
FILE_1 = "file.jpg.zip"
|
||||||
FILE_2 = "file.xyz"
|
FILE_2 = "file.xyz"
|
||||||
|
@ -51,3 +54,22 @@ def test_get_all_files_in_directory__subdir_has_files(tmp_path, monkeypatch):
|
||||||
|
|
||||||
expected_return_value = sorted(files)
|
expected_return_value = sorted(files)
|
||||||
assert sorted(get_all_files_in_directory(tmp_path)) == expected_return_value
|
assert sorted(get_all_files_in_directory(tmp_path)) == expected_return_value
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_files__no_results(tmp_path):
|
||||||
|
add_files_to_dir(tmp_path)
|
||||||
|
|
||||||
|
files_in_dir = get_all_files_in_directory(tmp_path)
|
||||||
|
filtered_files = filter_files(files_in_dir, lambda _: False)
|
||||||
|
|
||||||
|
assert len(filtered_files) == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_filter_files__all_true(tmp_path):
|
||||||
|
files = add_files_to_dir(tmp_path)
|
||||||
|
expected_return_value = sorted(files)
|
||||||
|
|
||||||
|
files_in_dir = get_all_files_in_directory(tmp_path)
|
||||||
|
filtered_files = filter_files(files_in_dir, lambda _: True)
|
||||||
|
|
||||||
|
assert sorted(filtered_files) == expected_return_value
|
||||||
|
|
Loading…
Reference in New Issue