Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/patched_file_system_test.py |
| =================================================================== |
| --- chrome/common/extensions/docs/server2/patched_file_system_test.py (revision 0) |
| +++ chrome/common/extensions/docs/server2/patched_file_system_test.py (revision 0) |
| @@ -0,0 +1,143 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os |
| +import sys |
| +import unittest |
| +from appengine_url_fetcher import AppEngineUrlFetcher |
| +from appengine_wrappers import files |
| +from fake_fetchers import ConfigureFakeFetchers |
| +from fake_url_fetcher import FakeUrlFetcher, _Response |
| +from file_system import FileNotFoundError, StatInfo |
| +from future import Future |
| +from object_store_creator import ObjectStoreCreator |
| +from patched_file_system import PatchedFileSystem, Patcher |
| +from subversion_file_system import SubversionFileSystem |
| +import url_constants |
| + |
| +class TestPatcher(Patcher): |
| + def GetVersion(self): |
| + return '1' |
|
not at google - send to devlin
2013/05/03 19:12:22
ditto not used
方觉(Fang Jue)
2013/05/04 02:05:41
But GetVersion must be implemented.
not at google - send to devlin
2013/05/04 06:47:43
Getting ahead of myself.
|
| + |
| + def GetPatchedFiles(self): |
| + return ( |
| + # Added files |
| + [ |
| + 'test4.txt', |
| + 'list/file7.html', |
| + 'newdir/sub/1.html', |
| + ], |
| + # Deleted files |
| + [ |
| + 'test3.txt', |
| + 'list/file1.html', |
| + # These should be silently ignored instead of errors. |
| + 'not_existing_foo', |
| + 'list/not_existing_foo', |
| + ], |
| + # Modified files |
| + [ |
| + 'test1.txt', |
| + 'test2.txt', |
| + ]) |
| + |
| + def Apply(self, paths, file_system, binary): |
| + patch_data = { |
| + 'test1.txt': 'Test1 is patched.\n', |
| + 'test2.txt': 'Test2 is also patched.\n', |
| + 'test4.txt': 'Test4 is added.\n', |
| + 'list/file6.html': '', |
| + 'newdir/sub/1.html': '', |
| + } |
| + |
| + value = {} |
| + for path in paths: |
| + if patch_data.get(path) is not None: |
| + value[path] = patch_data[path] |
| + else: |
| + raise FileNotFoundError('%s is deleted in the patch.' % path) |
| + return Future(value=value) |
| + |
| +class FakeStatFetcher(FakeUrlFetcher): |
| + def Fetch(self, url): |
| + assert url.endswith('/') |
| + try: |
| + return FakeUrlFetcher.Fetch(self, url + 'stat') |
| + except IOError: |
| + # FakeUrlFetcher throws IOError when the file was not found. But |
| + # ViewVC server returns 404 in this case. |
| + result = _Response() |
| + result.status_code = 404 |
| + return result |
| + |
| +class PatchedFileSystemTest(unittest.TestCase): |
| + def setUp(self): |
| + ConfigureFakeFetchers(os.path.join(sys.path[0], os.pardir)) |
|
not at google - send to devlin
2013/05/03 19:12:22
it should be possible to write this test using pur
|
| + fetcher = FakeUrlFetcher(os.path.join(sys.path[0], |
| + 'test_data', |
| + 'file_system')) |
| + stat_fetcher = FakeStatFetcher(os.path.join(sys.path[0], |
| + 'test_data', |
| + 'file_system_stat')) |
| + self._svn_file_system = SubversionFileSystem(fetcher, stat_fetcher) |
| + self._patcher = TestPatcher() |
| + self._file_system = PatchedFileSystem(self._svn_file_system, |
| + self._patcher) |
| + |
| + def testRead(self): |
| + expected = { |
| + 'test1.txt': 'Test1 is patched.\n', |
| + 'test2.txt': 'Test2 is also patched.\n', |
| + 'test4.txt': 'Test4 is added.\n', |
| + 'newdir/sub/1.html': '', |
| + } |
| + self.assertEqual( |
| + expected, |
| + self._file_system.Read(expected.keys()).Get()) |
| + |
| + self.assertRaises(FileNotFoundError, self._file_system.ReadSingle, |
| + 'test3.txt') |
| + |
| + def testReadDir(self): |
| + self.assertEqual(sorted(self._file_system.ReadSingle('list/')), |
| + sorted(set(self._svn_file_system.ReadSingle('list/')) | |
| + {'file7.html'} - {'file1.html'})) |
| + |
| + def testStat(self): |
| + version = 'patched_%s' % self._patcher.GetVersion() |
| + |
| + # Stat an unmodified file. |
| + self.assertEqual(self._file_system.Stat('list/file6.html').version, |
| + '147471') |
| + |
| + # Stat an unmodified directory. |
| + self.assertEqual(self._file_system.Stat('list/dir/'), |
| + self._svn_file_system.Stat('list/dir/')) |
| + |
| + # Stat a modified directory. |
| + list_stat = self._svn_file_system.Stat('list/') |
| + list_stat.version = version |
| + list_stat.child_versions.update({'file7.html': version}) |
| + del list_stat.child_versions['file1.html'] |
| + self.assertEqual(self._file_system.Stat('list/'), list_stat) |
| + |
| + # Stat an added directory. |
| + self.assertEqual(self._file_system.Stat('newdir/'), |
| + StatInfo(version, { |
| + 'sub/': version, |
| + })) |
| + |
| + # Stat files removed in the patch. |
| + self.assertRaises(FileNotFoundError,self._file_system.Stat, |
| + 'list/file1.html') |
| + |
| + # Stat files that don't exist in either before or after patching. |
| + self.assertRaises(FileNotFoundError, self._file_system.Stat, |
| + 'list/not_existing_file') |
| + self.assertRaises(FileNotFoundError, self._file_system.Stat, |
| + 'not_existing_dir/') |
| + |
| +if __name__ == '__main__': |
| + unittest.main() |
| Property changes on: chrome/common/extensions/docs/server2/patched_file_system_test.py |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |
| Added: svn:executable |
| + * |