annotate tests/test-atomictempfile.py @ 33202:c1994c986d77 stable

Added signature for changeset 26c49ed51a69
author Augie Fackler <raf@durin42.com>
date Wed, 05 Jul 2017 11:24:22 -0400
parents 318a24b52eeb
children 68c43a416585
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
1 from __future__ import absolute_import
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
2
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
3 import glob
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
4 import os
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
5 import shutil
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
6 import tempfile
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
7 import unittest
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
8
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
9 from mercurial import (
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
10 util,
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
11 )
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
12 atomictempfile = util.atomictempfile
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
13
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
14 class testatomictempfile(unittest.TestCase):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
15 def setUp(self):
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
16 self._testdir = tempfile.mkdtemp('atomictempfiletest')
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
17 self._filename = os.path.join(self._testdir, 'testfilename')
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
18
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
19 def tearDown(self):
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
20 shutil.rmtree(self._testdir, True)
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
21
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
22 def testsimple(self):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
23 file = atomictempfile(self._filename)
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
24 self.assertFalse(os.path.isfile(self._filename))
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
25 tempfilename = file._tempname
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
26 self.assertTrue(tempfilename in glob.glob(
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
27 os.path.join(self._testdir, '.testfilename-*')))
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
28
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
29 file.write(b'argh\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
30 file.close()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
31
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
32 self.assertTrue(os.path.isfile(self._filename))
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
33 self.assertTrue(tempfilename not in glob.glob(
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
34 os.path.join(self._testdir, '.testfilename-*')))
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
35
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
36 # discard() removes the temp file without making the write permanent
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
37 def testdiscard(self):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
38 file = atomictempfile(self._filename)
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
39 (dir, basename) = os.path.split(file._tempname)
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
40
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
41 file.write(b'yo\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
42 file.discard()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
43
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
44 self.assertFalse(os.path.isfile(self._filename))
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
45 self.assertTrue(basename not in os.listdir('.'))
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
46
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
47 # if a programmer screws up and passes bad args to atomictempfile, they
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
48 # get a plain ordinary TypeError, not infinite recursion
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
49 def testoops(self):
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
50 self.assertRaises(TypeError, atomictempfile)
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
51
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
52 # checkambig=True avoids ambiguity of timestamp
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
53 def testcheckambig(self):
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
54 def atomicwrite(checkambig):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
55 f = atomictempfile(self._filename, checkambig=checkambig)
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
56 f.write('FOO')
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
57 f.close()
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
58
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
59 # try some times, because reproduction of ambiguity depends on
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
60 # "filesystem time"
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
61 for i in xrange(5):
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
62 atomicwrite(False)
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
63 oldstat = os.stat(self._filename)
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
64 if oldstat.st_ctime != oldstat.st_mtime:
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
65 # subsequent changing never causes ambiguity
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
66 continue
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
67
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
68 repetition = 3
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
69
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
70 # repeat atomic write with checkambig=True, to examine
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 29394
diff changeset
71 # whether st_mtime is advanced multiple times as expected
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
72 for j in xrange(repetition):
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
73 atomicwrite(True)
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
74 newstat = os.stat(self._filename)
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
75 if oldstat.st_ctime != newstat.st_ctime:
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
76 # timestamp ambiguity was naturally avoided while repetition
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
77 continue
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
78
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
79 # st_mtime should be advanced "repetition" times, because
30332
318a24b52eeb spelling: fixes of non-dictionary words
Mads Kiilerich <madski@unity3d.com>
parents: 29394
diff changeset
80 # all atomicwrite() occurred at same time (in sec)
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
81 self.assertTrue(newstat.st_mtime ==
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
82 ((oldstat.st_mtime + repetition) & 0x7fffffff))
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
83 # no more examination is needed, if assumption above is true
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
84 break
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
85 else:
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
86 # This platform seems too slow to examine anti-ambiguity
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
87 # of file timestamp (or test happened to be executed at
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
88 # bad timing). Exit silently in this case, because running
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
89 # on other faster platforms can detect problems
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
90 pass
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
91
29393
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
92 def testread(self):
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
93 with open(self._filename, 'wb') as f:
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
94 f.write(b'foobar\n')
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
95 file = atomictempfile(self._filename, mode='rb')
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
96 self.assertTrue(file.read(), b'foobar\n')
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
97 file.discard()
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
98
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
99 def testcontextmanagersuccess(self):
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
100 """When the context closes, the file is closed"""
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
101 with atomictempfile('foo') as f:
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
102 self.assertFalse(os.path.isfile('foo'))
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
103 f.write(b'argh\n')
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
104 self.assertTrue(os.path.isfile('foo'))
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
105
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
106 def testcontextmanagerfailure(self):
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
107 """On exception, the file is discarded"""
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
108 try:
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
109 with atomictempfile('foo') as f:
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
110 self.assertFalse(os.path.isfile('foo'))
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
111 f.write(b'argh\n')
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
112 raise ValueError
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
113 except ValueError:
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
114 pass
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
115 self.assertFalse(os.path.isfile('foo'))
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
116
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
117 if __name__ == '__main__':
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
118 import silenttestrunner
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
119 silenttestrunner.main(__name__)