| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import functools |
| 6 import os | 7 import os |
| 7 | 8 |
| 8 from appengine_wrappers import GetAppVersion | 9 from appengine_wrappers import GetAppVersion |
| 9 from compiled_file_system import CompiledFileSystem | 10 from compiled_file_system import CompiledFileSystem |
| 10 from copy import deepcopy | 11 from copy import deepcopy |
| 11 from file_system import FileNotFoundError, FileSystem, StatInfo | 12 from file_system import FileNotFoundError, FileSystem, StatInfo |
| 12 from future import Gettable, Future | 13 from future import Gettable, Future |
| 13 from mock_file_system import MockFileSystem | 14 from mock_file_system import MockFileSystem |
| 14 from object_store_creator import ObjectStoreCreator | 15 from object_store_creator import ObjectStoreCreator |
| 15 from test_file_system import TestFileSystem | 16 from test_file_system import TestFileSystem |
| (...skipping 16 matching lines...) Expand all Loading... |
| 32 } | 33 } |
| 33 }, | 34 }, |
| 34 'extensions': { | 35 'extensions': { |
| 35 'activeTab.html': 'activeTab.html contents', | 36 'activeTab.html': 'activeTab.html contents', |
| 36 'alarms.html': 'alarms.html contents' | 37 'alarms.html': 'alarms.html contents' |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 identity = lambda _, x: x | 41 identity = lambda _, x: x |
| 41 | 42 |
| 42 def _CreateFactory(): | 43 def _GetTestCompiledFsCreator(): |
| 43 return CompiledFileSystem.Factory( | 44 '''Returns a function which creates CompiledFileSystem views of |
| 44 TestFileSystem(deepcopy(_TEST_DATA)), | 45 TestFileSystems backed by _TEST_DATA. |
| 45 ObjectStoreCreator(start_empty=False, | 46 ''' |
| 46 store_type=TestObjectStore, | 47 return functools.partial( |
| 47 disable_wrappers=True)) | 48 CompiledFileSystem.Factory( |
| 49 ObjectStoreCreator(start_empty=False, |
| 50 store_type=TestObjectStore, |
| 51 disable_wrappers=True), |
| 52 ).Create, |
| 53 TestFileSystem(deepcopy(_TEST_DATA))) |
| 48 | 54 |
| 49 class CompiledFileSystemTest(unittest.TestCase): | 55 class CompiledFileSystemTest(unittest.TestCase): |
| 50 def testPopulateNamespace(self): | 56 def testPopulateNamespace(self): |
| 51 def CheckNamespace(expected_file, expected_list, fs): | 57 def CheckNamespace(expected_file, expected_list, fs): |
| 52 self.assertEqual(expected_file, fs._file_object_store.namespace) | 58 self.assertEqual(expected_file, fs._file_object_store.namespace) |
| 53 self.assertEqual(expected_list, fs._list_object_store.namespace) | 59 self.assertEqual(expected_list, fs._list_object_store.namespace) |
| 54 factory = _CreateFactory() | 60 compiled_fs_creator = _GetTestCompiledFsCreator() |
| 55 f = lambda x: x | 61 f = lambda x: x |
| 56 CheckNamespace( | 62 CheckNamespace( |
| 57 'class=CompiledFileSystem&' | 63 'class=CompiledFileSystem&' |
| 58 'category=CompiledFileSystemTest/TestFileSystem/file&' | 64 'category=CompiledFileSystemTest/TestFileSystem/file&' |
| 59 'app_version=%s' % GetAppVersion(), | 65 'app_version=%s' % GetAppVersion(), |
| 60 'class=CompiledFileSystem&' | 66 'class=CompiledFileSystem&' |
| 61 'category=CompiledFileSystemTest/TestFileSystem/list&' | 67 'category=CompiledFileSystemTest/TestFileSystem/list&' |
| 62 'app_version=%s' % GetAppVersion(), | 68 'app_version=%s' % GetAppVersion(), |
| 63 factory.Create(f, CompiledFileSystemTest)) | 69 compiled_fs_creator(f, CompiledFileSystemTest)) |
| 64 CheckNamespace( | 70 CheckNamespace( |
| 65 'class=CompiledFileSystem&' | 71 'class=CompiledFileSystem&' |
| 66 'category=CompiledFileSystemTest/TestFileSystem/foo/file&' | 72 'category=CompiledFileSystemTest/TestFileSystem/foo/file&' |
| 67 'app_version=%s' % GetAppVersion(), | 73 'app_version=%s' % GetAppVersion(), |
| 68 'class=CompiledFileSystem&' | 74 'class=CompiledFileSystem&' |
| 69 'category=CompiledFileSystemTest/TestFileSystem/foo/list&' | 75 'category=CompiledFileSystemTest/TestFileSystem/foo/list&' |
| 70 'app_version=%s' % GetAppVersion(), | 76 'app_version=%s' % GetAppVersion(), |
| 71 factory.Create(f, CompiledFileSystemTest, category='foo')) | 77 compiled_fs_creator(f, CompiledFileSystemTest, category='foo')) |
| 72 | 78 |
| 73 def testPopulateFromFile(self): | 79 def testPopulateFromFile(self): |
| 74 def Sleepy(key, val): | 80 def Sleepy(key, val): |
| 75 return '%s%s' % ('Z' * len(key), 'z' * len(val)) | 81 return '%s%s' % ('Z' * len(key), 'z' * len(val)) |
| 76 compiled_fs = _CreateFactory().Create(Sleepy, CompiledFileSystemTest) | 82 compiled_fs = _GetTestCompiledFsCreator()(Sleepy, CompiledFileSystemTest) |
| 77 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', | 83 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', |
| 78 compiled_fs.GetFromFile('404.html').Get()) | 84 compiled_fs.GetFromFile('404.html').Get()) |
| 79 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', | 85 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 80 compiled_fs.GetFromFile('apps/a11y.html').Get()) | 86 compiled_fs.GetFromFile('apps/a11y.html').Get()) |
| 81 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', | 87 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 82 compiled_fs.GetFromFile('/apps/fakedir/file.html').Get()) | 88 compiled_fs.GetFromFile('/apps/fakedir/file.html').Get()) |
| 83 | 89 |
| 84 def testPopulateFromFileListing(self): | 90 def testPopulateFromFileListing(self): |
| 85 def strip_ext(path, files): | 91 def strip_ext(path, files): |
| 86 return [os.path.splitext(f)[0] for f in files] | 92 return [os.path.splitext(f)[0] for f in files] |
| 87 | 93 compiled_fs = _GetTestCompiledFsCreator()(strip_ext, CompiledFileSystemTest) |
| 88 compiled_fs = _CreateFactory().Create(strip_ext, CompiledFileSystemTest) | |
| 89 expected_top_listing = [ | 94 expected_top_listing = [ |
| 90 '404', | 95 '404', |
| 91 'apps/a11y', | 96 'apps/a11y', |
| 92 'apps/about_apps', | 97 'apps/about_apps', |
| 93 'apps/deepdir/deeper/deepest', | 98 'apps/deepdir/deeper/deepest', |
| 94 'apps/deepdir/deepfile', | 99 'apps/deepdir/deepfile', |
| 95 'apps/fakedir/file', | 100 'apps/fakedir/file', |
| 96 'extensions/activeTab', | 101 'extensions/activeTab', |
| 97 'extensions/alarms' | 102 'extensions/alarms' |
| 98 ] | 103 ] |
| (...skipping 23 matching lines...) Expand all Loading... |
| 122 sorted(compiled_fs.GetFromFileListing( | 127 sorted(compiled_fs.GetFromFileListing( |
| 123 'apps/deepdir/').Get())) | 128 'apps/deepdir/').Get())) |
| 124 self.assertEqual(['deepest'], | 129 self.assertEqual(['deepest'], |
| 125 compiled_fs.GetFromFileListing( | 130 compiled_fs.GetFromFileListing( |
| 126 '/apps/deepdir/deeper/').Get()) | 131 '/apps/deepdir/deeper/').Get()) |
| 127 self.assertEqual(['deepest'], | 132 self.assertEqual(['deepest'], |
| 128 compiled_fs.GetFromFileListing( | 133 compiled_fs.GetFromFileListing( |
| 129 'apps/deepdir/deeper/').Get()) | 134 'apps/deepdir/deeper/').Get()) |
| 130 | 135 |
| 131 def testCaching(self): | 136 def testCaching(self): |
| 132 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) | 137 compiled_fs = _GetTestCompiledFsCreator()(identity, CompiledFileSystemTest) |
| 133 self.assertEqual('404.html contents', | 138 self.assertEqual('404.html contents', |
| 134 compiled_fs.GetFromFile('404.html').Get()) | 139 compiled_fs.GetFromFile('404.html').Get()) |
| 135 self.assertEqual(set(('file.html',)), | 140 self.assertEqual(set(('file.html',)), |
| 136 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 141 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 137 | 142 |
| 138 compiled_fs._file_system._obj['404.html'] = 'boom' | 143 compiled_fs._file_system._obj['404.html'] = 'boom' |
| 139 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' | 144 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' |
| 140 self.assertEqual('404.html contents', | 145 self.assertEqual('404.html contents', |
| 141 compiled_fs.GetFromFile('404.html').Get()) | 146 compiled_fs.GetFromFile('404.html').Get()) |
| 142 self.assertEqual(set(('file.html',)), | 147 self.assertEqual(set(('file.html',)), |
| 143 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 148 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 144 | 149 |
| 145 compiled_fs._file_system.IncrementStat() | 150 compiled_fs._file_system.IncrementStat() |
| 146 self.assertEqual('boom', compiled_fs.GetFromFile('404.html').Get()) | 151 self.assertEqual('boom', compiled_fs.GetFromFile('404.html').Get()) |
| 147 self.assertEqual(set(('file.html', 'boom.html')), | 152 self.assertEqual(set(('file.html', 'boom.html')), |
| 148 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 153 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 149 | 154 |
| 150 def testFailures(self): | 155 def testFailures(self): |
| 151 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) | 156 compiled_fs = _GetTestCompiledFsCreator()(identity, CompiledFileSystemTest) |
| 152 self.assertRaises(FileNotFoundError, | 157 self.assertRaises(FileNotFoundError, |
| 153 compiled_fs.GetFromFile('405.html').Get) | 158 compiled_fs.GetFromFile('405.html').Get) |
| 154 # TODO(kalman): would be nice to test this fails since apps/ is a dir. | 159 # TODO(kalman): would be nice to test this fails since apps/ is a dir. |
| 155 compiled_fs.GetFromFile('apps/') | 160 compiled_fs.GetFromFile('apps/') |
| 156 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') | 161 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') |
| 157 self.assertRaises(FileNotFoundError, | 162 self.assertRaises(FileNotFoundError, |
| 158 compiled_fs.GetFromFileListing('nodir/').Get) | 163 compiled_fs.GetFromFileListing('nodir/').Get) |
| 159 # TODO(kalman): likewise, not a FileNotFoundError. | 164 # TODO(kalman): likewise, not a FileNotFoundError. |
| 160 self.assertRaises(FileNotFoundError, | 165 self.assertRaises(FileNotFoundError, |
| 161 compiled_fs.GetFromFileListing('404.html').Get) | 166 compiled_fs.GetFromFileListing('404.html').Get) |
| 162 | 167 |
| 163 def testCorrectFutureBehaviour(self): | 168 def testCorrectFutureBehaviour(self): |
| 164 # Tests that the underlying FileSystem's Read Future has had Get() called | 169 # Tests that the underlying FileSystem's Read Future has had Get() called |
| 165 # on it before the Future is resolved, but the underlying Future isn't | 170 # on it before the Future is resolved, but the underlying Future isn't |
| 166 # resolved until Get is. | 171 # resolved until Get is. |
| 167 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA)) | 172 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA)) |
| 168 compiled_fs = CompiledFileSystem.Factory( | 173 compiled_fs = CompiledFileSystem.Factory( |
| 169 mock_fs, ObjectStoreCreator.ForTest()).Create( | 174 ObjectStoreCreator.ForTest()).Create( |
| 170 lambda path, contents: contents, type(self)) | 175 mock_fs, lambda path, contents: contents, type(self)) |
| 171 | 176 |
| 172 self.assertTrue(*mock_fs.CheckAndReset()) | 177 self.assertTrue(*mock_fs.CheckAndReset()) |
| 173 future = compiled_fs.GetFromFile('404.html') | 178 future = compiled_fs.GetFromFile('404.html') |
| 174 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, read_count=1)) | 179 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, read_count=1)) |
| 175 future.Get() | 180 future.Get() |
| 176 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1)) | 181 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1)) |
| 177 | 182 |
| 178 future = compiled_fs.GetFromFileListing('apps/') | 183 future = compiled_fs.GetFromFileListing('apps/') |
| 179 # Current behaviour is to have read=2 and read_resolve=1 because the first | 184 # Current behaviour is to have read=2 and read_resolve=1 because the first |
| 180 # level is read eagerly, then all of the second is read (in parallel). If | 185 # level is read eagerly, then all of the second is read (in parallel). If |
| (...skipping 23 matching lines...) Expand all Loading... |
| 204 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, | 209 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, |
| 205 read_count=2, | 210 read_count=2, |
| 206 read_resolve_count=1)) | 211 read_resolve_count=1)) |
| 207 future.Get() | 212 future.Get() |
| 208 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3)) | 213 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3)) |
| 209 | 214 |
| 210 | 215 |
| 211 | 216 |
| 212 if __name__ == '__main__': | 217 if __name__ == '__main__': |
| 213 unittest.main() | 218 unittest.main() |
| OLD | NEW |