Skip to content

Commit

Permalink
fix file_tools.files_in_directory(recursive=True), update tests to de…
Browse files Browse the repository at this point in the history
…al with recursion and files starting with dots "."
  • Loading branch information
bbean23 committed Mar 12, 2024
1 parent b460002 commit 0926b5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
9 changes: 8 additions & 1 deletion opencsp/common/lib/tool/file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,14 @@ def files_in_directory(input_dir, sort=True, files_only=False, recursive=False):
# Walk the directory and all subdirectories and assemble a list of files.
norm_input_dir = norm_path(input_dir)
for root, dirnames, file_names_exts in os.walk(norm_input_dir):
relative_path: str = root.replace(norm_input_dir, "").lstrip("./\\")
# remove the front of the path, in order to get the path relative to the input_dir
relative_path: str = root.replace(norm_input_dir, "")
# don't include any leading ./
if relative_path.startswith("./") or relative_path.startswith(".\\"):
relative_path = relative_path[2:]
# don't include any leading /
relative_path = relative_path.lstrip("\\/")

scanned_files += [
os.path.join(relative_path, file_name_ext)
for file_name_ext in file_names_exts
Expand Down
Empty file.
Empty file.
39 changes: 17 additions & 22 deletions opencsp/common/lib/tool/test/test_file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,33 @@

class TestFileTools(unittest.TestCase):
def setUp(self) -> None:
self.data_dir = os.path.join(
orp.opencsp_code_dir(),
'common',
'lib',
'tool',
'test',
'data',
'input',
'file_tools',
)
self.out_dir = os.path.join(
orp.opencsp_code_dir(),
'common',
'lib',
'tool',
'test',
'data',
'output',
'file_tools',
)
path, _, _ = ft.path_components(__file__)
self.data_dir = os.path.join(path, "data", "input", "file_tools")
self.out_dir = os.path.join(path, "data", "output", "file_tools")

def tearDown(self) -> None:
pass

def test_files_in_directory(self):
files_name_ext = ft.files_in_directory(self.data_dir)
expected = ["a.a", "b.b", "d"]
expected = [".dotfile", "a.a", "b.b", "d"]
self.assertListEqual(expected, files_name_ext)

def test_files_in_directory_files_only(self):
files_name_ext = ft.files_in_directory(self.data_dir, files_only=True)
expected = ["a.a", "b.b"]
expected = [".dotfile", "a.a", "b.b"]
self.assertListEqual(expected, files_name_ext)

def test_files_in_directory_recursive(self):
files_name_ext = ft.files_in_directory(self.data_dir, recursive=True)
files_name_ext = [f.replace("\\", "/") for f in files_name_ext]
expected = [".dotfile", "a.a", "b.b", "d", "d/c.c", "d/e", "d/e/f.f"]
self.assertListEqual(expected, files_name_ext)

def test_files_in_directory_recursive_files_only(self):
files_name_ext = ft.files_in_directory(self.data_dir, recursive=True, files_only=True)
files_name_ext = [f.replace("\\", "/") for f in files_name_ext]
expected = [".dotfile", "a.a", "b.b", "d/c.c", "d/e/f.f"]
self.assertListEqual(expected, files_name_ext)

def test_files_in_directory_by_extension(self):
Expand Down

0 comments on commit 0926b5c

Please sign in to comment.