comparison tests/mockutil.py @ 24:644869022b6e

Mock `os.path.isfile`, and fix a few other test utilities.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 19 Aug 2014 11:06:48 -0700
parents 617191dec18e
children 43091c9837bf
comparison
equal deleted inserted replaced
23:923699e816d0 24:644869022b6e
127 def _startMock(self): 127 def _startMock(self):
128 self._createMock('__main__.open', open, self._open, create=True) 128 self._createMock('__main__.open', open, self._open, create=True)
129 self._createMock('codecs.open', codecs.open, self._codecsOpen) 129 self._createMock('codecs.open', codecs.open, self._codecsOpen)
130 self._createMock('os.listdir', os.listdir, self._listdir) 130 self._createMock('os.listdir', os.listdir, self._listdir)
131 self._createMock('os.path.isdir', os.path.isdir, self._isdir) 131 self._createMock('os.path.isdir', os.path.isdir, self._isdir)
132 self._createMock('os.path.isfile', os.path.isfile, self._isfile)
132 self._createMock('os.path.islink', os.path.islink, self._islink) 133 self._createMock('os.path.islink', os.path.islink, self._islink)
133 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime) 134 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime)
134 for p in self._patchers: 135 for p in self._patchers:
135 p.start() 136 p.start()
136 137
147 if path.startswith(resources_path): 148 if path.startswith(resources_path):
148 return self._originals['__main__.open'](path, **kwargs) 149 return self._originals['__main__.open'](path, **kwargs)
149 e = self._getFsEntry(path) 150 e = self._getFsEntry(path)
150 if e is None: 151 if e is None:
151 raise OSError("No such file: %s" % path) 152 raise OSError("No such file: %s" % path)
153 if not isinstance(e, tuple):
154 raise OSError("'%s' is not a file" % path)
152 return io.StringIO(e[0]) 155 return io.StringIO(e[0])
153 156
154 def _codecsOpen(self, path, *args, **kwargs): 157 def _codecsOpen(self, path, *args, **kwargs):
155 path = os.path.normpath(path) 158 path = os.path.normpath(path)
156 if path.startswith(resources_path): 159 if path.startswith(resources_path):
157 return self._originals['codecs.open'](path, *args, **kwargs) 160 return self._originals['codecs.open'](path, *args, **kwargs)
158 e = self._getFsEntry(path) 161 e = self._getFsEntry(path)
159 if e is None: 162 if e is None:
160 raise OSError("No such file: %s" % path) 163 raise OSError("No such file: %s" % path)
164 if not isinstance(e, tuple):
165 raise OSError("'%s' is not a file" % path)
161 return io.StringIO(e[0]) 166 return io.StringIO(e[0])
162 167
163 def _listdir(self, path): 168 def _listdir(self, path):
164 if not path.startswith('/' + self._root): 169 if not path.startswith('/' + self._root):
165 return self._originals['os.listdir'](path) 170 return self._originals['os.listdir'](path)
173 def _isdir(self, path): 178 def _isdir(self, path):
174 if not path.startswith('/' + self._root): 179 if not path.startswith('/' + self._root):
175 return self._originals['os.path.isdir'](path) 180 return self._originals['os.path.isdir'](path)
176 e = self._getFsEntry(path) 181 e = self._getFsEntry(path)
177 return e is not None and isinstance(e, dict) 182 return e is not None and isinstance(e, dict)
183
184 def _isfile(self, path):
185 if not path.startswith('/' + self._root):
186 return self._originals['os.path.isfile'](path)
187 e = self._getFsEntry(path)
188 return e is not None and isinstance(e, tuple)
178 189
179 def _islink(self, path): 190 def _islink(self, path):
180 if not path.startswith('/' + self._root): 191 if not path.startswith('/' + self._root):
181 return self._originals['os.path.islink'](path) 192 return self._originals['os.path.islink'](path)
182 return False 193 return False