annotate tests/test-atomictempfile.py @ 51696:6aa74bcd4255

Try the full CI run
author Raphaël Gomès <rgomes@octobus.net>
date Thu, 18 Jul 2024 15:48:05 +0200
parents 56f98406831b
children
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 import glob
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
2 import os
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
3 import shutil
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
4 import stat
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
5 import tempfile
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
6 import unittest
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
7
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
8 from mercurial import (
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
9 util,
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
10 )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
11
29194
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
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
14
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
15 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
16 def setUp(self):
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
17 self._testdir = tempfile.mkdtemp(b'atomictempfiletest')
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
18 self._filename = os.path.join(self._testdir, b'testfilename')
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
19
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
20 def tearDown(self):
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
21 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
22
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
23 def testsimple(self):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
24 file = atomictempfile(self._filename)
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
25 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
26 tempfilename = file._tempname
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
27 self.assertTrue(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
28 tempfilename
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
29 in glob.glob(os.path.join(self._testdir, b'.testfilename-*'))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
30 )
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
31
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
32 file.write(b'argh\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
33 file.close()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
34
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
35 self.assertTrue(os.path.isfile(self._filename))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
36 self.assertTrue(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
37 tempfilename
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
38 not in glob.glob(os.path.join(self._testdir, b'.testfilename-*'))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
39 )
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
40
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
41 # 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
42 def testdiscard(self):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
43 file = atomictempfile(self._filename)
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
44 (dir, basename) = os.path.split(file._tempname)
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
45
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
46 file.write(b'yo\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
47 file.discard()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
48
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
49 self.assertFalse(os.path.isfile(self._filename))
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
50 self.assertTrue(basename not in os.listdir(b'.'))
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
51
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
52 # 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
53 # get a plain ordinary TypeError, not infinite recursion
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
54 def testoops(self):
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30332
diff changeset
55 with self.assertRaises(TypeError):
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 30332
diff changeset
56 atomictempfile()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
57
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
58 # checkambig=True avoids ambiguity of timestamp
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
59 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
60 def atomicwrite(checkambig):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
61 f = atomictempfile(self._filename, checkambig=checkambig)
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
62 f.write(b'FOO')
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
63 f.close()
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
64
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
65 # 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
66 # "filesystem time"
49285
56f98406831b py3: remove xrange() compatibility code
Manuel Jacob <me@manueljacob.de>
parents: 48875
diff changeset
67 for i in range(5):
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
68 atomicwrite(False)
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
69 oldstat = os.stat(self._filename)
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
70 if oldstat[stat.ST_CTIME] != oldstat[stat.ST_MTIME]:
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
71 # 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
72 continue
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
73
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
74 repetition = 3
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
75
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
76 # 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
77 # whether st_mtime is advanced multiple times as expected
49285
56f98406831b py3: remove xrange() compatibility code
Manuel Jacob <me@manueljacob.de>
parents: 48875
diff changeset
78 for j in range(repetition):
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
79 atomicwrite(True)
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
80 newstat = os.stat(self._filename)
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
81 if oldstat[stat.ST_CTIME] != newstat[stat.ST_CTIME]:
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
82 # 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
83 continue
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
84
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
85 # 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
86 # all atomicwrite() occurred at same time (in sec)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
87 oldtime = (oldstat[stat.ST_MTIME] + repetition) & 0x7FFFFFFF
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
88 self.assertTrue(newstat[stat.ST_MTIME] == oldtime)
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
89 # 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
90 break
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
91 else:
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
92 # 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
93 # 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
94 # 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
95 # 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
96 pass
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
97
29393
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
98 def testread(self):
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
99 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
100 f.write(b'foobar\n')
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
101 file = atomictempfile(self._filename, mode=b'rb')
29393
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
102 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
103 file.discard()
50269a4dce61 atomictempfile: add read to the supported file operations
Martijn Pieters <mjpieters@fb.com>
parents: 29392
diff changeset
104
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
105 def testcontextmanagersuccess(self):
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
106 """When the context closes, the file is closed"""
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
107 with atomictempfile(b'foo') as f:
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
108 self.assertFalse(os.path.isfile(b'foo'))
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
109 f.write(b'argh\n')
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
110 self.assertTrue(os.path.isfile(b'foo'))
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
111
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
112 def testcontextmanagerfailure(self):
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
113 """On exception, the file is discarded"""
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
114 try:
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
115 with atomictempfile(b'foo') as f:
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
116 self.assertFalse(os.path.isfile(b'foo'))
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
117 f.write(b'argh\n')
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
118 raise ValueError
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
119 except ValueError:
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
120 pass
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
121 self.assertFalse(os.path.isfile(b'foo'))
29394
6d96658a22b0 atomictempfile: add context manager support
Martijn Pieters <mjpieters@fb.com>
parents: 29393
diff changeset
122
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
123
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
124 if __name__ == '__main__':
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
125 import silenttestrunner
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 36781
diff changeset
126
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
127 silenttestrunner.main(__name__)