Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Unified Diff: chromite/lib/cros_build_lib_unittest.py

Issue 3325017: Add ListFiles a function to recursively list files in a directory. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils
Patch Set: Simplify while statement Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromite/lib/cros_build_lib.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromite/lib/cros_build_lib_unittest.py
diff --git a/chromite/lib/cros_build_lib_unittest.py b/chromite/lib/cros_build_lib_unittest.py
new file mode 100755
index 0000000000000000000000000000000000000000..ef6ee9e27ad0e1576df714a68bd05b533458c739
--- /dev/null
+++ b/chromite/lib/cros_build_lib_unittest.py
@@ -0,0 +1,69 @@
+#!/usr/bin/python
+
+import errno
+import os
+import unittest
+import shutil
+import tempfile
+
+import cros_build_lib
+
+class TestListFiles(unittest.TestCase):
+
+ def setUp(self):
+ self.root_dir = tempfile.mkdtemp(prefix='listfiles_unittest')
+
+ def tearDown(self):
+ shutil.rmtree(self.root_dir)
+
+ def _createNestedDir(self, dir_structure):
+ for entry in dir_structure:
+ full_path = os.path.join(os.path.join(self.root_dir, entry))
+ # ensure dirs are created
+ try:
+ os.makedirs(os.path.dirname(full_path))
+ if full_path.endswith('/'):
+ # we only want to create directories
+ return
+ except OSError, err:
+ if err.errno == errno.EEXIST:
+ # we don't care if the dir already exists
+ pass
+ else:
+ raise
+ # create dummy files
+ tmp = open(full_path, 'w')
+ tmp.close()
+
+ def testTraverse(self):
+ """
+ Test that we are traversing the directory properly
+ """
+ dir_structure = ['one/two/test.txt', 'one/blah.py',
+ 'three/extra.conf']
+ self._createNestedDir(dir_structure)
+
+ files = cros_build_lib.ListFiles(self.root_dir)
+ for file in files:
+ file = file.replace(self.root_dir, '').lstrip('/')
+ if file not in dir_structure:
+ self.fail('%s was not found in %s' % (file, dir_structure))
+
+ def testEmptyFilePath(self):
+ """
+ Test that we return nothing when directories are empty
+ """
+ dir_structure = ['one/', 'two/', 'one/a/']
+ self._createNestedDir(dir_structure)
+ files = cros_build_lib.ListFiles(self.root_dir)
+ self.assertEqual(files, [])
+
+ def testNoSuchDir(self):
+ try:
+ cros_build_lib.ListFiles('/me/no/existe')
+ except OSError, err:
+ self.assertEqual(err.errno, errno.ENOENT)
+
+
+if __name__ == '__main__':
+ unittest.main()
« no previous file with comments | « chromite/lib/cros_build_lib.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698