From efc60a943b2c2e14707c46ab58f5eca2168a34ca Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Tue, 26 Oct 2010 00:08:51 +0200 Subject: [PATCH] Update find files behaviour and add test for it --- mopidy/utils/path.py | 13 ++++++++----- tests/utils/path_test.py | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/mopidy/utils/path.py b/mopidy/utils/path.py index e73258ea..9220c53d 100644 --- a/mopidy/utils/path.py +++ b/mopidy/utils/path.py @@ -27,8 +27,11 @@ def path_to_uri(*paths): return 'file:' + urllib.pathname2url(path) return 'file://' + urllib.pathname2url(path) -def find_files(folder): - for dirpath, dirnames, filenames in os.walk(folder): - for filename in filenames: - dirpath = os.path.abspath(dirpath) - yield os.path.join(dirpath, filename) +def find_files(path): + if os.path.isfile(path): + yield os.path.abspath(path) + else: + for dirpath, dirnames, filenames in os.walk(path): + for filename in filenames: + dirpath = os.path.abspath(dirpath) + yield os.path.join(dirpath, filename) diff --git a/tests/utils/path_test.py b/tests/utils/path_test.py index ae63d5c0..e0359a16 100644 --- a/tests/utils/path_test.py +++ b/tests/utils/path_test.py @@ -6,9 +6,9 @@ import sys import tempfile import unittest -from mopidy.utils.path import get_or_create_folder, path_to_uri +from mopidy.utils.path import get_or_create_folder, path_to_uri, find_files -from tests import SkipTest +from tests import SkipTest, data_folder class GetOrCreateFolderTest(unittest.TestCase): def setUp(self): @@ -69,3 +69,19 @@ class PathToFileURITest(unittest.TestCase): else: result = path_to_uri(u'/tmp/æøå') self.assertEqual(result, u'file:///tmp/%C3%A6%C3%B8%C3%A5') + + +class FindFilesTest(unittest.TestCase): + def find(self, path): + return list(find_files(data_folder(path))) + + def test_basic_folder(self): + self.assert_(self.find('')) + + def test_nonexistant_folder(self): + self.assertEqual(self.find('does-not-exist'), []) + + def test_file(self): + files = self.find('blank.mp3') + self.assertEqual(len(files), 1) + self.assert_(files[0], data_folder('blank.mp3'))