tests/test-atomictempfile.py
author Pulkit Goyal <7895pulkit@gmail.com>
Mon, 16 May 2016 04:28:22 +0530
changeset 29194 3bea82dd4c4e
parent 29188 f00f1de16454
child 29201 a109bf7e0dc2
permissions -rw-r--r--
py3: make tests/test-atomictempfile.py use absolute_import
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
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
     5
import unittest
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
     6
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
     7
from mercurial import (
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
     8
    util,
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
     9
)
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    10
atomictempfile = util.atomictempfile
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    11
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    12
class testatomictempfile(unittest.TestCase):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    13
    def test1_simple(self):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    14
        if os.path.exists('foo'):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    15
            os.remove('foo')
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    16
        file = atomictempfile('foo')
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    17
        (dir, basename) = os.path.split(file._tempname)
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    18
        self.assertFalse(os.path.isfile('foo'))
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    19
        self.assertTrue(basename in glob.glob('.foo-*'))
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    20
29188
f00f1de16454 tests: mark test-atomictempfile.py write as binary
timeless <timeless@mozdev.org>
parents: 18666
diff changeset
    21
        file.write(b'argh\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    22
        file.close()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    23
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    24
        self.assertTrue(os.path.isfile('foo'))
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    25
        self.assertTrue(basename not in glob.glob('.foo-*'))
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    26
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    27
    # discard() removes the temp file without making the write permanent
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    28
    def test2_discard(self):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    29
        if os.path.exists('foo'):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    30
            os.remove('foo')
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    31
        file = atomictempfile('foo')
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    32
        (dir, basename) = os.path.split(file._tempname)
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'yo\n')
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    35
        file.discard()
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    36
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    37
        self.assertFalse(os.path.isfile('foo'))
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    38
        self.assertTrue(basename not in os.listdir('.'))
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    39
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    40
    # 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
    41
    # get a plain ordinary TypeError, not infinite recursion
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    42
    def test3_oops(self):
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    43
        self.assertRaises(TypeError, atomictempfile)
14007
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    44
d764463b433e atomictempfile: avoid infinite recursion in __del__().
Greg Ward <greg@gerg.ca>
parents:
diff changeset
    45
if __name__ == '__main__':
29194
3bea82dd4c4e py3: make tests/test-atomictempfile.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29188
diff changeset
    46
    import silenttestrunner
18666
fb9d1c2805ff test-atomictempfile: convert to unit test
Idan Kamara <idankk86@gmail.com>
parents: 15057
diff changeset
    47
    silenttestrunner.main(__name__)