1 #!/usr/bin/env python 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 4 # found in the LICENSE file. 5 6 from appengine_wrappers import GetAppVersion 7 from compiled_file_system import CompiledFileSystem 8 from copy import deepcopy 9 from file_system import FileNotFoundError 10 from object_store_creator import ObjectStoreCreator 11 from test_file_system import TestFileSystem 12 from test_object_store import TestObjectStore 13 import unittest 14 15 _TEST_DATA = { 16 '404.html': '404.html contents', 17 'apps': { 18 'a11y.html': 'a11y.html contents', 19 'about_apps.html': 'about_apps.html contents', 20 'fakedir': { 21 'file.html': 'file.html contents' 22 } 23 }, 24 'extensions': { 25 'activeTab.html': 'activeTab.html contents', 26 'alarms.html': 'alarms.html contents' 27 } 28 } 29 30 identity = lambda _, x: x 31 32 def _CreateFactory(): 33 return CompiledFileSystem.Factory( 34 TestFileSystem(deepcopy(_TEST_DATA)), 35 ObjectStoreCreator(start_empty=False, 36 store_type=TestObjectStore, 37 disable_wrappers=True)) 38 39 class CompiledFileSystemTest(unittest.TestCase): 40 def testPopulateNamespace(self): 41 def CheckNamespace(expected_file, expected_list, fs): 42 self.assertEqual(expected_file, fs._file_object_store.namespace) 43 self.assertEqual(expected_list, fs._list_object_store.namespace) 44 factory = _CreateFactory() 45 f = lambda x: x 46 CheckNamespace( 47 'class=CompiledFileSystem&' 48 'category=CompiledFileSystemTest/TestFileSystem/file&' 49 'app_version=%s' % GetAppVersion(), 50 'class=CompiledFileSystem&' 51 'category=CompiledFileSystemTest/TestFileSystem/list&' 52 'app_version=%s' % GetAppVersion(), 53 factory.Create(f, CompiledFileSystemTest)) 54 CheckNamespace( 55 'class=CompiledFileSystem&' 56 'category=CompiledFileSystemTest/TestFileSystem/foo/file&' 57 'app_version=%s' % GetAppVersion(), 58 'class=CompiledFileSystem&' 59 'category=CompiledFileSystemTest/TestFileSystem/foo/list&' 60 'app_version=%s' % GetAppVersion(), 61 factory.Create(f, CompiledFileSystemTest, category='foo')) 62 63 def testPopulateFromFile(self): 64 def Sleepy(key, val): 65 return '%s%s' % ('Z' * len(key), 'z' * len(val)) 66 compiled_fs = _CreateFactory().Create(Sleepy, CompiledFileSystemTest) 67 self.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz', 68 compiled_fs.GetFromFile('404.html')) 69 self.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', 70 compiled_fs.GetFromFile('apps/a11y.html')) 71 self.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz', 72 compiled_fs.GetFromFile('/apps/fakedir/file.html')) 73 74 def testCaching(self): 75 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) 76 self.assertEqual('404.html contents', compiled_fs.GetFromFile('404.html')) 77 self.assertEqual(set(('file.html',)), 78 set(compiled_fs.GetFromFileListing('apps/fakedir'))) 79 80 compiled_fs._file_system._obj['404.html'] = 'boom' 81 compiled_fs._file_system._obj['apps']['fakedir']['boom.html'] = 'blam' 82 self.assertEqual('404.html contents', compiled_fs.GetFromFile('404.html')) 83 self.assertEqual(set(('file.html',)), 84 set(compiled_fs.GetFromFileListing('apps/fakedir'))) 85 86 compiled_fs._file_system.IncrementStat() 87 self.assertEqual('boom', compiled_fs.GetFromFile('404.html')) 88 self.assertEqual(set(('file.html', 'boom.html')), 89 set(compiled_fs.GetFromFileListing('apps/fakedir'))) 90 91 def testFailures(self): 92 compiled_fs = _CreateFactory().Create(identity, CompiledFileSystemTest) 93 self.assertRaises(FileNotFoundError, compiled_fs.GetFromFile, '405.html') 94 # TODO(kalman): would be nice to test this fails since apps/ is a dir. 95 compiled_fs.GetFromFile('apps/') 96 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/') 97 self.assertRaises(FileNotFoundError, 98 compiled_fs.GetFromFileListing, 'nodir/') 99 # TODO(kalman): likewise, not a FileNotFoundError. 100 self.assertRaises(FileNotFoundError, 101 compiled_fs.GetFromFileListing, '404.html') 102 103 if __name__ == '__main__': 104 unittest.main() 105