Skip to content

Commit

Permalink
IO-575: Issue with directory imports (#510)
Browse files Browse the repository at this point in the history
* Fix for bug, and tests to ensure same doesn't happen again

Co-authored-by: Owen Jones <[email protected]>
  • Loading branch information
owencjones and Owen Jones authored Dec 21, 2022
1 parent 3f6c172 commit 5195742
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
10 changes: 8 additions & 2 deletions darwin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,14 @@ def find_files(

for f in files:
path = Path(f)
if path.is_dir():
found_files.extend([f for f in path.glob(pattern) if is_extension_allowed_by_filename(str(path))])
if path.is_dir() == True:
found_files.extend(
[
path_object
for path_object in path.glob(pattern)
if is_extension_allowed_by_filename(str(path_object))
]
)
elif is_extension_allowed_by_filename(str(path)):
found_files.append(path)
else:
Expand Down
47 changes: 35 additions & 12 deletions tests/darwin/utils/find_files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,48 @@ def test_raises_error_unsupported_filetype(self, mock_is_extension_allowed):
with self.assertRaises(UnsupportedFileType):
find_files(["1"], files_to_exclude=[], recursive=False)

@patch("darwin.utils.Path", autospec=True)
@patch("darwin.utils.is_extension_allowed_by_filename")
def test_uses_correct_glob_if_recursive(self, mock_is_extension_allowed, mock_path):
mock_path.is_dir.return_value = True
mock_path.glob.return_value = ["1"]
def test_uses_correct_glob_if_recursive(self, mock_is_extension_allowed):
with patch("darwin.utils.Path.is_dir") as mock_is_dir:
with patch("darwin.utils.Path.glob") as mock_glob:
mock_is_dir.return_value = True
mock_glob.return_value = ["1"]

find_files(["1"], files_to_exclude=[], recursive=True)
find_files(["123"], files_to_exclude=[], recursive=True)

mock_path.return_value.glob.assert_called_once_with("**/*")
self.assertTrue(mock_glob.called)
self.assertEqual(mock_glob.call_args[0][0], "**/*")

@patch("darwin.utils.Path", autospec=True)
@patch("darwin.utils.is_extension_allowed_by_filename")
def test_uses_correct_glob_if_not_recursive(self, mock_is_extension_allowed, mock_path):
mock_path.is_dir.return_value = True
mock_path.glob.return_value = ["1"]
def test_glob_results_in_correct_call_to_is_extension_allowed_by_filename(self, mock_is_extension_allowed):
mock_is_extension_allowed.return_value = True
with patch("darwin.utils.Path.is_dir") as mock_is_dir:
with patch("darwin.utils.Path.glob") as mock_glob:
mock_is_dir.return_value = True
mock_glob.return_value = [Path("1.png"), Path("1/b/c/2.png"), Path("1/b/c/3.png")]

result = find_files(["1"], files_to_exclude=[], recursive=True)

self.assertEqual(
result,
[
Path("1.png"),
Path("1/b/c/2.png"),
Path("1/b/c/3.png"),
],
)

find_files(["1"], files_to_exclude=[], recursive=False)
@patch("darwin.utils.is_extension_allowed_by_filename")
def test_uses_correct_glob_if_not_recursive(self, mock_is_extension_allowed):
with patch("darwin.utils.Path.is_dir") as mock_is_dir:
with patch("darwin.utils.Path.glob") as mock_glob:
mock_is_dir.return_value = True
mock_glob.return_value = ["1"]

find_files(["1"], files_to_exclude=[], recursive=False)

mock_path.return_value.glob.assert_called_once_with("*")
self.assertTrue(mock_glob.called)
self.assertEqual(mock_glob.call_args[0][0], "*")


class TestIsExtensionAllowedByFilenameFunctions(FindFileTestCase):
Expand Down

0 comments on commit 5195742

Please sign in to comment.