Mercurial > hg > mercurial-source
diff tests/test-atomictempfile.py @ 30155:6d96658a22b0
atomictempfile: add context manager support
Close the file (moving it in place) on clean context exit, discard when there
has been an exception.
author | Martijn Pieters <mjpieters@fb.com> |
---|---|
date | Thu, 23 Jun 2016 18:21:25 +0100 |
parents | 50269a4dce61 |
children | 318a24b52eeb |
line wrap: on
line diff
--- a/tests/test-atomictempfile.py +++ b/tests/test-atomictempfile.py @@ -96,6 +96,24 @@ self.assertTrue(file.read(), b'foobar\n') file.discard() + def testcontextmanagersuccess(self): + """When the context closes, the file is closed""" + with atomictempfile('foo') as f: + self.assertFalse(os.path.isfile('foo')) + f.write(b'argh\n') + self.assertTrue(os.path.isfile('foo')) + + def testcontextmanagerfailure(self): + """On exception, the file is discarded""" + try: + with atomictempfile('foo') as f: + self.assertFalse(os.path.isfile('foo')) + f.write(b'argh\n') + raise ValueError + except ValueError: + pass + self.assertFalse(os.path.isfile('foo')) + if __name__ == '__main__': import silenttestrunner silenttestrunner.main(__name__)