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.
6 from copy
import deepcopy
9 from file_system
import FileNotFoundError
, StatInfo
10 from patched_file_system
import PatchedFileSystem
11 from test_file_system
import TestFileSystem
12 from test_patcher
import TestPatcher
16 'file1.html': 'This is dir1/file1.html',
24 'sub1.txt': 'in subdir(1)',
25 'sub2.txt': 'in subdir(2)',
26 'sub3.txt': 'in subdir(3)',
43 _TEST_PATCH_VERSION
= '1001'
49 'dir1/newsubdir/a.js',
55 'dir2/subdir1/sub1.txt',
61 'dir2/subdir1/sub2.txt',
65 'test3.txt': 'test3 is added.',
66 'dir1/file2.html': 'This is dir1/file2.html',
67 'dir1/newsubdir/a.js': 'This is a.js',
68 'newdir/1.html': 'This comes from a new dir.',
69 'dir2/subdir1/sub2.txt': 'in subdir',
72 class PatchedFileSystemTest(unittest
.TestCase
):
74 self
._patcher
= TestPatcher(_TEST_PATCH_VERSION
,
77 self
._host
_file
_system
= TestFileSystem(_TEST_FS_DATA
)
78 self
._file
_system
= PatchedFileSystem(self
._host
_file
_system
,
82 expected
= deepcopy(_TEST_PATCH_DATA
)
83 # Files that are not modified.
85 'dir2/subdir1/sub3.txt': 'in subdir(3)',
86 'dir1/file1.html': 'This is dir1/file1.html',
90 self
.assertEqual(expected
[key
], self
._file
_system
.ReadSingle(key
).Get())
94 self
._file
_system
.Read(expected
.keys()).Get())
96 self
.assertRaises(FileNotFoundError
,
97 self
._file
_system
.ReadSingle('test2.txt').Get
)
98 self
.assertRaises(FileNotFoundError
,
99 self
._file
_system
.ReadSingle('dir2/subdir1/sub1.txt').Get
)
100 self
.assertRaises(FileNotFoundError
,
101 self
._file
_system
.ReadSingle('not_existing').Get
)
102 self
.assertRaises(FileNotFoundError
,
103 self
._file
_system
.ReadSingle('dir1/not_existing').Get
)
106 self
._file
_system
.ReadSingle('dir1/newsubdir/not_existing').Get
)
108 def testReadDir(self
):
110 sorted(self
._file
_system
.ReadSingle('dir1/').Get()),
111 sorted(set(self
._host
_file
_system
.ReadSingle('dir1/').Get()) |
112 set(('file2.html', 'newsubdir/'))))
115 sorted(self
._file
_system
.ReadSingle('dir1/newsubdir/').Get()),
118 self
.assertEqual(sorted(self
._file
_system
.ReadSingle('dir2/').Get()),
119 sorted(self
._host
_file
_system
.ReadSingle('dir2/').Get()))
122 sorted(self
._file
_system
.ReadSingle('dir2/subdir1/').Get()),
123 sorted(set(self
._host
_file
_system
.ReadSingle('dir2/subdir1/').Get()) -
126 self
.assertEqual(sorted(self
._file
_system
.ReadSingle('newdir/').Get()),
129 self
.assertEqual(self
._file
_system
.ReadSingle('dir3/').Get(), [])
131 self
.assertEqual(self
._file
_system
.ReadSingle('dir4/').Get(), [])
133 self
.assertRaises(FileNotFoundError
,
134 self
._file
_system
.ReadSingle('not_existing_dir/').Get
)
137 version
= 'patched_%s' % self
._patcher
.GetVersion()
138 old_version
= self
._host
_file
_system
.Stat('dir1/file1.html').version
140 # Stat an unmodified file.
141 self
.assertEqual(self
._file
_system
.Stat('dir1/file1.html'),
142 self
._host
_file
_system
.Stat('dir1/file1.html'))
144 # Stat an unmodified directory.
145 self
.assertEqual(self
._file
_system
.Stat('dir1/unmodified/'),
146 self
._host
_file
_system
.Stat('dir1/unmodified/'))
148 # Stat a modified directory.
149 self
.assertEqual(self
._file
_system
.Stat('dir2/'),
150 StatInfo(version
, {'subdir1/': version
}))
151 self
.assertEqual(self
._file
_system
.Stat('dir2/subdir1/'),
152 StatInfo(version
, {'sub2.txt': version
,
153 'sub3.txt': old_version
}))
155 # Stat a modified directory with new files.
156 expected
= self
._host
_file
_system
.Stat('dir1/')
157 expected
.version
= version
158 expected
.child_versions
.update({'file2.html': version
,
159 'newsubdir/': version
})
160 self
.assertEqual(self
._file
_system
.Stat('dir1/'),
163 # Stat an added directory.
164 self
.assertEqual(self
._file
_system
.Stat('dir1/newsubdir/'),
165 StatInfo(version
, {'a.js': version
}))
166 self
.assertEqual(self
._file
_system
.Stat('dir1/newsubdir/a.js'),
168 self
.assertEqual(self
._file
_system
.Stat('newdir/'),
169 StatInfo(version
, {'1.html': version
}))
170 self
.assertEqual(self
._file
_system
.Stat('newdir/1.html'),
173 # Stat files removed in the patch.
174 self
.assertRaises(FileNotFoundError
, self
._file
_system
.Stat
,
175 'dir2/subdir1/sub1.txt')
176 self
.assertRaises(FileNotFoundError
, self
._file
_system
.Stat
,
179 # Stat empty directories.
180 self
.assertEqual(self
._file
_system
.Stat('dir3/'),
181 StatInfo(old_version
, {}))
182 self
.assertEqual(self
._file
_system
.Stat('dir4/'),
183 StatInfo(version
, {}))
184 self
.assertEqual(self
._file
_system
.Stat('dir5/subdir/'),
185 StatInfo(version
, {}))
187 # Stat empty (after patch) directory's parent
188 self
.assertEqual(self
._file
_system
.Stat('dir5/'),
189 StatInfo(version
, {'subdir/': version
}))
191 # Stat files that don't exist either before or after patching.
192 self
.assertRaises(FileNotFoundError
, self
._file
_system
.Stat
,
194 self
.assertRaises(FileNotFoundError
, self
._file
_system
.Stat
,
195 'dir1/not_existing/')
196 self
.assertRaises(FileNotFoundError
, self
._file
_system
.Stat
,
199 if __name__
== '__main__':