From 68f0f2a8ad4c16e8e49b1a13191559949e9f6214 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 12 Nov 2009 21:21:15 -0500 Subject: [PATCH] Moved the multiple-filename logic inside the find_file_paths function. 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 | 24 +----- bin/run_tests | 2 + src/FileUtils.py | 23 +++--- .../Fixtures/FileUtils/gentoo/postgis.sql | 1 + .../Fixtures/FileUtils/ubuntu/lwpostgis.sql | 1 + src/Tests/Unit/FileUtilsTest.py | 73 +++++++++++++++++++ 6 files changed, 93 insertions(+), 31 deletions(-) create mode 100644 src/Tests/Fixtures/FileUtils/gentoo/postgis.sql create mode 100644 src/Tests/Fixtures/FileUtils/ubuntu/lwpostgis.sql create mode 100644 src/Tests/Unit/FileUtilsTest.py diff --git a/bin/find_file_paths b/bin/find_file_paths index cf4d9c5..4761828 100755 --- a/bin/find_file_paths +++ b/bin/find_file_paths @@ -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 diff --git a/bin/run_tests b/bin/run_tests index 9600bd3..f6b9528 100755 --- a/bin/run_tests +++ b/bin/run_tests @@ -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()) diff --git a/src/FileUtils.py b/src/FileUtils.py index cf2df4e..ac50b8a 100644 --- a/src/FileUtils.py +++ b/src/FileUtils.py @@ -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 index 0000000..c78d484 --- /dev/null +++ b/src/Tests/Fixtures/FileUtils/gentoo/postgis.sql @@ -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 index 0000000..c78d484 --- /dev/null +++ b/src/Tests/Fixtures/FileUtils/ubuntu/lwpostgis.sql @@ -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 index 0000000..e649c4f --- /dev/null +++ b/src/Tests/Unit/FileUtilsTest.py @@ -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 -- 2.44.2