| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 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. |
| 5 |
| 6 import os |
| 7 import sys |
| 8 import unittest |
| 9 from appengine_url_fetcher import AppEngineUrlFetcher |
| 10 from appengine_wrappers import files |
| 11 from fake_fetchers import ConfigureFakeFetchers |
| 12 from fake_url_fetcher import FakeUrlFetcher, _Response |
| 13 from file_system import FileNotFoundError, StatInfo |
| 14 from future import Future |
| 15 from object_store_creator import ObjectStoreCreator |
| 16 from patched_file_system import PatchedFileSystem |
| 17 from patcher import Patcher |
| 18 from subversion_file_system import SubversionFileSystem |
| 19 import url_constants |
| 20 |
| 21 class TestPatcher(Patcher): |
| 22 def GetVersion(self): |
| 23 return '1' |
| 24 |
| 25 def GetPatchedFiles(self): |
| 26 return ( |
| 27 # Added files |
| 28 [ |
| 29 'test4.txt', |
| 30 'list/file7.html', |
| 31 'newdir/sub/1.html', |
| 32 ], |
| 33 # Deleted files |
| 34 [ |
| 35 'test3.txt', |
| 36 'list/file1.html', |
| 37 # These should be silently ignored instead of errors. |
| 38 'not_existing_foo', |
| 39 'list/not_existing_foo', |
| 40 ], |
| 41 # Modified files |
| 42 [ |
| 43 'test1.txt', |
| 44 'test2.txt', |
| 45 ]) |
| 46 |
| 47 def Apply(self, paths, file_system, binary): |
| 48 patch_data = { |
| 49 'test1.txt': 'Test1 is patched.\n', |
| 50 'test2.txt': 'Test2 is also patched.\n', |
| 51 'test4.txt': 'Test4 is added.\n', |
| 52 'list/file6.html': '', |
| 53 'newdir/sub/1.html': '', |
| 54 } |
| 55 |
| 56 value = {} |
| 57 for path in paths: |
| 58 if patch_data.get(path) is not None: |
| 59 value[path] = patch_data[path] |
| 60 else: |
| 61 raise FileNotFoundError('%s is deleted in the patch.' % path) |
| 62 return Future(value=value) |
| 63 |
| 64 class FakeStatFetcher(FakeUrlFetcher): |
| 65 def Fetch(self, url): |
| 66 assert url.endswith('/') |
| 67 try: |
| 68 return FakeUrlFetcher.Fetch(self, url + 'stat') |
| 69 except IOError: |
| 70 # FakeUrlFetcher throws IOError when the file was not found. But |
| 71 # ViewVC server returns 404 in this case. |
| 72 result = _Response() |
| 73 result.status_code = 404 |
| 74 return result |
| 75 |
| 76 class PatchedFileSystemTest(unittest.TestCase): |
| 77 def setUp(self): |
| 78 ConfigureFakeFetchers(os.path.join(sys.path[0], os.pardir)) |
| 79 fetcher = FakeUrlFetcher(os.path.join(sys.path[0], |
| 80 'test_data', |
| 81 'file_system')) |
| 82 stat_fetcher = FakeStatFetcher(os.path.join(sys.path[0], |
| 83 'test_data', |
| 84 'file_system_stat')) |
| 85 self._svn_file_system = SubversionFileSystem(fetcher, stat_fetcher) |
| 86 self._patcher = TestPatcher() |
| 87 self._file_system = PatchedFileSystem(self._svn_file_system, |
| 88 self._patcher) |
| 89 |
| 90 def testRead(self): |
| 91 expected = { |
| 92 'test1.txt': 'Test1 is patched.\n', |
| 93 'test2.txt': 'Test2 is also patched.\n', |
| 94 'test4.txt': 'Test4 is added.\n', |
| 95 'newdir/sub/1.html': '', |
| 96 } |
| 97 self.assertEqual( |
| 98 expected, |
| 99 self._file_system.Read(expected.keys()).Get()) |
| 100 |
| 101 self.assertRaises(FileNotFoundError, self._file_system.ReadSingle, |
| 102 'test3.txt') |
| 103 |
| 104 def testReadDir(self): |
| 105 self.assertEqual(sorted(self._file_system.ReadSingle('list/')), |
| 106 sorted(set(self._svn_file_system.ReadSingle('list/')) | |
| 107 {'file7.html'} - {'file1.html'})) |
| 108 |
| 109 def testStat(self): |
| 110 version = 'patched_%s' % self._patcher.GetVersion() |
| 111 |
| 112 # Stat an unmodified file. |
| 113 self.assertEqual(self._file_system.Stat('list/file6.html').version, |
| 114 '147471') |
| 115 |
| 116 # Stat an unmodified directory. |
| 117 self.assertEqual(self._file_system.Stat('list/dir/'), |
| 118 self._svn_file_system.Stat('list/dir/')) |
| 119 |
| 120 # Stat a modified directory. |
| 121 list_stat = self._svn_file_system.Stat('list/') |
| 122 list_stat.version = version |
| 123 list_stat.child_versions.update({'file7.html': version}) |
| 124 del list_stat.child_versions['file1.html'] |
| 125 self.assertEqual(self._file_system.Stat('list/'), list_stat) |
| 126 |
| 127 # Stat an added directory. |
| 128 self.assertEqual(self._file_system.Stat('newdir/'), |
| 129 StatInfo(version, { |
| 130 'sub/': version, |
| 131 })) |
| 132 |
| 133 # Stat files removed in the patch. |
| 134 self.assertRaises(FileNotFoundError,self._file_system.Stat, |
| 135 'list/file1.html') |
| 136 |
| 137 # Stat files that don't exist in either before or after patching. |
| 138 self.assertRaises(FileNotFoundError, self._file_system.Stat, |
| 139 'list/not_existing_file') |
| 140 self.assertRaises(FileNotFoundError, self._file_system.Stat, |
| 141 'not_existing_dir/') |
| 142 |
| 143 if __name__ == '__main__': |
| 144 unittest.main() |
| OLD | NEW |