forked from p15670423/monkey
agent: Switch get_all_files_in_directory from str to Path
Path and PurePath objects provide a lot of syntactic sugar to file handling that makes the code clearer and more concise.
This commit is contained in:
parent
e2dfd6a5e3
commit
bfc86041ab
|
@ -2,7 +2,5 @@ from pathlib import Path
|
|||
from typing import List
|
||||
|
||||
|
||||
def get_all_files_in_directory(dir_path: str) -> List:
|
||||
path = Path(dir_path)
|
||||
|
||||
return [str(f) for f in path.iterdir() if f.is_file()]
|
||||
def get_all_files_in_directory(dir_path: Path) -> List[Path]:
|
||||
return [f for f in dir_path.iterdir() if f.is_file()]
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from infection_monkey.utils.dir_utils import get_all_files_in_directory
|
||||
|
||||
FILE_1 = "file.jpg.zip"
|
||||
|
@ -10,47 +7,47 @@ SUBDIR_2 = "subdir2"
|
|||
|
||||
|
||||
def add_subdirs_to_dir(parent_dir):
|
||||
subdir1 = os.path.join(parent_dir, SUBDIR_1)
|
||||
subdir2 = os.path.join(parent_dir, SUBDIR_2)
|
||||
subdir1 = parent_dir / SUBDIR_1
|
||||
subdir2 = parent_dir / SUBDIR_2
|
||||
subdirs = [subdir1, subdir2]
|
||||
|
||||
for subdir in subdirs:
|
||||
os.mkdir(subdir)
|
||||
subdir.mkdir()
|
||||
|
||||
return subdirs
|
||||
|
||||
|
||||
def add_files_to_dir(parent_dir):
|
||||
file1 = os.path.join(parent_dir, FILE_1)
|
||||
file2 = os.path.join(parent_dir, FILE_2)
|
||||
file1 = parent_dir / FILE_1
|
||||
file2 = parent_dir / FILE_2
|
||||
files = [file1, file2]
|
||||
|
||||
for f in files:
|
||||
Path(f).touch()
|
||||
f.touch()
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def test_get_all_files_in_directory__no_files(tmpdir, monkeypatch):
|
||||
add_subdirs_to_dir(tmpdir)
|
||||
def test_get_all_files_in_directory__no_files(tmp_path, monkeypatch):
|
||||
add_subdirs_to_dir(tmp_path)
|
||||
|
||||
expected_return_value = []
|
||||
assert get_all_files_in_directory(tmpdir) == expected_return_value
|
||||
assert get_all_files_in_directory(tmp_path) == expected_return_value
|
||||
|
||||
|
||||
def test_get_all_files_in_directory__has_files(tmpdir, monkeypatch):
|
||||
add_subdirs_to_dir(tmpdir)
|
||||
files = add_files_to_dir(tmpdir)
|
||||
def test_get_all_files_in_directory__has_files(tmp_path, monkeypatch):
|
||||
add_subdirs_to_dir(tmp_path)
|
||||
files = add_files_to_dir(tmp_path)
|
||||
|
||||
expected_return_value = sorted(files)
|
||||
assert sorted(get_all_files_in_directory(tmpdir)) == expected_return_value
|
||||
assert sorted(get_all_files_in_directory(tmp_path)) == expected_return_value
|
||||
|
||||
|
||||
def test_get_all_files_in_directory__subdir_has_files(tmpdir, monkeypatch):
|
||||
subdirs = add_subdirs_to_dir(tmpdir)
|
||||
def test_get_all_files_in_directory__subdir_has_files(tmp_path, monkeypatch):
|
||||
subdirs = add_subdirs_to_dir(tmp_path)
|
||||
add_files_to_dir(subdirs[0])
|
||||
|
||||
files = add_files_to_dir(tmpdir)
|
||||
files = add_files_to_dir(tmp_path)
|
||||
|
||||
expected_return_value = sorted(files)
|
||||
assert sorted(get_all_files_in_directory(tmpdir)) == expected_return_value
|
||||
assert sorted(get_all_files_in_directory(tmp_path)) == expected_return_value
|
||||
|
|
Loading…
Reference in New Issue