]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Moved the multiple-filename logic inside the find_file_paths function.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 13 Nov 2009 02:21:15 +0000 (21:21 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 13 Nov 2009 02:21:15 +0000 (21:21 -0500)
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.

bin/find_file_paths
bin/run_tests
src/FileUtils.py
src/Tests/Fixtures/FileUtils/gentoo/postgis.sql [new file with mode: 0644]
src/Tests/Fixtures/FileUtils/ubuntu/lwpostgis.sql [new file with mode: 0644]
src/Tests/Unit/FileUtilsTest.py [new file with mode: 0644]

index cf4d9c51260d30d69785f1cbfe1662459e357f03..47618281d5deba2817494f237cbb3419e4ca6ece 100755 (executable)
@@ -63,25 +63,9 @@ if len(args) < 1:
     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
index 9600bd39c938abdc1b06726f99b8c7d06081d4b7..f6b952881e6165aad3538a4dde646beb75e10040 100755 (executable)
@@ -6,6 +6,7 @@ site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src')
 
 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
@@ -13,6 +14,7 @@ from Tests.Unit import StringUtilsTest
 
 suite = unittest.TestSuite()
 suite.addTest(CensusTest.suite())
+suite.addTest(FileUtilsTest.suite())
 suite.addTest(GeometryTest.suite())
 suite.addTest(KMLTest.suite())
 suite.addTest(SummaryFile1Test.suite())
index cf2df4e897c73f50b7e7bff88ef4e5af0c5d893f..ac50b8a9b7a5a0d9be116664054b65a52d737e97 100644 (file)
@@ -29,22 +29,23 @@ def mkdir_p(path, mode):
     
 
 
-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
diff --git a/src/Tests/Fixtures/FileUtils/gentoo/postgis.sql b/src/Tests/Fixtures/FileUtils/gentoo/postgis.sql
new file mode 100644 (file)
index 0000000..c78d484
--- /dev/null
@@ -0,0 +1 @@
+# Dummy file, used in tests.
diff --git a/src/Tests/Fixtures/FileUtils/ubuntu/lwpostgis.sql b/src/Tests/Fixtures/FileUtils/ubuntu/lwpostgis.sql
new file mode 100644 (file)
index 0000000..c78d484
--- /dev/null
@@ -0,0 +1 @@
+# Dummy file, used in tests.
diff --git a/src/Tests/Unit/FileUtilsTest.py b/src/Tests/Unit/FileUtilsTest.py
new file mode 100644 (file)
index 0000000..e649c4f
--- /dev/null
@@ -0,0 +1,73 @@
+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