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:
Mike Salvatore 2021-06-22 12:47:51 -04:00
parent e2dfd6a5e3
commit bfc86041ab
2 changed files with 19 additions and 24 deletions

View File

@ -2,7 +2,5 @@ from pathlib import Path
from typing import List from typing import List
def get_all_files_in_directory(dir_path: str) -> List: def get_all_files_in_directory(dir_path: Path) -> List[Path]:
path = Path(dir_path) return [f for f in dir_path.iterdir() if f.is_file()]
return [str(f) for f in path.iterdir() if f.is_file()]

View File

@ -1,6 +1,3 @@
import os
from pathlib import Path
from infection_monkey.utils.dir_utils import get_all_files_in_directory from infection_monkey.utils.dir_utils import get_all_files_in_directory
FILE_1 = "file.jpg.zip" FILE_1 = "file.jpg.zip"
@ -10,47 +7,47 @@ SUBDIR_2 = "subdir2"
def add_subdirs_to_dir(parent_dir): def add_subdirs_to_dir(parent_dir):
subdir1 = os.path.join(parent_dir, SUBDIR_1) subdir1 = parent_dir / SUBDIR_1
subdir2 = os.path.join(parent_dir, SUBDIR_2) subdir2 = parent_dir / SUBDIR_2
subdirs = [subdir1, subdir2] subdirs = [subdir1, subdir2]
for subdir in subdirs: for subdir in subdirs:
os.mkdir(subdir) subdir.mkdir()
return subdirs return subdirs
def add_files_to_dir(parent_dir): def add_files_to_dir(parent_dir):
file1 = os.path.join(parent_dir, FILE_1) file1 = parent_dir / FILE_1
file2 = os.path.join(parent_dir, FILE_2) file2 = parent_dir / FILE_2
files = [file1, file2] files = [file1, file2]
for f in files: for f in files:
Path(f).touch() f.touch()
return files return files
def test_get_all_files_in_directory__no_files(tmpdir, monkeypatch): def test_get_all_files_in_directory__no_files(tmp_path, monkeypatch):
add_subdirs_to_dir(tmpdir) add_subdirs_to_dir(tmp_path)
expected_return_value = [] 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): def test_get_all_files_in_directory__has_files(tmp_path, monkeypatch):
add_subdirs_to_dir(tmpdir) add_subdirs_to_dir(tmp_path)
files = add_files_to_dir(tmpdir) files = add_files_to_dir(tmp_path)
expected_return_value = sorted(files) 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): def test_get_all_files_in_directory__subdir_has_files(tmp_path, monkeypatch):
subdirs = add_subdirs_to_dir(tmpdir) subdirs = add_subdirs_to_dir(tmp_path)
add_files_to_dir(subdirs[0]) add_files_to_dir(subdirs[0])
files = add_files_to_dir(tmpdir) files = add_files_to_dir(tmp_path)
expected_return_value = sorted(files) 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