From 8c10b61e90abbcfdff472bad4bb760558c7b850c Mon Sep 17 00:00:00 2001 From: Pablo 'merKur' Kohan Date: Thu, 23 Jul 2020 15:16:40 +0300 Subject: [PATCH] Fix findfiles not to fail on missing/invalid dir Was failing on --dry-run and tests. Added unit-test. --- osxphotos/utils.py | 5 ++++- tests/test_utils.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/osxphotos/utils.py b/osxphotos/utils.py index 42534c13..28f7bb83 100644 --- a/osxphotos/utils.py +++ b/osxphotos/utils.py @@ -262,7 +262,10 @@ def get_preferred_uti_extension(uti): def findfiles(pattern, path_): """Returns list of filenames from path_ matched by pattern - shell pattern. Matching is case-insensitive.""" + shell pattern. Matching is case-insensitive. + If 'path_' is invalid/doesn't exist, returns [].""" + if not os.path.isdir(path_): + return [] # See: https://gist.github.com/techtonik/5694830 rule = re.compile(fnmatch.translate(pattern), re.IGNORECASE) diff --git a/tests/test_utils.py b/tests/test_utils.py index 2986e22c..a00fb2d8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -76,3 +76,13 @@ def test_findfiles(): assert len(files) == 2 assert "file1.jpg" in files assert "file2.JPG" in files + + +def test_findfiles_invalid_dir(): + import tempfile + import os.path + from osxphotos.utils import findfiles + + temp_dir = tempfile.TemporaryDirectory(prefix="osxphotos_") + files = findfiles("*.jpg", f"{temp_dir.name}/no_such_dir" ) + assert len(files) == 0