tests/test-atomictempfile.py
author Gregory Szorc <gregory.szorc@gmail.com>
Tue, 28 Aug 2018 15:02:48 -0700
changeset 39411 aeb551a3bb8a
parent 36781 ffa3026d4196
child 43076 2372284d9457
permissions -rw-r--r--
cborutil: implement sans I/O decoder The vendored CBOR package decodes by calling read(n) on an object. There are a number of disadvantages to this: * Uses blocking I/O. If sufficient data is not available, the decoder will hang until it is. * No support for partial reads. If the read(n) returns less data than requested, the decoder raises an error. * Requires the use of a file like object. If the original data is in say a buffer, we need to "cast" it to e.g. a BytesIO to appease the decoder. In addition, the vendored CBOR decoder doesn't provide flexibility that we desire. Specifically: * It buffers indefinite length bytestrings instead of streaming them. * It doesn't allow limiting the set of types that can be decoded. This property is useful when implementing a "hardened" decoder that is less susceptible to abusive input. * It doesn't provide sufficient "hook points" and introspection to institute checks around behavior. These are useful for implementing a "hardened" decoder. This all adds up to a reasonable set of justifications for writing our own decoder. So, this commit implements our own CBOR decoder. At the heart of the decoder is a function that decodes a single "item" from a buffer. This item can be a complete simple value or a special value, such as "start of array." Using this function, we can build a decoder that effectively iterates over the stream of decoded items and builds up higher-level values, such as arrays, maps, sets, and indefinite length bytestrings. And we can do this without performing I/O in the decoder itself. The core of the sans I/O decoder will probably not be used directly. Instead, it is expected that we'll build utility functions for invoking the decoder given specific input types. This will allow extreme flexibility in how data is delivered to the decoder. I'm pretty happy with the state of the decoder modulo the TODO items to track wanted features to help with a "hardened" decoder. The one thing I could be convinced to change is the handling of semantic tags. Since we only support a single semantic tag (sets), I thought it would be easier to handle them inline in decodeitem(). This is simpler now. But if we add support for other semantic tags, it will likely be easier to move semantic tag handling outside of decodeitem(). But, properly supporting semantic tags opens up a whole can of worms, as many semantic tags imply new types. I'm optimistic we won't need these in Mercurial. But who knows. I'm also pretty happy with the test coverage. Writing comprehensive tests for partial decoding did flush out a handful of bugs. One general improvement to testing would be fuzz testing for partial decoding. I may implement that later. I also anticipate switching the wire protocol code to this new decoder will flush out any lingering bugs. Differential Revision: https://phab.mercurial-scm.org/D4414
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
     6
import stat
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
     7
import tempfile
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
     8
import unittest
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
     9
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    10
from mercurial import (
36285
3ec9afb951a0 py3: use range instead on xrange on py3 in tests/test-atomictempfile.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32279
diff changeset
    11
    pycompat,
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    12
    util,
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    13
)
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    14
atomictempfile = util.atomictempfile
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    15
36285
3ec9afb951a0 py3: use range instead on xrange on py3 in tests/test-atomictempfile.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32279
diff changeset
    16
if pycompat.ispy3:
3ec9afb951a0 py3: use range instead on xrange on py3 in tests/test-atomictempfile.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32279
diff changeset
    17
    xrange = range
3ec9afb951a0 py3: use range instead on xrange on py3 in tests/test-atomictempfile.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 32279
diff changeset
    18
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    19
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
    20
    def setUp(self):
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
    21
        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
    22
        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
    23
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    24
    def tearDown(self):
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    25
        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
    26
29392
f21286e48bc6 atomictempfile: remove test ordering
Martijn Pieters <mjpieters@fb.com>
parents: 29391
diff changeset
    27
    def testsimple(self):
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    28
        file = atomictempfile(self._filename)
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    29
        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
    30
        tempfilename = file._tempname
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    31
        self.assertTrue(tempfilename in glob.glob(
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
    32
            os.path.join(self._testdir, b'.testfilename-*')))
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    33
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
    34
        file.write(b'argh\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    35
        file.close()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    36
29391
1acf654f0985 atomictempfile: use a tempdir to keep the test environment clean
Martijn Pieters <mjpieters@fb.com>
parents: 29201
diff changeset
    37
        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
    38
        self.assertTrue(tempfilename not in glob.glob(
36616
a007db19dc4d tests: add missing b prefixes in test-atomictempfile.py
Augie Fackler <augie@google.com>
parents: 36285
diff changeset
    39
            os.path.join(self._testdir, b'.testfilename-*')))
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"
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
    67
        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
    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
29201
a109bf7e0dc2 util: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29194
diff changeset
    78
            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
    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)
36781
ffa3026d4196 cleanup: use stat_result[stat.ST_MTIME] instead of stat_result.st_mtime
Augie Fackler <augie@google.com>
parents: 36616
diff changeset
    87
            oldtime = (oldstat[stat.ST_MTIME] + repetition) & 0x7fffffff
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
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
   123
if __name__ == '__main__':
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
   124
    import silenttestrunner
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
   125
    silenttestrunner.main(__name__)