forked from zopefoundation/grokproject
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
137 lines (118 loc) · 3.6 KB
/
tests.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Grabs the tests in doctest
Previously Taken virtually verbatim from zopeskel with permission (for ZPL)
from Tarek Ziade. (c) Tarek Ziade
"""
__docformat__ = 'restructuredtext'
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
import doctest
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
SHORTTESTFILE = os.path.join(os.path.dirname(__file__), 'shorttests')
def rmdir(*args):
dirname = os.path.join(*args)
if os.path.isdir(dirname):
shutil.rmtree(dirname)
def read_sh(command, input=None, opts=None):
# XXX check for other platforms too?
close_fds = not sys.platform.startswith('win')
if opts is not None:
command = [command]
command.extend(opts)
p = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
close_fds=close_fds
)
out, err_ = p.communicate(input)
return out
def sh(cmd):
print cmd
print read_sh(cmd)
def ls(*args):
dirname = os.path.join(*args)
if os.path.isdir(dirname):
filenames = os.listdir(dirname)
for filename in sorted(filenames):
print filename
else:
print 'No directory named %s' % dirname
def cd(*args):
dirname = os.path.join(*args)
os.chdir(dirname)
def cat(*args):
filename = os.path.join(*args)
if os.path.isfile(filename):
print open(filename).read()
else:
print 'No file named %s' % filename
def shorttests():
return os.path.exists(SHORTTESTFILE)
def maybe_mkdir(path):
if shorttests() and os.path.isdir(path):
return
rmdir(path)
os.makedirs(path)
def maybe_rmdir(path):
if shorttests() and os.path.isdir(path):
return
rmdir(path)
def setup(test):
if CURRENT_DIR.find('/travis/') >= 0:
eggsdir = os.path.join(CURRENT_DIR, 'eggs') # work around buildout bug
else:
eggsdir = os.path.join(tempfile.gettempdir(), 'grokproject-test-eggs')
maybe_mkdir(eggsdir)
test.globs['eggsdir'] = eggsdir
test.globs['testdir'] = tempfile.mkdtemp()
def teardown(test):
if CURRENT_DIR.find('/travis/') < 0: # Work around buildout bug,
maybe_rmdir(test.globs['eggsdir'])
shutil.rmtree(test.globs['testdir'])
def show_shorttests_message():
if shorttests():
print
print 'WARNING: running shorttests.'
print
print ' This reduces the runtime of testruns by making use of'
print ' a once filled eggs directory.'
print ' If you want clean test runs with an empty eggs directory,'
print ' remove the file "' + SHORTTESTFILE + '".'
print
print ' Running shorttests might lead to failing tests. Please run'
print ' the full tests before submitting code.'
print
else:
print
print 'NOTE: running full tests.'
print
print ' If you want to reuse a prefilled eggs directory between'
print ' test runs (which dramatically reduces runtime), create a'
print ' file "' + SHORTTESTFILE + '" and rerun the tests.'
print
def test_suite():
show_shorttests_message()
globs = {
'ls': ls,
'cd': cd,
'cat': cat,
'sh': sh,
'read_sh': read_sh,
'current_dir': CURRENT_DIR,
}
suite = []
suite.append(
doctest.DocFileSuite(
'tests.txt',
optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE,
globs=globs,
setUp=setup,
tearDown=teardown,
))
return unittest.TestSuite(suite)