-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (63 loc) · 2.71 KB
/
utils.py
File metadata and controls
90 lines (63 loc) · 2.71 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import sys
import tempfile
import contextlib
import shutil
import unittest
# If we're using Python 2.6, add in more modern unittest convenience methods
if not hasattr(unittest.TestCase, 'assertIn'):
def assertIn(self, member, container, msg=None):
return self.assertTrue(member in container,
msg or '%s not found in %s' % (member, container))
def assertNotIn(self, member, container, msg=None):
return self.assertTrue(member not in container,
msg or '%s unexpectedly found in %s'
% (member, container))
def assertIsInstance(self, obj, cls, msg=None):
return self.assertTrue(isinstance(obj, cls),
msg or '%s is not an instance of %s' % (obj, cls))
def assertLessEqual(self, a, b, msg=None):
return self.assertTrue(a <= b,
msg or '%s not less than or equal to %s' % (a, b))
def assertGreaterEqual(self, a, b, msg=None):
return self.assertTrue(a >= b,
msg or '%s not greater than or equal to %s' % (a, b))
unittest.TestCase.assertIn = assertIn
unittest.TestCase.assertNotIn = assertNotIn
unittest.TestCase.assertIsInstance = assertIsInstance
unittest.TestCase.assertLessEqual = assertLessEqual
unittest.TestCase.assertGreaterEqual = assertGreaterEqual
def set_search_paths(topdir):
"""Set search paths so that we can import Python modules"""
os.environ['PYTHONPATH'] = topdir + os.pathsep \
+ os.environ.get('PYTHONPATH', '')
sys.path.insert(0, topdir)
def get_input_file_name(topdir, fname):
"""Return full path to a test input file"""
return os.path.join(topdir, 'test', 'input', fname)
@contextlib.contextmanager
def temporary_directory(dir=None):
_tmpdir = tempfile.mkdtemp(dir=dir)
yield _tmpdir
shutil.rmtree(_tmpdir, ignore_errors=True)
if 'coverage' in sys.modules:
import atexit
# Collect coverage information from subprocesses
__site_tmpdir = tempfile.mkdtemp()
with open(os.path.join(__site_tmpdir, 'sitecustomize.py'), 'w') as fh:
fh.write("""
import coverage
import atexit
import os
_cov = coverage.coverage(branch=True, data_suffix=True, auto_data=True,
data_file=os.path.join('%s', '.coverage'))
_cov.start()
def _coverage_cleanup(c):
c.stop()
atexit.register(_coverage_cleanup, _cov)
""" % os.getcwd())
os.environ['PYTHONPATH'] = __site_tmpdir + os.pathsep \
+ os.environ.get('PYTHONPATH', '')
def __cleanup(d):
shutil.rmtree(d, ignore_errors=True)
atexit.register(__cleanup, __site_tmpdir)