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.
9 from appengine_wrappers
import GetAppVersion
10 from compiled_file_system
import CompiledFileSystem
11 from copy
import deepcopy
12 from file_system
import FileNotFoundError
13 from mock_file_system
import MockFileSystem
14 from object_store_creator
import ObjectStoreCreator
15 from test_file_system
import TestFileSystem
16 from test_object_store
import TestObjectStore
20 '404.html': '404.html contents',
22 'a11y.html': 'a11y.html contents',
23 'about_apps.html': 'about_apps.html contents',
25 'file.html': 'file.html contents'
28 'deepfile.html': 'deepfile.html contents',
30 'deepest.html': 'deepest.html contents',
35 'activeTab.html': 'activeTab.html contents',
36 'alarms.html': 'alarms.html contents'
40 identity
= lambda _
, x
: x
42 def _GetTestCompiledFsCreator():
43 '''Returns a function which creates CompiledFileSystem views of
44 TestFileSystems backed by _TEST_DATA.
46 return functools
.partial(
47 CompiledFileSystem
.Factory(
48 ObjectStoreCreator(start_empty
=False,
49 store_type
=TestObjectStore
,
50 disable_wrappers
=True),
52 TestFileSystem(deepcopy(_TEST_DATA
)))
54 class CompiledFileSystemTest(unittest
.TestCase
):
55 def testPopulateNamespace(self
):
56 def CheckNamespace(expected_file
, expected_list
, fs
):
57 self
.assertEqual(expected_file
, fs
._file
_object
_store
.namespace
)
58 self
.assertEqual(expected_list
, fs
._list
_object
_store
.namespace
)
59 compiled_fs_creator
= _GetTestCompiledFsCreator()
62 'class=CompiledFileSystem&'
63 'category=CompiledFileSystemTest/TestFileSystem/file&'
64 'app_version=%s' % GetAppVersion(),
65 'class=CompiledFileSystem&'
66 'category=CompiledFileSystemTest/TestFileSystem/list&'
67 'app_version=%s' % GetAppVersion(),
68 compiled_fs_creator(f
, CompiledFileSystemTest
))
70 'class=CompiledFileSystem&'
71 'category=CompiledFileSystemTest/TestFileSystem/foo/file&'
72 'app_version=%s' % GetAppVersion(),
73 'class=CompiledFileSystem&'
74 'category=CompiledFileSystemTest/TestFileSystem/foo/list&'
75 'app_version=%s' % GetAppVersion(),
76 compiled_fs_creator(f
, CompiledFileSystemTest
, category
='foo'))
78 def testPopulateFromFile(self
):
80 return '%s%s' % ('Z' * len(key
), 'z' * len(val
))
81 compiled_fs
= _GetTestCompiledFsCreator()(Sleepy
, CompiledFileSystemTest
)
82 self
.assertEqual('ZZZZZZZZzzzzzzzzzzzzzzzzz',
83 compiled_fs
.GetFromFile('404.html').Get())
84 self
.assertEqual('ZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz',
85 compiled_fs
.GetFromFile('apps/a11y.html').Get())
86 self
.assertEqual('ZZZZZZZZZZZZZZZZZZZZZZZzzzzzzzzzzzzzzzzzz',
87 compiled_fs
.GetFromFile('/apps/fakedir/file.html').Get())
89 def testPopulateFromFileListing(self
):
90 def strip_ext(path
, files
):
91 return [os
.path
.splitext(f
)[0] for f
in files
]
92 compiled_fs
= _GetTestCompiledFsCreator()(strip_ext
, CompiledFileSystemTest
)
93 expected_top_listing
= [
97 'apps/deepdir/deeper/deepest',
98 'apps/deepdir/deepfile',
100 'extensions/activeTab',
103 self
.assertEqual(expected_top_listing
,
104 sorted(compiled_fs
.GetFromFileListing('/').Get()))
105 self
.assertEqual(expected_top_listing
,
106 sorted(compiled_fs
.GetFromFileListing('').Get()))
107 expected_apps_listing
= [
110 'deepdir/deeper/deepest',
114 self
.assertEqual(expected_apps_listing
,
115 sorted(compiled_fs
.GetFromFileListing('/apps/').Get()))
116 self
.assertEqual(expected_apps_listing
,
117 sorted(compiled_fs
.GetFromFileListing('apps/').Get()))
118 self
.assertEqual(['file',],
119 compiled_fs
.GetFromFileListing('/apps/fakedir/').Get())
120 self
.assertEqual(['file',],
121 compiled_fs
.GetFromFileListing('apps/fakedir/').Get())
122 self
.assertEqual(['deeper/deepest', 'deepfile'],
123 sorted(compiled_fs
.GetFromFileListing(
124 '/apps/deepdir/').Get()))
125 self
.assertEqual(['deeper/deepest', 'deepfile'],
126 sorted(compiled_fs
.GetFromFileListing(
127 'apps/deepdir/').Get()))
128 self
.assertEqual(['deepest'],
129 compiled_fs
.GetFromFileListing(
130 '/apps/deepdir/deeper/').Get())
131 self
.assertEqual(['deepest'],
132 compiled_fs
.GetFromFileListing(
133 'apps/deepdir/deeper/').Get())
135 def testCaching(self
):
136 compiled_fs
= _GetTestCompiledFsCreator()(identity
, CompiledFileSystemTest
)
137 self
.assertEqual('404.html contents',
138 compiled_fs
.GetFromFile('404.html').Get())
139 self
.assertEqual(set(('file.html',)),
140 set(compiled_fs
.GetFromFileListing('apps/fakedir').Get()))
142 compiled_fs
._file
_system
._obj
['404.html'] = 'boom'
143 compiled_fs
._file
_system
._obj
['apps']['fakedir']['boom.html'] = 'blam'
144 self
.assertEqual('404.html contents',
145 compiled_fs
.GetFromFile('404.html').Get())
146 self
.assertEqual(set(('file.html',)),
147 set(compiled_fs
.GetFromFileListing('apps/fakedir').Get()))
149 compiled_fs
._file
_system
.IncrementStat()
150 self
.assertEqual('boom', compiled_fs
.GetFromFile('404.html').Get())
151 self
.assertEqual(set(('file.html', 'boom.html')),
152 set(compiled_fs
.GetFromFileListing('apps/fakedir').Get()))
154 def testFailures(self
):
155 compiled_fs
= _GetTestCompiledFsCreator()(identity
, CompiledFileSystemTest
)
156 self
.assertRaises(FileNotFoundError
,
157 compiled_fs
.GetFromFile('405.html').Get
)
158 # TODO(kalman): would be nice to test this fails since apps/ is a dir.
159 compiled_fs
.GetFromFile('apps/')
160 #self.assertRaises(SomeError, compiled_fs.GetFromFile, 'apps/')
161 self
.assertRaises(FileNotFoundError
,
162 compiled_fs
.GetFromFileListing('nodir/').Get
)
163 # TODO(kalman): likewise, not a FileNotFoundError.
164 self
.assertRaises(FileNotFoundError
,
165 compiled_fs
.GetFromFileListing('404.html').Get
)
167 def testCorrectFutureBehaviour(self
):
168 # Tests that the underlying FileSystem's Read Future has had Get() called
169 # on it before the Future is resolved, but the underlying Future isn't
170 # resolved until Get is.
171 mock_fs
= MockFileSystem(TestFileSystem(_TEST_DATA
))
172 compiled_fs
= CompiledFileSystem
.Factory(
173 ObjectStoreCreator
.ForTest()).Create(
174 mock_fs
, lambda path
, contents
: contents
, type(self
))
176 self
.assertTrue(*mock_fs
.CheckAndReset())
177 future
= compiled_fs
.GetFromFile('404.html')
178 self
.assertTrue(*mock_fs
.CheckAndReset(stat_count
=1, read_count
=1))
180 self
.assertTrue(*mock_fs
.CheckAndReset(read_resolve_count
=1))
182 future
= compiled_fs
.GetFromFileListing('apps/')
183 # Current behaviour is to have read=2 and read_resolve=1 because the first
184 # level is read eagerly, then all of the second is read (in parallel). If
185 # it weren't eager (and it may be worth experimenting with that) then it'd
186 # be read=1 and read_resolve=0.
187 self
.assertTrue(*mock_fs
.CheckAndReset(stat_count
=1,
189 read_resolve_count
=1))
191 # It's doing 1 more level 'deeper' (already read 'fakedir' and 'deepdir'
192 # though not resolved), so that's 1 more read/resolve + the resolve from
194 self
.assertTrue(*mock_fs
.CheckAndReset(read_count
=1, read_resolve_count
=2))
196 # Even though the directory is 1 layer deep the caller has no way of
197 # determining that ahead of time (though perhaps the API could give some
198 # kind of clue, if we really cared).
199 future
= compiled_fs
.GetFromFileListing('extensions/')
200 self
.assertTrue(*mock_fs
.CheckAndReset(stat_count
=1,
202 read_resolve_count
=1))
204 self
.assertTrue(*mock_fs
.CheckAndReset())
206 # Similar configuration to the 'apps/' case but deeper.
207 future
= compiled_fs
.GetFromFileListing('/')
208 self
.assertTrue(*mock_fs
.CheckAndReset(stat_count
=1,
210 read_resolve_count
=1))
212 self
.assertTrue(*mock_fs
.CheckAndReset(read_count
=2, read_resolve_count
=3))
216 if __name__
== '__main__':