Chromium Code Reviews| 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 os | 6 import os |
| 7 | 7 |
| 8 from appengine_wrappers import GetAppVersion | 8 from appengine_wrappers import GetAppVersion |
| 9 from compiled_file_system import CompiledFileSystem | 9 from compiled_file_system import CompiledFileSystem |
| 10 from copy import deepcopy | 10 from copy import deepcopy |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 } | 32 } |
| 33 }, | 33 }, |
| 34 'extensions': { | 34 'extensions': { |
| 35 'activeTab.html': 'activeTab.html contents', | 35 'activeTab.html': 'activeTab.html contents', |
| 36 'alarms.html': 'alarms.html contents' | 36 'alarms.html': 'alarms.html contents' |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 identity = lambda _, x: x | 40 identity = lambda _, x: x |
| 41 | 41 |
| 42 def _CreateFactory(): | 42 def _CreateTestObjects(): |
|
Jeffrey Yasskin
2013/10/14 18:52:31
Instead of returning an obscure tuple, how about r
not at google - send to devlin
2013/10/14 21:03:34
Done.
| |
| 43 return CompiledFileSystem.Factory( | 43 '''Returns a tuple (CompiledFileSystem.Factory, FileSystem) configured for |
| 44 TestFileSystem(deepcopy(_TEST_DATA)), | 44 this test. |
| 45 ObjectStoreCreator(start_empty=False, | 45 ''' |
| 46 store_type=TestObjectStore, | 46 return ( |
| 47 disable_wrappers=True)) | 47 CompiledFileSystem.Factory( |
| 48 ObjectStoreCreator(start_empty=False, | |
| 49 store_type=TestObjectStore, | |
| 50 disable_wrappers=True)), | |
| 51 TestFileSystem(deepcopy(_TEST_DATA))) | |
| 48 | 52 |
| 49 class CompiledFileSystemTest(unittest.TestCase): | 53 class CompiledFileSystemTest(unittest.TestCase): |
| 50 def testPopulateNamespace(self): | 54 def testPopulateNamespace(self): |
| 51 def CheckNamespace(expected_file, expected_list, fs): | 55 def CheckNamespace(expected_file, expected_list, fs): |
| 52 self.assertEqual(expected_file, fs._file_object_store.namespace) | 56 self.assertEqual(expected_file, fs._file_object_store.namespace) |
| 53 self.assertEqual(expected_list, fs._list_object_store.namespace) | 57 self.assertEqual(expected_list, fs._list_object_store.namespace) |
| 54 factory = _CreateFactory() | 58 factory, fs = _CreateTestObjects() |
| 55 f = lambda x: x | 59 f = lambda x: x |
| 56 CheckNamespace( | 60 CheckNamespace( |
| 57 'class=CompiledFileSystem&' | 61 'class=CompiledFileSystem&' |
| 58 'category=CompiledFileSystemTest/TestFileSystem/file&' | 62 'category=CompiledFileSystemTest/TestFileSystem/file&' |
| 59 'app_version=%s' % GetAppVersion(), | 63 'app_version=%s' % GetAppVersion(), |
| 60 'class=CompiledFileSystem&' | 64 'class=CompiledFileSystem&' |
| 61 'category=CompiledFileSystemTest/TestFileSystem/list&' | 65 'category=CompiledFileSystemTest/TestFileSystem/list&' |
| 62 'app_version=%s' % GetAppVersion(), | 66 'app_version=%s' % GetAppVersion(), |
| 63 factory.Create(f, CompiledFileSystemTest)) | 67 factory.Create(fs, f, CompiledFileSystemTest)) |
| 64 CheckNamespace( | 68 CheckNamespace( |
| 65 'class=CompiledFileSystem&' | 69 'class=CompiledFileSystem&' |
| 66 'category=CompiledFileSystemTest/TestFileSystem/foo/file&' | 70 'category=CompiledFileSystemTest/TestFileSystem/foo/file&' |
| 67 'app_version=%s' % GetAppVersion(), | 71 'app_version=%s' % GetAppVersion(), |
| 68 'class=CompiledFileSystem&' | 72 'class=CompiledFileSystem&' |
| 69 'category=CompiledFileSystemTest/TestFileSystem/foo/list&' | 73 'category=CompiledFileSystemTest/TestFileSystem/foo/list&' |
| 70 'app_version=%s' % GetAppVersion(), | 74 'app_version=%s' % GetAppVersion(), |
| 71 factory.Create(f, CompiledFileSystemTest, category='foo')) | 75 factory.Create(fs, f, CompiledFileSystemTest, category='foo')) |
| 72 | 76 |
| 73 def testPopulateFromFile(self): | 77 def testPopulateFromFile(self): |
| 74 def Sleepy(key, val): | 78 def Sleepy(key, val): |
| 75 return '%s%s' % ('Z' * len(key), 'z' * len(val)) | 79 return '%s%s' % ('Z' * len(key), 'z' * len(val)) |
| 76 compiled_fs = _CreateFactory().Create(Sleepy, CompiledFileSystemTest) | 80 factory, fs = _CreateTestObjects() |
| 81 compiled_fs = factory.Create(fs, Sleepy, CompiledFileSystemTest) | |
| 77 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', | 82 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', |
| 78 compiled_fs.GetFromFile('404.html').Get()) | 83 compiled_fs.GetFromFile('404.html').Get()) |
| 79 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', | 84 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 80 compiled_fs.GetFromFile('apps/a11y.html').Get()) | 85 compiled_fs.GetFromFile('apps/a11y.html').Get()) |
| 81 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', | 86 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', |
| 82 compiled_fs.GetFromFile('/apps/fakedir/file.html').Get()) | 87 compiled_fs.GetFromFile('/apps/fakedir/file.html').Get()) |
| 83 | 88 |
| 84 def testPopulateFromFileListing(self): | 89 def testPopulateFromFileListing(self): |
| 85 def strip_ext(path, files): | 90 def strip_ext(path, files): |
| 86 return [os.path.splitext(f)[0] for f in files] | 91 return [os.path.splitext(f)[0] for f in files] |
| 87 | 92 |
| 88 compiled_fs = _CreateFactory().Create(strip_ext, CompiledFileSystemTest) | 93 factory, fs = _CreateTestObjects() |
| 94 compiled_fs = factory.Create(fs, strip_ext, CompiledFileSystemTest) | |
| 89 expected_top_listing = [ | 95 expected_top_listing = [ |
| 90 '404', | 96 '404', |
| 91 'apps/a11y', | 97 'apps/a11y', |
| 92 'apps/about_apps', | 98 'apps/about_apps', |
| 93 'apps/deepdir/deeper/deepest', | 99 'apps/deepdir/deeper/deepest', |
| 94 'apps/deepdir/deepfile', | 100 'apps/deepdir/deepfile', |
| 95 'apps/fakedir/file', | 101 'apps/fakedir/file', |
| 96 'extensions/activeTab', | 102 'extensions/activeTab', |
| 97 'extensions/alarms' | 103 'extensions/alarms' |
| 98 ] | 104 ] |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 122 sorted(compiled_fs.GetFromFileListing( | 128 sorted(compiled_fs.GetFromFileListing( |
| 123 'apps/deepdir/').Get())) | 129 'apps/deepdir/').Get())) |
| 124 self.assertEqual(['deepest'], | 130 self.assertEqual(['deepest'], |
| 125 compiled_fs.GetFromFileListing( | 131 compiled_fs.GetFromFileListing( |
| 126 '/apps/deepdir/deeper/').Get()) | 132 '/apps/deepdir/deeper/').Get()) |
| 127 self.assertEqual(['deepest'], | 133 self.assertEqual(['deepest'], |
| 128 compiled_fs.GetFromFileListing( | 134 compiled_fs.GetFromFileListing( |
| 129 'apps/deepdir/deeper/').Get()) | 135 'apps/deepdir/deeper/').Get()) |
| 130 | 136 |
| 131 def testCaching(self): | 137 def testCaching(self): |
| 132 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) | 138 factory, fs = _CreateTestObjects() |
| 139 compiled_fs = factory.Create(fs, identity, CompiledFileSystemTest) | |
| 133 self.assertEqual('404.html contents', | 140 self.assertEqual('404.html contents', |
| 134 compiled_fs.GetFromFile('404.html').Get()) | 141 compiled_fs.GetFromFile('404.html').Get()) |
| 135 self.assertEqual(set(('file.html',)), | 142 self.assertEqual(set(('file.html',)), |
| 136 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 143 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 137 | 144 |
| 138 compiled_fs._file_system._obj['404.html'] = 'boom' | 145 compiled_fs._file_system._obj['404.html'] = 'boom' |
| 139 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' | 146 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' |
| 140 self.assertEqual('404.html contents', | 147 self.assertEqual('404.html contents', |
| 141 compiled_fs.GetFromFile('404.html').Get()) | 148 compiled_fs.GetFromFile('404.html').Get()) |
| 142 self.assertEqual(set(('file.html',)), | 149 self.assertEqual(set(('file.html',)), |
| 143 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 150 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 144 | 151 |
| 145 compiled_fs._file_system.IncrementStat() | 152 compiled_fs._file_system.IncrementStat() |
| 146 self.assertEqual('boom', compiled_fs.GetFromFile('404.html').Get()) | 153 self.assertEqual('boom', compiled_fs.GetFromFile('404.html').Get()) |
| 147 self.assertEqual(set(('file.html', 'boom.html')), | 154 self.assertEqual(set(('file.html', 'boom.html')), |
| 148 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) | 155 set(compiled_fs.GetFromFileListing('apps/fakedir').Get())) |
| 149 | 156 |
| 150 def testFailures(self): | 157 def testFailures(self): |
| 151 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) | 158 factory, fs = _CreateTestObjects() |
| 159 compiled_fs = factory.Create(fs, identity, CompiledFileSystemTest) | |
| 152 self.assertRaises(FileNotFoundError, | 160 self.assertRaises(FileNotFoundError, |
| 153 compiled_fs.GetFromFile('405.html').Get) | 161 compiled_fs.GetFromFile('405.html').Get) |
| 154 # TODO(kalman): would be nice to test this fails since apps/ is a dir. | 162 # TODO(kalman): would be nice to test this fails since apps/ is a dir. |
| 155 compiled_fs.GetFromFile('apps/') | 163 compiled_fs.GetFromFile('apps/') |
| 156 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') | 164 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') |
| 157 self.assertRaises(FileNotFoundError, | 165 self.assertRaises(FileNotFoundError, |
| 158 compiled_fs.GetFromFileListing('nodir/').Get) | 166 compiled_fs.GetFromFileListing('nodir/').Get) |
| 159 # TODO(kalman): likewise, not a FileNotFoundError. | 167 # TODO(kalman): likewise, not a FileNotFoundError. |
| 160 self.assertRaises(FileNotFoundError, | 168 self.assertRaises(FileNotFoundError, |
| 161 compiled_fs.GetFromFileListing('404.html').Get) | 169 compiled_fs.GetFromFileListing('404.html').Get) |
| 162 | 170 |
| 163 def testCorrectFutureBehaviour(self): | 171 def testCorrectFutureBehaviour(self): |
| 164 # Tests that the underlying FileSystem's Read Future has had Get() called | 172 # 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 | 173 # on it before the Future is resolved, but the underlying Future isn't |
| 166 # resolved until Get is. | 174 # resolved until Get is. |
| 167 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA)) | 175 mock_fs = MockFileSystem(TestFileSystem(_TEST_DATA)) |
| 168 compiled_fs = CompiledFileSystem.Factory( | 176 compiled_fs = CompiledFileSystem.Factory( |
| 169 mock_fs, ObjectStoreCreator.ForTest()).Create( | 177 ObjectStoreCreator.ForTest()).Create( |
| 170 lambda path, contents: contents, type(self)) | 178 mock_fs, lambda path, contents: contents, type(self)) |
| 171 | 179 |
| 172 self.assertTrue(*mock_fs.CheckAndReset()) | 180 self.assertTrue(*mock_fs.CheckAndReset()) |
| 173 future = compiled_fs.GetFromFile('404.html') | 181 future = compiled_fs.GetFromFile('404.html') |
| 174 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, read_count=1)) | 182 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, read_count=1)) |
| 175 future.Get() | 183 future.Get() |
| 176 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1)) | 184 self.assertTrue(*mock_fs.CheckAndReset(read_resolve_count=1)) |
| 177 | 185 |
| 178 future = compiled_fs.GetFromFileListing('apps/') | 186 future = compiled_fs.GetFromFileListing('apps/') |
| 179 # Current behaviour is to have read=2 and read_resolve=1 because the first | 187 # 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 | 188 # 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, | 212 self.assertTrue(*mock_fs.CheckAndReset(stat_count=1, |
| 205 read_count=2, | 213 read_count=2, |
| 206 read_resolve_count=1)) | 214 read_resolve_count=1)) |
| 207 future.Get() | 215 future.Get() |
| 208 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3)) | 216 self.assertTrue(*mock_fs.CheckAndReset(read_count=2, read_resolve_count=3)) |
| 209 | 217 |
| 210 | 218 |
| 211 | 219 |
| 212 if __name__ == '__main__': | 220 if __name__ == '__main__': |
| 213 unittest.main() | 221 unittest.main() |
| OLD | NEW |