Added tests for the FileUtils module, and in particular for the find_file_paths function.
Added two fixtures for the new tests.
Updated the test suite.
raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS)
-found_paths = []
-
-# Build a list of found paths, in case there are / we want more than
-# one.
-for filename in args:
- # If --single was passed, we relay it to the find_file_paths
- # function so that it completes sooner (it doesn't have to keep
- # looking after the first match).
- found_paths += FileUtils.find_file_paths(options.root,
- filename,
- options.single)
-
- if options.single and (len(found_paths) > 0):
- # If we found anything and we only want the first match, just
- # print it and quit.
- print found_paths[0]
- raise SystemExit(ExitCodes.EXIT_SUCCESS)
-
-
-# If we weren't passed --single, spit out all matches.
+found_paths = FileUtils.find_file_paths(options.root,
+ args,
+ options.single)
+
for fp in found_paths:
print fp
import unittest
from Tests.Unit import CensusTest
+from Tests.Unit import FileUtilsTest
from Tests.Unit import GeometryTest
from Tests.Unit import KMLTest
from Tests.Unit import SummaryFile1Test
suite = unittest.TestSuite()
suite.addTest(CensusTest.suite())
+suite.addTest(FileUtilsTest.suite())
suite.addTest(GeometryTest.suite())
suite.addTest(KMLTest.suite())
suite.addTest(SummaryFile1Test.suite())
-def find_file_paths(root, target_filename, return_first = False):
+def find_file_paths(root, target_filenames=[], return_first = False):
"""
- Search beneath root for files named target_filename. If
- return_first is True, then return as soon as a match is
- found. Otherwise, return once all matching paths have been
- found. Either way, the result is a list containing all matched
- paths (even if we only matched one).
+ Search beneath root for files whose names are contained in the
+ target_filenames list. If return_first is True, then return as
+ soon as a match is found. Otherwise, return once all matching
+ paths have been found. Either way, the result is a list containing
+ all matched paths (even if we only matched one).
"""
found_files = []
for folder, subfolders, files in os.walk(root):
for f in files:
- if (f == target_filename):
- if return_first:
- return [os.path.join(folder, f)]
- else:
- found_files.append(os.path.join(folder, f))
+ for t in target_filenames:
+ if (f == t):
+ if return_first:
+ return [os.path.join(folder, f)]
+ else:
+ found_files.append(os.path.join(folder, f))
return found_files
--- /dev/null
+# Dummy file, used in tests.
--- /dev/null
+# Dummy file, used in tests.
--- /dev/null
+import unittest
+
+import Tests.Fixtures
+import FileUtils
+
+
+class FindFilePathsTest(unittest.TestCase):
+
+ def testPostgisFixturesFound(self):
+ """
+ There are two dummy files in the fixtures directory,
+ lwpostgis.sql and postgis.sql. They have been placed in two
+ subfolders named after two distros who include the SQL file with
+ different names.
+
+ If we search a distro subdirectory for *both* names, we should
+ get back only one result: the correct one to use on that
+ distro.
+ """
+ gentoo_fixtures_path = Tests.Fixtures.Path() + '/FileUtils/gentoo'
+ ubuntu_fixtures_path = Tests.Fixtures.Path() + '/FileUtils/ubuntu'
+
+ target_files = ['lwpostgis.sql', 'postgis.sql']
+
+ res = FileUtils.find_file_paths(gentoo_fixtures_path, target_files)
+ self.assertTrue(len(res) == 1)
+ self.assertTrue('postgis.sql' in res[0])
+
+ res = FileUtils.find_file_paths(ubuntu_fixtures_path, target_files)
+ print res[0]
+ self.assertTrue(len(res) == 1)
+ self.assertTrue('lwpostgis.sql' in res[0])
+
+
+ def testBothFixturesFoundFromRoot(self):
+ """
+ If we search for the (lw)postgis.sql files from the fixtures
+ root, we should find both of them.
+ """
+ fixtures_path = Tests.Fixtures.Path() + '/FileUtils'
+
+ target_files = ['lwpostgis.sql', 'postgis.sql']
+
+ res = FileUtils.find_file_paths(fixtures_path, target_files)
+ self.assertTrue(len(res) == 2)
+ # "gentoo" comes first alphabetically, and so we'll traverse
+ # it first. Thus, postgis.sql should be found before
+ # lwpostgis.sql.
+ self.assertTrue('postgis.sql' in res[0])
+ self.assertTrue('lwpostgis.sql' in res[1])
+
+
+ def testOnlyOneResultReturnedWhenReturnFirstTrue(self):
+ """
+ Make sure that, in a case where we would normally get two
+ results back, the usage of return_first causes us to receive
+ only one.
+ """
+ fixtures_path = Tests.Fixtures.Path() + '/FileUtils'
+
+ target_files = ['lwpostgis.sql', 'postgis.sql']
+
+ res = FileUtils.find_file_paths(fixtures_path, target_files, True)
+ self.assertTrue(len(res) == 1)
+ # Again, "gentoo" comes before "ubuntu" alphabetically, so
+ # this is the expected result.
+ self.assertTrue('postgis.sql' in res[0])
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(FindFilePathsTest))
+ return suite