OLD | NEW |
(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() |
OLD | NEW |