summaryrefslogtreecommitdiff
path: root/tests/test-atomictempfile.py
blob: 9ebd7c0d64a7035ce61306d89747ade3b99cbcce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import glob
from mercurial.util import atomictempfile

# basic usage
def test1_simple():
    if os.path.exists('foo'):
        os.remove('foo')
    file = atomictempfile('foo')
    (dir, basename) = os.path.split(file._tempname)
    assert not os.path.isfile('foo')
    assert basename in glob.glob('.foo-*')

    file.write('argh\n')
    file.close()

    assert os.path.isfile('foo')
    assert basename not in glob.glob('.foo-*')
    print 'OK'

# discard() removes the temp file without making the write permanent
def test2_discard():
    if os.path.exists('foo'):
        os.remove('foo')
    file = atomictempfile('foo')
    (dir, basename) = os.path.split(file._tempname)

    file.write('yo\n')
    file.discard()

    assert not os.path.isfile('foo')
    assert basename not in os.listdir('.')
    print 'OK'

# if a programmer screws up and passes bad args to atomictempfile, they
# get a plain ordinary TypeError, not infinite recursion
def test3_oops():
    try:
        file = atomictempfile()
    except TypeError:
        print "OK"
    else:
        print "expected TypeError"

if __name__ == '__main__':
    test1_simple()
    test2_discard()
    test3_oops()