OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 from copy import deepcopy |
6 from file_system import FileNotFoundError, StatInfo | 7 from file_system import FileNotFoundError, StatInfo |
7 from test_file_system import TestFileSystem | 8 from test_file_system import TestFileSystem |
8 import unittest | 9 import unittest |
9 | 10 |
| 11 _TEST_DATA = { |
| 12 '404.html': '404.html contents', |
| 13 'apps': { |
| 14 'a11y.html': 'a11y.html contents', |
| 15 'about_apps.html': 'about_apps.html contents', |
| 16 'fakedir': { |
| 17 'file.html': 'file.html contents' |
| 18 } |
| 19 }, |
| 20 'extensions': { |
| 21 'activeTab.html': 'activeTab.html contents', |
| 22 'alarms.html': 'alarms.html contents' |
| 23 } |
| 24 } |
| 25 |
10 def _Get(fn): | 26 def _Get(fn): |
11 '''Returns a function which calls Future.Get on the result of |fn|. | 27 '''Returns a function which calls Future.Get on the result of |fn|. |
12 ''' | 28 ''' |
13 return lambda *args: fn(*args).Get() | 29 return lambda *args: fn(*args).Get() |
14 | 30 |
15 def _CreateTestData(): | |
16 return { | |
17 '404.html': '404.html contents', | |
18 'apps': { | |
19 'a11y.html': 'a11y.html contents', | |
20 'about_apps.html': 'about_apps.html contents', | |
21 'fakedir': { | |
22 'file.html': 'file.html contents' | |
23 } | |
24 }, | |
25 'extensions': { | |
26 'activeTab.html': 'activeTab.html contents', | |
27 'alarms.html': 'alarms.html contents' | |
28 } | |
29 } | |
30 | |
31 class TestFileSystemTest(unittest.TestCase): | 31 class TestFileSystemTest(unittest.TestCase): |
32 def testEmptyFileSystem(self): | 32 def testEmptyFileSystem(self): |
33 self._TestMetasyntacticPaths(TestFileSystem({})) | 33 self._TestMetasyntacticPaths(TestFileSystem({})) |
34 | 34 |
35 def testNonemptyFileNotFoundErrors(self): | 35 def testNonemptyFileNotFoundErrors(self): |
36 fs = TestFileSystem(_CreateTestData()) | 36 fs = TestFileSystem(deepcopy(_TEST_DATA)) |
37 self._TestMetasyntacticPaths(fs) | 37 self._TestMetasyntacticPaths(fs) |
38 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['404.html/']) | 38 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['404.html/']) |
39 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/']) | 39 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/']) |
40 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html']) | 40 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html']) |
41 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html']) | 41 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html']) |
42 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/', | 42 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/', |
43 'apps/foo.html']) | 43 'apps/foo.html']) |
44 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/', | 44 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/', |
45 'apps/a11y.html']) | 45 'apps/a11y.html']) |
46 | 46 |
47 def _TestMetasyntacticPaths(self, fs): | 47 def _TestMetasyntacticPaths(self, fs): |
48 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo']) | 48 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo']) |
49 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/']) | 49 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/']) |
50 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/baz']) | 50 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/baz']) |
51 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo', | 51 self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo', |
52 'bar/', | 52 'bar/', |
53 'bar/baz']) | 53 'bar/baz']) |
54 self.assertRaises(FileNotFoundError, fs.Stat, 'foo') | 54 self.assertRaises(FileNotFoundError, fs.Stat, 'foo') |
55 self.assertRaises(FileNotFoundError, fs.Stat, 'bar/') | 55 self.assertRaises(FileNotFoundError, fs.Stat, 'bar/') |
56 self.assertRaises(FileNotFoundError, fs.Stat, 'bar/baz') | 56 self.assertRaises(FileNotFoundError, fs.Stat, 'bar/baz') |
57 | 57 |
58 def testNonemptySuccess(self): | 58 def testNonemptySuccess(self): |
59 fs = TestFileSystem(_CreateTestData()) | 59 fs = TestFileSystem(deepcopy(_TEST_DATA)) |
60 self.assertEqual('404.html contents', fs.ReadSingle('404.html')) | 60 self.assertEqual('404.html contents', fs.ReadSingle('404.html')) |
61 self.assertEqual('404.html contents', fs.ReadSingle('/404.html')) | 61 self.assertEqual('404.html contents', fs.ReadSingle('/404.html')) |
62 self.assertEqual('a11y.html contents', fs.ReadSingle('apps/a11y.html')) | 62 self.assertEqual('a11y.html contents', fs.ReadSingle('apps/a11y.html')) |
63 self.assertEqual(set(['404.html', 'apps/', 'extensions/']), | 63 self.assertEqual(set(['404.html', 'apps/', 'extensions/']), |
64 set(fs.ReadSingle('/'))) | 64 set(fs.ReadSingle('/'))) |
65 self.assertEqual(set(['a11y.html', 'about_apps.html', 'fakedir/']), | 65 self.assertEqual(set(['a11y.html', 'about_apps.html', 'fakedir/']), |
66 set(fs.ReadSingle('apps/'))) | 66 set(fs.ReadSingle('apps/'))) |
67 self.assertEqual(set(['a11y.html', 'about_apps.html', 'fakedir/']), | 67 self.assertEqual(set(['a11y.html', 'about_apps.html', 'fakedir/']), |
68 set(fs.ReadSingle('/apps/'))) | 68 set(fs.ReadSingle('/apps/'))) |
69 | 69 |
70 def testStat(self): | 70 def testStat(self): |
71 fs = TestFileSystem(_CreateTestData()) | 71 fs = TestFileSystem(deepcopy(_TEST_DATA)) |
72 self.assertRaises(FileNotFoundError, fs.Stat, 'foo') | 72 self.assertRaises(FileNotFoundError, fs.Stat, 'foo') |
73 self.assertRaises(FileNotFoundError, fs.Stat, '404.html/') | 73 self.assertRaises(FileNotFoundError, fs.Stat, '404.html/') |
74 self.assertEquals(StatInfo('0'), fs.Stat('404.html')) | 74 self.assertEquals(StatInfo('0'), fs.Stat('404.html')) |
75 self.assertEquals(StatInfo('0', child_versions={ | 75 self.assertEquals(StatInfo('0', child_versions={ |
76 'activeTab.html': '0', | 76 'activeTab.html': '0', |
77 'alarms.html': '0', | 77 'alarms.html': '0', |
78 }), fs.Stat('extensions/')) | 78 }), fs.Stat('extensions/')) |
| 79 |
79 fs.IncrementStat() | 80 fs.IncrementStat() |
80 self.assertEquals(StatInfo('1'), fs.Stat('404.html')) | 81 self.assertEquals(StatInfo('1'), fs.Stat('404.html')) |
81 self.assertEquals(StatInfo('1', child_versions={ | 82 self.assertEquals(StatInfo('1', child_versions={ |
82 'activeTab.html': '1', | 83 'activeTab.html': '1', |
83 'alarms.html': '1', | 84 'alarms.html': '1', |
84 }), fs.Stat('extensions/')) | 85 }), fs.Stat('extensions/')) |
85 | 86 |
| 87 fs.IncrementStat(path='404.html') |
| 88 self.assertEquals(StatInfo('2'), fs.Stat('404.html')) |
| 89 self.assertEquals(StatInfo('1', child_versions={ |
| 90 'activeTab.html': '1', |
| 91 'alarms.html': '1', |
| 92 }), fs.Stat('extensions/')) |
| 93 |
| 94 fs.IncrementStat() |
| 95 self.assertEquals(StatInfo('3'), fs.Stat('404.html')) |
| 96 self.assertEquals(StatInfo('2', child_versions={ |
| 97 'activeTab.html': '2', |
| 98 'alarms.html': '2', |
| 99 }), fs.Stat('extensions/')) |
| 100 |
| 101 fs.IncrementStat(path='extensions/') |
| 102 self.assertEquals(StatInfo('3'), fs.Stat('404.html')) |
| 103 self.assertEquals(StatInfo('3', child_versions={ |
| 104 'activeTab.html': '2', |
| 105 'alarms.html': '2', |
| 106 }), fs.Stat('extensions/')) |
| 107 |
| 108 fs.IncrementStat(path='extensions/alarms.html') |
| 109 self.assertEquals(StatInfo('3'), fs.Stat('404.html')) |
| 110 self.assertEquals(StatInfo('3', child_versions={ |
| 111 'activeTab.html': '2', |
| 112 'alarms.html': '3', |
| 113 }), fs.Stat('extensions/')) |
| 114 |
| 115 def testCheckAndReset(self): |
| 116 fs = TestFileSystem(deepcopy(_TEST_DATA)) |
| 117 |
| 118 self.assertTrue(fs.CheckAndReset()) |
| 119 self.assertFalse(fs.CheckAndReset(read_count=1)) |
| 120 self.assertFalse(fs.CheckAndReset(stat_count=1)) |
| 121 |
| 122 fs.ReadSingle('apps/') |
| 123 self.assertTrue(fs.CheckAndReset(read_count=1)) |
| 124 self.assertFalse(fs.CheckAndReset(read_count=1)) |
| 125 self.assertTrue(fs.CheckAndReset()) |
| 126 |
| 127 fs.ReadSingle('apps/') |
| 128 self.assertFalse(fs.CheckAndReset(read_count=2)) |
| 129 |
| 130 fs.ReadSingle('extensions/') |
| 131 fs.ReadSingle('extensions/') |
| 132 self.assertTrue(fs.CheckAndReset(read_count=2)) |
| 133 self.assertFalse(fs.CheckAndReset(read_count=2)) |
| 134 self.assertTrue(fs.CheckAndReset()) |
| 135 |
| 136 fs.ReadSingle('404.html') |
| 137 fs.Read(['notfound.html', 'apps/']) |
| 138 self.assertTrue(fs.CheckAndReset(read_count=2)) |
| 139 |
| 140 fs.Stat('404.html') |
| 141 fs.Stat('404.html') |
| 142 fs.Stat('apps/') |
| 143 self.assertFalse(fs.CheckAndReset(stat_count=42)) |
| 144 self.assertFalse(fs.CheckAndReset(stat_count=42)) |
| 145 self.assertTrue(fs.CheckAndReset()) |
| 146 |
| 147 fs.ReadSingle('404.html') |
| 148 fs.Stat('404.html') |
| 149 fs.Stat('apps/') |
| 150 self.assertTrue(fs.CheckAndReset(read_count=1, stat_count=2)) |
| 151 self.assertTrue(fs.CheckAndReset()) |
| 152 |
| 153 def testMoveTo(self): |
| 154 self.assertEqual({'foo': {'a': 'b', 'c': 'd'}}, |
| 155 TestFileSystem.MoveTo('foo', {'a': 'b', 'c': 'd'})) |
| 156 self.assertEqual({'foo': {'bar': {'a': 'b', 'c': 'd'}}}, |
| 157 TestFileSystem.MoveTo('foo/bar', {'a': 'b', 'c': 'd'})) |
| 158 self.assertEqual({'foo': {'bar': {'baz': {'a': 'b'}}}}, |
| 159 TestFileSystem.MoveTo('foo/bar/baz', {'a': 'b'})) |
| 160 |
86 if __name__ == '__main__': | 161 if __name__ == '__main__': |
87 unittest.main() | 162 unittest.main() |
OLD | NEW |