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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « chromite/lib/cros_build_lib.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2
3 import errno
4 import os
5 import unittest
6 import shutil
7 import tempfile
8
9 import cros_build_lib
10
11 class TestListFiles(unittest.TestCase):
12
13 def setUp(self):
14 self.root_dir = tempfile.mkdtemp(prefix='listfiles_unittest')
15
16 def tearDown(self):
17 shutil.rmtree(self.root_dir)
18
19 def _createNestedDir(self, dir_structure):
20 for entry in dir_structure:
21 full_path = os.path.join(os.path.join(self.root_dir, entry))
22 # ensure dirs are created
23 try:
24 os.makedirs(os.path.dirname(full_path))
25 if full_path.endswith('/'):
26 # we only want to create directories
27 return
28 except OSError, err:
29 if err.errno == errno.EEXIST:
30 # we don't care if the dir already exists
31 pass
32 else:
33 raise
34 # create dummy files
35 tmp = open(full_path, 'w')
36 tmp.close()
37
38 def testTraverse(self):
39 """
40 Test that we are traversing the directory properly
41 """
42 dir_structure = ['one/two/test.txt', 'one/blah.py',
43 'three/extra.conf']
44 self._createNestedDir(dir_structure)
45
46 files = cros_build_lib.ListFiles(self.root_dir)
47 for file in files:
48 file = file.replace(self.root_dir, '').lstrip('/')
49 if file not in dir_structure:
50 self.fail('%s was not found in %s' % (file, dir_structure))
51
52 def testEmptyFilePath(self):
53 """
54 Test that we return nothing when directories are empty
55 """
56 dir_structure = ['one/', 'two/', 'one/a/']
57 self._createNestedDir(dir_structure)
58 files = cros_build_lib.ListFiles(self.root_dir)
59 self.assertEqual(files, [])
60
61 def testNoSuchDir(self):
62 try:
63 cros_build_lib.ListFiles('/me/no/existe')
64 except OSError, err:
65 self.assertEqual(err.errno, errno.ENOENT)
66
67
68 if __name__ == '__main__':
69 unittest.main()
OLDNEW
« 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