tests/test-atomictempfile.py
changeset 18666 fb9d1c2805ff
parent 15057 774da7121fc9
child 29188 f00f1de16454
equal deleted inserted replaced
18665:2cbfb8c497ee 18666:fb9d1c2805ff
     1 import os
     1 import os
     2 import glob
     2 import glob
       
     3 import unittest
       
     4 import silenttestrunner
       
     5 
     3 from mercurial.util import atomictempfile
     6 from mercurial.util import atomictempfile
     4 
     7 
     5 # basic usage
     8 class testatomictempfile(unittest.TestCase):
     6 def test1_simple():
     9     def test1_simple(self):
     7     if os.path.exists('foo'):
    10         if os.path.exists('foo'):
     8         os.remove('foo')
    11             os.remove('foo')
     9     file = atomictempfile('foo')
    12         file = atomictempfile('foo')
    10     (dir, basename) = os.path.split(file._tempname)
    13         (dir, basename) = os.path.split(file._tempname)
    11     assert not os.path.isfile('foo')
    14         self.assertFalse(os.path.isfile('foo'))
    12     assert basename in glob.glob('.foo-*')
    15         self.assertTrue(basename in glob.glob('.foo-*'))
    13 
    16 
    14     file.write('argh\n')
    17         file.write('argh\n')
    15     file.close()
    18         file.close()
    16 
    19 
    17     assert os.path.isfile('foo')
    20         self.assertTrue(os.path.isfile('foo'))
    18     assert basename not in glob.glob('.foo-*')
    21         self.assertTrue(basename not in glob.glob('.foo-*'))
    19     print 'OK'
       
    20 
    22 
    21 # discard() removes the temp file without making the write permanent
    23     # discard() removes the temp file without making the write permanent
    22 def test2_discard():
    24     def test2_discard(self):
    23     if os.path.exists('foo'):
    25         if os.path.exists('foo'):
    24         os.remove('foo')
    26             os.remove('foo')
    25     file = atomictempfile('foo')
    27         file = atomictempfile('foo')
    26     (dir, basename) = os.path.split(file._tempname)
    28         (dir, basename) = os.path.split(file._tempname)
    27 
    29 
    28     file.write('yo\n')
    30         file.write('yo\n')
    29     file.discard()
    31         file.discard()
    30 
    32 
    31     assert not os.path.isfile('foo')
    33         self.assertFalse(os.path.isfile('foo'))
    32     assert basename not in os.listdir('.')
    34         self.assertTrue(basename not in os.listdir('.'))
    33     print 'OK'
       
    34 
    35 
    35 # if a programmer screws up and passes bad args to atomictempfile, they
    36     # if a programmer screws up and passes bad args to atomictempfile, they
    36 # get a plain ordinary TypeError, not infinite recursion
    37     # get a plain ordinary TypeError, not infinite recursion
    37 def test3_oops():
    38     def test3_oops(self):
    38     try:
    39         self.assertRaises(TypeError, atomictempfile)
    39         file = atomictempfile()
       
    40     except TypeError:
       
    41         print "OK"
       
    42     else:
       
    43         print "expected TypeError"
       
    44 
    40 
    45 if __name__ == '__main__':
    41 if __name__ == '__main__':
    46     test1_simple()
    42     silenttestrunner.main(__name__)
    47     test2_discard()
       
    48     test3_oops()