test-atomictempfile.py
42 lines
| 1.3 KiB
| text/x-python
|
PythonLexer
/ tests / test-atomictempfile.py
Greg Ward
|
r14007 | import os | ||
import glob | ||||
Idan Kamara
|
r18666 | import unittest | ||
import silenttestrunner | ||||
Greg Ward
|
r14007 | from mercurial.util import atomictempfile | ||
Idan Kamara
|
r18666 | class testatomictempfile(unittest.TestCase): | ||
def test1_simple(self): | ||||
if os.path.exists('foo'): | ||||
os.remove('foo') | ||||
file = atomictempfile('foo') | ||||
(dir, basename) = os.path.split(file._tempname) | ||||
self.assertFalse(os.path.isfile('foo')) | ||||
self.assertTrue(basename in glob.glob('.foo-*')) | ||||
Greg Ward
|
r14007 | |||
Idan Kamara
|
r18666 | file.write('argh\n') | ||
file.close() | ||||
Greg Ward
|
r14007 | |||
Idan Kamara
|
r18666 | self.assertTrue(os.path.isfile('foo')) | ||
self.assertTrue(basename not in glob.glob('.foo-*')) | ||||
Greg Ward
|
r14007 | |||
Idan Kamara
|
r18666 | # discard() removes the temp file without making the write permanent | ||
def test2_discard(self): | ||||
if os.path.exists('foo'): | ||||
os.remove('foo') | ||||
file = atomictempfile('foo') | ||||
(dir, basename) = os.path.split(file._tempname) | ||||
Greg Ward
|
r14007 | |||
Idan Kamara
|
r18666 | file.write('yo\n') | ||
file.discard() | ||||
Greg Ward
|
r14007 | |||
Idan Kamara
|
r18666 | self.assertFalse(os.path.isfile('foo')) | ||
self.assertTrue(basename not in os.listdir('.')) | ||||
# if a programmer screws up and passes bad args to atomictempfile, they | ||||
# get a plain ordinary TypeError, not infinite recursion | ||||
def test3_oops(self): | ||||
self.assertRaises(TypeError, atomictempfile) | ||||
Greg Ward
|
r14007 | |||
if __name__ == '__main__': | ||||
Idan Kamara
|
r18666 | silenttestrunner.main(__name__) | ||