OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import posixpath | 5 import posixpath |
6 | 6 |
7 from file_system import FileSystem, FileNotFoundError | 7 from file_system import FileSystem, FileNotFoundError |
8 from future import Future | 8 from future import Future |
9 from test_file_system import _List, _StatTracker, TestFileSystem | 9 from test_file_system import _List, _StatTracker, TestFileSystem |
10 from path_util import IsDirectory | 10 from path_util import IsDirectory |
11 | 11 |
12 | 12 |
13 class MockFileSystem(FileSystem): | 13 class MockFileSystem(FileSystem): |
14 '''Wraps FileSystems to add a selection of mock behaviour: | 14 '''Wraps FileSystems to add a selection of mock behaviour: |
15 - asserting how often Stat/Read calls are being made to it. | 15 - asserting how often Stat/Read calls are being made to it. |
16 - primitive changes/versioning via applying object "diffs", mapping paths to | 16 - primitive changes/versioning via applying object "diffs", mapping paths to |
17 new content (similar to how TestFileSystem works). | 17 new content (similar to how TestFileSystem works). |
18 ''' | 18 ''' |
19 def __init__(self, file_system): | 19 def __init__(self, file_system): |
20 self._file_system = file_system | 20 self._file_system = file_system |
21 # Updates are stored as TestFileSystems because it already implements a | 21 # Updates are stored as TestFileSystems because it already implements a |
22 # bunch of logic to intepret paths into dictionaries. | 22 # bunch of logic to intepret paths into dictionaries. |
23 self._updates = [] | 23 self._updates = [] |
24 self._stat_tracker = _StatTracker() | 24 self._stat_tracker = _StatTracker() |
25 self._read_count = 0 | 25 self._read_count = 0 |
26 self._read_resolve_count = 0 | 26 self._read_resolve_count = 0 |
27 self._stat_count = 0 | 27 self._stat_count = 0 |
| 28 self._version = None |
28 | 29 |
29 @staticmethod | 30 @staticmethod |
30 def Create(file_system, updates): | 31 def Create(file_system, updates): |
31 mock_file_system = MockFileSystem(file_system) | 32 mock_file_system = MockFileSystem(file_system) |
32 for update in updates: | 33 for update in updates: |
33 mock_file_system.Update(update) | 34 mock_file_system.Update(update) |
34 return mock_file_system | 35 return mock_file_system |
35 | 36 |
36 # | 37 # |
37 # FileSystem implementation. | 38 # FileSystem implementation. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 stat.version = stradd(stat.version, self._stat_tracker.GetVersion(path)) | 80 stat.version = stradd(stat.version, self._stat_tracker.GetVersion(path)) |
80 if stat.child_versions: | 81 if stat.child_versions: |
81 for child_path, child_version in stat.child_versions.iteritems(): | 82 for child_path, child_version in stat.child_versions.iteritems(): |
82 stat.child_versions[child_path] = stradd( | 83 stat.child_versions[child_path] = stradd( |
83 stat.child_versions[child_path], | 84 stat.child_versions[child_path], |
84 self._stat_tracker.GetVersion(posixpath.join(path, child_path))) | 85 self._stat_tracker.GetVersion(posixpath.join(path, child_path))) |
85 | 86 |
86 return stat | 87 return stat |
87 | 88 |
88 def GetCommitID(self): | 89 def GetCommitID(self): |
89 return Future(value=self._stat_tracker.GetVersion('')) | 90 return Future(value=str(self._stat_tracker.GetVersion(''))) |
90 | 91 |
91 def GetPreviousCommitID(self): | 92 def GetPreviousCommitID(self): |
92 return Future(value=self._stat_tracker.GetVersion('') - 1) | 93 return Future(value=str(self._stat_tracker.GetVersion('') - 1)) |
93 | 94 |
94 def GetIdentity(self): | 95 def GetIdentity(self): |
95 return self._file_system.GetIdentity() | 96 return self._file_system.GetIdentity() |
96 | 97 |
| 98 def GetVersion(self): |
| 99 return self._version |
| 100 |
97 def __str__(self): | 101 def __str__(self): |
98 return repr(self) | 102 return repr(self) |
99 | 103 |
100 def __repr__(self): | 104 def __repr__(self): |
101 return 'MockFileSystem(read_count=%s, stat_count=%s, updates=%s)' % ( | 105 return 'MockFileSystem(read_count=%s, stat_count=%s, updates=%s)' % ( |
102 self._read_count, self._stat_count, len(self._updates)) | 106 self._read_count, self._stat_count, len(self._updates)) |
103 | 107 |
104 # | 108 # |
105 # Testing methods. | 109 # Testing methods. |
106 # | 110 # |
(...skipping 22 matching lines...) Expand all Loading... |
129 self._read_resolve_count = 0 | 133 self._read_resolve_count = 0 |
130 self._stat_count = 0 | 134 self._stat_count = 0 |
131 | 135 |
132 def Update(self, update): | 136 def Update(self, update): |
133 self._updates.append(TestFileSystem(update)) | 137 self._updates.append(TestFileSystem(update)) |
134 for path in _List(update).iterkeys(): | 138 for path in _List(update).iterkeys(): |
135 # Any files (not directories) which changed are now at the version | 139 # Any files (not directories) which changed are now at the version |
136 # derived from |_updates|. | 140 # derived from |_updates|. |
137 if not IsDirectory(path): | 141 if not IsDirectory(path): |
138 self._stat_tracker.SetVersion(path, len(self._updates)) | 142 self._stat_tracker.SetVersion(path, len(self._updates)) |
| 143 |
| 144 def SetVersion(self, version): |
| 145 '''Override the reported FileSystem version (default None) for testing.''' |
| 146 self._version = version |
OLD | NEW |