2 # Copyright (c) 2012 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.
6 from copy
import deepcopy
7 from file_system
import FileNotFoundError
, StatInfo
8 from test_file_system
import TestFileSystem
, MoveTo
13 '404.html': '404.html contents',
15 'a11y.html': 'a11y.html contents',
16 'about_apps.html': 'about_apps.html contents',
18 'file.html': 'file.html contents'
22 'activeTab.html': 'activeTab.html contents',
23 'alarms.html': 'alarms.html contents'
29 '''Returns a function which calls Future.Get on the result of |fn|.
31 return lambda *args
: fn(*args
).Get()
34 class TestFileSystemTest(unittest
.TestCase
):
35 def testEmptyFileSystem(self
):
36 self
._TestMetasyntacticPaths
(TestFileSystem({}))
38 def testNonemptyFileNotFoundErrors(self
):
39 fs
= TestFileSystem(deepcopy(_TEST_DATA
))
40 self
._TestMetasyntacticPaths
(fs
)
41 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['404.html/'])
42 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['apps/foo/'])
43 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['apps/foo.html'])
44 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['apps/foo.html'])
45 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['apps/foo/',
47 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['apps/foo/',
50 def _TestMetasyntacticPaths(self
, fs
):
51 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['foo'])
52 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['bar/'])
53 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['bar/baz'])
54 self
.assertRaises(FileNotFoundError
, _Get(fs
.Read
), ['foo',
57 self
.assertRaises(FileNotFoundError
, fs
.Stat
, 'foo')
58 self
.assertRaises(FileNotFoundError
, fs
.Stat
, 'bar/')
59 self
.assertRaises(FileNotFoundError
, fs
.Stat
, 'bar/baz')
61 def testNonemptySuccess(self
):
62 fs
= TestFileSystem(deepcopy(_TEST_DATA
))
63 self
.assertEqual('404.html contents', fs
.ReadSingle('404.html').Get())
64 self
.assertEqual('404.html contents', fs
.ReadSingle('/404.html').Get())
65 self
.assertEqual('a11y.html contents',
66 fs
.ReadSingle('apps/a11y.html').Get())
67 self
.assertEqual(['404.html', 'apps/', 'extensions/'],
68 sorted(fs
.ReadSingle('/').Get()))
69 self
.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
70 sorted(fs
.ReadSingle('apps/').Get()))
71 self
.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
72 sorted(fs
.ReadSingle('/apps/').Get()))
74 def testReadFiles(self
):
75 fs
= TestFileSystem(deepcopy(_TEST_DATA
))
76 self
.assertEqual('404.html contents',
77 fs
.ReadSingle('404.html').Get())
78 self
.assertEqual('404.html contents',
79 fs
.ReadSingle('/404.html').Get())
80 self
.assertEqual('a11y.html contents',
81 fs
.ReadSingle('apps/a11y.html').Get())
82 self
.assertEqual('a11y.html contents',
83 fs
.ReadSingle('/apps/a11y.html').Get())
84 self
.assertEqual('file.html contents',
85 fs
.ReadSingle('apps/fakedir/file.html').Get())
86 self
.assertEqual('file.html contents',
87 fs
.ReadSingle('/apps/fakedir/file.html').Get())
89 def testReadDirs(self
):
90 fs
= TestFileSystem(deepcopy(_TEST_DATA
))
91 self
.assertEqual(['404.html', 'apps/', 'extensions/'],
92 sorted(fs
.ReadSingle('/').Get()))
93 self
.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
94 sorted(fs
.ReadSingle('/apps/').Get()))
95 self
.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
96 sorted(fs
.ReadSingle('apps/').Get()))
97 self
.assertEqual(['file.html'], fs
.ReadSingle('/apps/fakedir/').Get())
98 self
.assertEqual(['file.html'], fs
.ReadSingle('apps/fakedir/').Get())
101 fs
= TestFileSystem(deepcopy(_TEST_DATA
))
102 self
.assertRaises(FileNotFoundError
, fs
.Stat
, 'foo')
103 self
.assertRaises(FileNotFoundError
, fs
.Stat
, '404.html/')
104 self
.assertEquals(StatInfo('0'), fs
.Stat('404.html'))
105 self
.assertEquals(StatInfo('0', child_versions
={
106 'activeTab.html': '0',
108 }), fs
.Stat('extensions/'))
111 self
.assertEquals(StatInfo('1'), fs
.Stat('404.html'))
112 self
.assertEquals(StatInfo('1', child_versions
={
113 'activeTab.html': '1',
115 }), fs
.Stat('extensions/'))
117 fs
.IncrementStat(path
='404.html')
118 self
.assertEquals(StatInfo('2'), fs
.Stat('404.html'))
119 self
.assertEquals(StatInfo('1', child_versions
={
120 'activeTab.html': '1',
122 }), fs
.Stat('extensions/'))
125 self
.assertEquals(StatInfo('3'), fs
.Stat('404.html'))
126 self
.assertEquals(StatInfo('2', child_versions
={
127 'activeTab.html': '2',
129 }), fs
.Stat('extensions/'))
131 fs
.IncrementStat(path
='extensions/')
132 self
.assertEquals(StatInfo('3'), fs
.Stat('404.html'))
133 self
.assertEquals(StatInfo('3', child_versions
={
134 'activeTab.html': '2',
136 }), fs
.Stat('extensions/'))
138 fs
.IncrementStat(path
='extensions/alarms.html')
139 self
.assertEquals(StatInfo('3'), fs
.Stat('404.html'))
140 self
.assertEquals(StatInfo('3', child_versions
={
141 'activeTab.html': '2',
143 }), fs
.Stat('extensions/'))
145 def testMoveTo(self
):
146 self
.assertEqual({'foo': {'a': 'b', 'c': 'd'}},
147 MoveTo('foo', {'a': 'b', 'c': 'd'}))
148 self
.assertEqual({'foo': {'bar': {'a': 'b', 'c': 'd'}}},
149 MoveTo('foo/bar', {'a': 'b', 'c': 'd'}))
150 self
.assertEqual({'foo': {'bar': {'baz': {'a': 'b'}}}},
151 MoveTo('foo/bar/baz', {'a': 'b'}))
154 if __name__
== '__main__':