Debian Codegraph @

tests/test-atomictempfile.py

raw
1 import glob
2 import os
3 import shutil
4 import stat
5 import tempfile
6 import unittest
7
8 from mercurial import (
9 util,
10 )
11
12 atomictempfile = util.atomictempfile
13
14
15 class testatomictempfile(unittest.TestCase):
16 def setUp(self):
17 self._testdir = tempfile.mkdtemp(b'atomictempfiletest')
18 self._filename = os.path.join(self._testdir, b'testfilename')
19
20 def tearDown(self):
21 shutil.rmtree(self._testdir, True)
22
23 def testsimple(self):
24 file = atomictempfile(self._filename)
25 self.assertFalse(os.path.isfile(self._filename))
26 tempfilename = file._tempname
27 self.assertTrue(
28 tempfilename
29 in glob.glob(os.path.join(self._testdir, b'.testfilename-*'))
30 )
31
32 file.write(b'argh\n')
33 file.close()
34
35 self.assertTrue(os.path.isfile(self._filename))
36 self.assertTrue(
37 tempfilename
38 not in glob.glob(os.path.join(self._testdir, b'.testfilename-*'))
39 )
40
41 # discard() removes the temp file without making the write permanent
42 def testdiscard(self):
43 file = atomictempfile(self._filename)
44 (dir, basename) = os.path.split(file._tempname)
45
46 file.write(b'yo\n')
47 file.discard()
48
49 self.assertFalse(os.path.isfile(self._filename))
50 self.assertTrue(basename not in os.listdir(b'.'))
51
52 # if a programmer screws up and passes bad args to atomictempfile, they
53 # get a plain ordinary TypeError, not infinite recursion
54 def testoops(self):
55 with self.assertRaises(TypeError):
56 atomictempfile()
57
58 # checkambig=True avoids ambiguity of timestamp
59 def testcheckambig(self):
60 def atomicwrite(checkambig):
61 f = atomictempfile(self._filename, checkambig=checkambig)
62 f.write(b'FOO')
63 f.close()
64
65 # try some times, because reproduction of ambiguity depends on
66 # "filesystem time"
67 for i in range(5):
68 atomicwrite(False)
69 oldstat = os.stat(self._filename)
70 if oldstat[stat.ST_CTIME] != oldstat[stat.ST_MTIME]:
71 # subsequent changing never causes ambiguity
72 continue
73
74 repetition = 3
75
76 # repeat atomic write with checkambig=True, to examine
77 # whether st_mtime is advanced multiple times as expected
78 for j in range(repetition):
79 atomicwrite(True)
80 newstat = os.stat(self._filename)
81 if oldstat[stat.ST_CTIME] != newstat[stat.ST_CTIME]:
82 # timestamp ambiguity was naturally avoided while repetition
83 continue
84
85 # st_mtime should be advanced "repetition" times, because
86 # all atomicwrite() occurred at same time (in sec)
87 oldtime = (oldstat[stat.ST_MTIME] + repetition) & 0x7FFFFFFF
88 self.assertTrue(newstat[stat.ST_MTIME] == oldtime)
89 # no more examination is needed, if assumption above is true
90 break
91 else:
92 # This platform seems too slow to examine anti-ambiguity
93 # of file timestamp (or test happened to be executed at
94 # bad timing). Exit silently in this case, because running
95 # on other faster platforms can detect problems
96 pass
97
98 def testread(self):
99 with open(self._filename, 'wb') as f:
100 f.write(b'foobar\n')
101 file = atomictempfile(self._filename, mode=b'rb')
102 self.assertTrue(file.read(), b'foobar\n')
103 file.discard()
104
105 def testcontextmanagersuccess(self):
106 """When the context closes, the file is closed"""
107 with atomictempfile(b'foo') as f:
108 self.assertFalse(os.path.isfile(b'foo'))
109 f.write(b'argh\n')
110 self.assertTrue(os.path.isfile(b'foo'))
111
112 def testcontextmanagerfailure(self):
113 """On exception, the file is discarded"""
114 try:
115 with atomictempfile(b'foo') as f:
116 self.assertFalse(os.path.isfile(b'foo'))
117 f.write(b'argh\n')
118 raise ValueError
119 except ValueError:
120 pass
121 self.assertFalse(os.path.isfile(b'foo'))
122
123
124 if __name__ == '__main__':
125 import silenttestrunner
126
127 silenttestrunner.main(__name__)