tests/test-atomictempfile.py
author Martijn Pieters <mjpieters@fb.com>
Thu, 23 Jun 2016 18:18:33 +0100
changeset 29392 f21286e48bc6
parent 29391 1acf654f0985
child 29393 50269a4dce61
permissions -rw-r--r--
atomictempfile: remove test ordering These tests are independent and numbering only makes it harder to add more and logically group them.

from __future__ import absolute_import

import glob
import os
import shutil
import tempfile
import unittest

from mercurial import (
    util,
)
atomictempfile = util.atomictempfile

class testatomictempfile(unittest.TestCase):
    def setUp(self):
        self._testdir = tempfile.mkdtemp('atomictempfiletest')
        self._filename = os.path.join(self._testdir, 'testfilename')

    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, '.testfilename-*')))

        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):
        self.assertRaises(TypeError, atomictempfile)

    # checkambig=True avoids ambiguity of timestamp
    def testcheckambig(self):
        def atomicwrite(checkambig):
            f = atomictempfile(self._filename, checkambig=checkambig)
            f.write('FOO')
            f.close()

        # try some times, because reproduction of ambiguity depends on
        # "filesystem time"
        for i in xrange(5):
            atomicwrite(False)
            oldstat = os.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 expecetd
            for j in xrange(repetition):
                atomicwrite(True)
            newstat = os.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() occured 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

if __name__ == '__main__':
    import silenttestrunner
    silenttestrunner.main(__name__)