comparison tests/mockutil.py @ 120:133845647083

Better error management and removal support in baking/processing. * Baker and processor pipeline now store errors in their records. * They also support deleting output files that are no longer valid. * The basic transitional record class implements more boilerplate code. * The processor pipeline is run from the `bake` command directly. * New unit tests. * Unit test mocking now mocks `os.remove` too.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 09 Nov 2014 14:46:23 -0800
parents 3471ffa059b2
children d356f6178623
comparison
equal deleted inserted replaced
119:0811f92cbdc7 120:133845647083
44 def __enter__(self): 44 def __enter__(self):
45 return self 45 return self
46 46
47 def __exit__(self, exc_type, exc_value, exc_tb): 47 def __exit__(self, exc_type, exc_value, exc_tb):
48 self._entry.contents = self._stream.getvalue() 48 self._entry.contents = self._stream.getvalue()
49 self._entry.metadata['mtime'] = time.time()
49 self._stream.close() 50 self._stream.close()
50 51
51 52
52 class mock_fs(object): 53 class mock_fs(object):
53 def __init__(self, default_spec=True): 54 def __init__(self, default_spec=True):
195 196
196 def __exit__(self, type, value, traceback): 197 def __exit__(self, type, value, traceback):
197 self._endMock() 198 self._endMock()
198 199
199 def _startMock(self): 200 def _startMock(self):
201 # TODO: sadly, there seems to be no way to replace `open` everywhere?
200 self._createMock('__main__.open', open, self._open, create=True) 202 self._createMock('__main__.open', open, self._open, create=True)
201 # TODO: WTF, apparently the previous one doesn't really work?
202 self._createMock('piecrust.records.open', open, self._open, create=True) 203 self._createMock('piecrust.records.open', open, self._open, create=True)
203 self._createMock('codecs.open', codecs.open, self._codecsOpen) 204 self._createMock('codecs.open', codecs.open, self._codecsOpen)
204 self._createMock('os.listdir', os.listdir, self._listdir) 205 self._createMock('os.listdir', os.listdir, self._listdir)
205 self._createMock('os.makedirs', os.makedirs, self._makedirs) 206 self._createMock('os.makedirs', os.makedirs, self._makedirs)
207 self._createMock('os.remove', os.remove, self._remove)
206 self._createMock('os.path.isdir', os.path.isdir, self._isdir) 208 self._createMock('os.path.isdir', os.path.isdir, self._isdir)
207 self._createMock('os.path.isfile', os.path.isfile, self._isfile) 209 self._createMock('os.path.isfile', os.path.isfile, self._isfile)
208 self._createMock('os.path.islink', os.path.islink, self._islink) 210 self._createMock('os.path.islink', os.path.islink, self._islink)
209 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime) 211 self._createMock('os.path.getmtime', os.path.getmtime, self._getmtime)
210 self._createMock('shutil.copyfile', shutil.copyfile, self._copyfile) 212 self._createMock('shutil.copyfile', shutil.copyfile, self._copyfile)
270 def _makedirs(self, path, mode=0o777): 272 def _makedirs(self, path, mode=0o777):
271 if not path.replace('\\', '/').startswith('/' + self.root): 273 if not path.replace('\\', '/').startswith('/' + self.root):
272 raise Exception("Shouldn't create directory: %s" % path) 274 raise Exception("Shouldn't create directory: %s" % path)
273 self._fs._createDir(path) 275 self._fs._createDir(path)
274 276
277 def _remove(self, path):
278 path = os.path.normpath(path)
279 self._fs._deleteEntry(path)
280
275 def _isdir(self, path): 281 def _isdir(self, path):
276 path = os.path.normpath(path) 282 path = os.path.normpath(path)
277 if path.startswith(resources_path): 283 if path.startswith(resources_path):
278 return self._originals['os.path.isdir'](path) 284 return self._originals['os.path.isdir'](path)
279 e = self._getFsEntry(path) 285 e = self._getFsEntry(path)