2011-04-25 03:25:10 +04:00
|
|
|
import os
|
|
|
|
import glob
|
|
|
|
from mercurial.util import atomictempfile
|
|
|
|
|
|
|
|
# basic usage
|
|
|
|
def test1_simple():
|
|
|
|
if os.path.exists('foo'):
|
|
|
|
os.remove('foo')
|
|
|
|
file = atomictempfile('foo')
|
|
|
|
(dir, basename) = os.path.split(file._tempname)
|
|
|
|
assert not os.path.isfile('foo')
|
|
|
|
assert basename in glob.glob('.foo-*')
|
|
|
|
|
|
|
|
file.write('argh\n')
|
2011-08-26 04:21:04 +04:00
|
|
|
file.close()
|
2011-04-25 03:25:10 +04:00
|
|
|
|
|
|
|
assert os.path.isfile('foo')
|
|
|
|
assert basename not in glob.glob('.foo-*')
|
|
|
|
print 'OK'
|
|
|
|
|
2011-08-26 04:21:04 +04:00
|
|
|
# discard() removes the temp file without making the write permanent
|
|
|
|
def test2_discard():
|
2011-04-25 03:25:10 +04:00
|
|
|
if os.path.exists('foo'):
|
|
|
|
os.remove('foo')
|
|
|
|
file = atomictempfile('foo')
|
|
|
|
(dir, basename) = os.path.split(file._tempname)
|
|
|
|
|
|
|
|
file.write('yo\n')
|
2011-08-26 04:21:04 +04:00
|
|
|
file.discard()
|
2011-04-25 03:25:10 +04:00
|
|
|
|
|
|
|
assert not os.path.isfile('foo')
|
|
|
|
assert basename not in os.listdir('.')
|
|
|
|
print 'OK'
|
|
|
|
|
|
|
|
# if a programmer screws up and passes bad args to atomictempfile, they
|
|
|
|
# get a plain ordinary TypeError, not infinite recursion
|
|
|
|
def test3_oops():
|
|
|
|
try:
|
|
|
|
file = atomictempfile()
|
|
|
|
except TypeError:
|
|
|
|
print "OK"
|
|
|
|
else:
|
|
|
|
print "expected TypeError"
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test1_simple()
|
2011-08-26 04:21:04 +04:00
|
|
|
test2_discard()
|
2011-04-25 03:25:10 +04:00
|
|
|
test3_oops()
|