sapling/eden/hg-server/tests/test-atomictempfile.py
Durham Goode 98d9269874 server: copy hg to a new hg-server directory
Summary:
Create a fork of the Mercurial code that we can use to build server
rpms. The hg servers will continue to exist for a few more months while we move
the darkstorm and ediscovery use cases off them. In the mean time, we want to
start making breaking changes to the client, so let's create a stable copy of
the hg code to produce rpms for the hg servers.

The fork is based off c7770c78d, the latest hg release.

This copies the files as is, then adds some minor tweaks to get it to build:
- Disables some lint checks that appear to be bypassed by path
- sed replace eden/scm with eden/hg-server
- Removed a dependency on scm/telemetry from the edenfs-client tests since
  scm/telemetry pulls in the original eden/scm/lib/configparser which conflicts
  with the hg-server conflict parser.

allow-large-files

Reviewed By: quark-zju

Differential Revision: D27632557

fbshipit-source-id: b2f442f4ec000ea08e4d62de068750832198e1f4
2021-04-09 10:09:06 -07:00

136 lines
4.3 KiB
Python

from __future__ import absolute_import
import glob
import os
import shutil
import tempfile
import unittest
from edenscm.mercurial import util
from hghave import require
atomictempfile = util.atomictempfile
# force dealing with tmp filenames that go
# over the maximum file length
filename = "A" * 253
try:
xrange(0)
except NameError:
xrange = range
class testatomictempfile(unittest.TestCase):
def setUp(self):
self._testdir = tempfile.mkdtemp("atomictempfiletest")
self._filename = os.path.join(self._testdir, filename)
def tearDown(self):
shutil.rmtree(self._testdir, True)
def testsimple(self):
file = atomictempfile(self._filename)
self.assertFalse(os.path.isfile(self._filename))
tempfilename = file._tempname
self.assertTrue(tempfilename in glob.glob(os.path.join(self._testdir, ".*")))
file.write(b"argh\n")
file.close()
self.assertTrue(os.path.isfile(self._filename))
self.assertTrue(
tempfilename
not in glob.glob(os.path.join(self._testdir, ".testfilename-*"))
)
# discard() removes the temp file without making the write permanent
def testdiscard(self):
file = atomictempfile(self._filename)
(dir, basename) = os.path.split(file._tempname)
file.write(b"yo\n")
file.discard()
self.assertFalse(os.path.isfile(self._filename))
self.assertTrue(basename not in os.listdir("."))
# if a programmer screws up and passes bad args to atomictempfile, they
# get a plain ordinary TypeError, not infinite recursion
def testoops(self):
with self.assertRaises(TypeError):
atomictempfile()
# checkambig=True avoids ambiguity of timestamp
def testcheckambig(self):
def atomicwrite(checkambig):
f = atomictempfile(self._filename, checkambig=checkambig)
f.writeutf8("FOO")
f.close()
# try some times, because reproduction of ambiguity depends on
# "filesystem time"
for i in xrange(5):
atomicwrite(False)
oldstat = util.stat(self._filename)
if oldstat.st_ctime != oldstat.st_mtime:
# subsequent changing never causes ambiguity
continue
repetition = 3
# repeat atomic write with checkambig=True, to examine
# whether st_mtime is advanced multiple times as expected
for j in xrange(repetition):
atomicwrite(True)
newstat = util.stat(self._filename)
if oldstat.st_ctime != newstat.st_ctime:
# timestamp ambiguity was naturally avoided while repetition
continue
# st_mtime should be advanced "repetition" times, because
# all atomicwrite() occurred at same time (in sec)
self.assertTrue(
newstat.st_mtime == ((oldstat.st_mtime + repetition) & 0x7FFFFFFF)
)
# no more examination is needed, if assumption above is true
break
else:
# This platform seems too slow to examine anti-ambiguity
# of file timestamp (or test happened to be executed at
# bad timing). Exit silently in this case, because running
# on other faster platforms can detect problems
pass
def testread(self):
with open(self._filename, "wb") as f:
f.write(b"foobar\n")
file = atomictempfile(self._filename, mode="rb")
self.assertTrue(file.read(), b"foobar\n")
file.discard()
def testcontextmanagersuccess(self):
"""When the context closes, the file is closed"""
with atomictempfile("foo") as f:
self.assertFalse(os.path.isfile("foo"))
f.write(b"argh\n")
self.assertTrue(os.path.isfile("foo"))
def testcontextmanagerfailure(self):
"""On exception, the file is discarded"""
try:
with atomictempfile("foo") as f:
self.assertFalse(os.path.isfile("foo"))
f.write(b"argh\n")
raise ValueError
except ValueError:
pass
self.assertFalse(os.path.isfile("foo"))
if __name__ == "__main__":
import silenttestrunner
silenttestrunner.main(__name__)