tests/test-atomictempfile.py
changeset 15057 774da7121fc9
parent 14007 d764463b433e
child 18666 fb9d1c2805ff
equal deleted inserted replaced
15056:8413916df816 15057:774da7121fc9
    10     (dir, basename) = os.path.split(file._tempname)
    10     (dir, basename) = os.path.split(file._tempname)
    11     assert not os.path.isfile('foo')
    11     assert not os.path.isfile('foo')
    12     assert basename in glob.glob('.foo-*')
    12     assert basename in glob.glob('.foo-*')
    13 
    13 
    14     file.write('argh\n')
    14     file.write('argh\n')
    15     file.rename()
    15     file.close()
    16 
    16 
    17     assert os.path.isfile('foo')
    17     assert os.path.isfile('foo')
    18     assert basename not in glob.glob('.foo-*')
    18     assert basename not in glob.glob('.foo-*')
    19     print 'OK'
    19     print 'OK'
    20 
    20 
    21 # close() removes the temp file but does not make the write
    21 # discard() removes the temp file without making the write permanent
    22 # permanent -- essentially discards your work (WTF?!)
    22 def test2_discard():
    23 def test2_close():
       
    24     if os.path.exists('foo'):
    23     if os.path.exists('foo'):
    25         os.remove('foo')
    24         os.remove('foo')
    26     file = atomictempfile('foo')
    25     file = atomictempfile('foo')
    27     (dir, basename) = os.path.split(file._tempname)
    26     (dir, basename) = os.path.split(file._tempname)
    28 
    27 
    29     file.write('yo\n')
    28     file.write('yo\n')
    30     file.close()
    29     file.discard()
    31 
    30 
    32     assert not os.path.isfile('foo')
    31     assert not os.path.isfile('foo')
    33     assert basename not in os.listdir('.')
    32     assert basename not in os.listdir('.')
    34     print 'OK'
    33     print 'OK'
    35 
    34 
    43     else:
    42     else:
    44         print "expected TypeError"
    43         print "expected TypeError"
    45 
    44 
    46 if __name__ == '__main__':
    45 if __name__ == '__main__':
    47     test1_simple()
    46     test1_simple()
    48     test2_close()
    47     test2_discard()
    49     test3_oops()
    48     test3_oops()