mirror of
https://github.com/facebook/sapling.git
synced 2024-10-09 16:31:02 +03:00
bc1dfb1ac9
The usual contract is that close() makes your writes permanent, so atomictempfile's use of close() to *discard* writes (and rename() to keep them) is rather unexpected. Thus, change it so close() makes things permanent and add a new discard() method to throw them away. discard() is only used internally, in __del__(), to ensure that writes are discarded when an atomictempfile object goes out of scope. I audited mercurial.*, hgext.*, and ~80 third-party extensions, and found no one using the existing semantics of close() to discard writes, so this should be safe.
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
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')
|
|
file.close()
|
|
|
|
assert os.path.isfile('foo')
|
|
assert basename not in glob.glob('.foo-*')
|
|
print 'OK'
|
|
|
|
# discard() removes the temp file without making the write permanent
|
|
def test2_discard():
|
|
if os.path.exists('foo'):
|
|
os.remove('foo')
|
|
file = atomictempfile('foo')
|
|
(dir, basename) = os.path.split(file._tempname)
|
|
|
|
file.write('yo\n')
|
|
file.discard()
|
|
|
|
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()
|
|
test2_discard()
|
|
test3_oops()
|