aboutsummaryrefslogtreecommitdiff
path: root/gitosis/test/util.py
blob: 592b766a7eae76766429a3374640b45b8636c6ca (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from nose.tools import eq_ as eq
 
import errno
import os
import shutil
import stat
import sys
 
def mkdir(*a, **kw):
    try:
        os.mkdir(*a**kw)
    except OSErrore:
        if e.errno == errno.EEXIST:
            pass
        else:
            raise
 
def maketemp():
    tmp = os.path.join(os.path.dirname(__file__)'tmp')
    mkdir(tmp)
 
    caller = sys._getframe(1)
    name = '%s.%s' % (
        sys._getframe(1).f_globals['__name__'],
        caller.f_code.co_name,
        )
    tmp = os.path.join(tmpname)
    try:
        shutil.rmtree(tmp)
    except OSErrore:
        if e.errno == errno.ENOENT:
            pass
        else:
            raise
    os.mkdir(tmp)
    return tmp
 
def writeFile(path, content):
    tmp = '%s.tmp' % path
    f = file(tmp'w')
    try:
        f.write(content)
    finally:
        f.close()
    os.rename(tmppath)
 
def readFile(path):
    f = file(path)
    try:
        data = f.read()
    finally:
        f.close()
    return data
 
def assert_raises(excClass, callableObj, *args, **kwargs):
    """
    Like unittest.TestCase.assertRaises, but returns the exception.
    """
    try:
        callableObj(*args**kwargs)
    except excClasse:
        return e
    else:
        if hasattr(excClass,'__name__')excName = excClass.__name__
        elseexcName = str(excClass)
        raise AssertionError("%s not raised" % excName)
 
def check_mode(path, mode, is_file=None, is_dir=None):
    st = os.stat(path)
    if is_dir:
        assert stat.S_ISDIR(st.st_mode)
    if is_file:
        assert stat.S_ISREG(st.st_mode)
 
    got = stat.S_IMODE(st.st_mode)
    eq(gotmode'File mode %04o!=%04o for %s' % (gotmodepath))