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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import doctest
import re
FIRST_SCRIPT = 'prepare'
LAST_SCRIPT = 'cleanup'
PRE_EXTENSION = '.pre'
POST_EXTENSION = '.post'
PYTHON_TEST_EXTENSION = '.pytest'
BASH_TEST_EXTENSION = '.shtest'
class RunTest:
'''Runs the tests'''
def __init__(self):
self.path = os.path.abspath('.')
# Only no-hide files
self.all_files = [filename for filename in os.listdir(self.path)
if filename[0] != '.' and os.path.isfile(filename)]
self.all_files = sorted(self.all_files)
self.python_tests = []
self.bash_tests = []
self.script_tests = []
self.first_script = ''
self.last_script = ''
self.pre_scripts = []
self.post_scripts = []
for filename in self.all_files:
if filename.endswith(PYTHON_TEST_EXTENSION):
self.python_tests.append(filename)
elif filename.endswith(BASH_TEST_EXTENSION):
self.bash_tests.append(filename)
elif os.access(filename, os.X_OK):
basename, extension = os.path.splitext(filename)
if basename == FIRST_SCRIPT:
if self.first_script:
raise MoreThanOneFirstScript()
self.first_script = filename
elif basename == LAST_SCRIPT:
if self.last_script:
raise MoreThanOneLastScript()
self.last_script = filename
elif extension == PRE_EXTENSION:
self.pre_scripts.append(filename)
elif extension == POST_EXTENSION:
self.post_scripts.append(filename)
else:
self.script_tests.append(filename)
self.fails = 0
def run_script(self, script):
'''Run a script test'''
path_script = os.path.join(self.path, script)
proc = subprocess.Popen((path_script), shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return_value = proc.wait()
if return_value != 0:
self.fails += 1
stdout, stderr = proc.communicate()
print("*******************************************************")
print("Error %d in %s:" % (return_value, script))
print(stdout.decode(), end='')
print(stderr.decode(), end='')
return return_value
def run_bash_test(self, script):
'''Run bash test'''
#import pdb; pdb.set_trace()
path_script = os.path.join(self.path, script)
errors = 0
test_no = 0
for command, result, line in read_bash_tests(path_script):
test_no += 1
try:
proc = subprocess.Popen((command.split()), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
stdout = proc.communicate()[0]
except OSError as exc:
print('File "%s" line %d:' % (script, line))
print('Failed example:')
print(' ' + command)
print("Exception was raised:")
print(" ", end="")
print(exc)
print("*******************************************************")
errors += 1
else:
if result != stdout.decode():
print('File "%s" line %d:' % (script, line))
print('Failed example:')
print(' ' + command)
print("Expected:")
for l in result.split('\n')[:-1]:
print(' ' + l)
print("Got:")
for l in stdout.decode().split('\n')[:-1]:
print(' ' + l)
errors += 1
print("*******************************************************")
if errors != 0:
print("%d items had failures:" % errors)
print(" %d of %d in %s" % (errors, test_no, script))
print("***Test failed***")
else:
print("%3d tests PASSED in %s" % (test_no, script))
return errors
def run_pre_test(self, test):
'''Run the pre-test of a test'''
pre_test = test + PRE_EXTENSION
return_value = 0
if pre_test in self.pre_scripts:
return_value = self.run_script(pre_test)
if return_value:
print("No running: %s %s" % (test, test + POST_EXTENSION))
return return_value
def run_post_test(self, test):
'''Run the post-test of a test'''
post_test = test + POST_EXTENSION
if post_test in self.post_scripts:
return self.run_script(post_test)
def run_tests(self):
'''Run the tests in the correct order'''
if self.first_script:
if self.run_script(self.first_script) != 0:
print('*Error in prepare script. Aborting.*')
return self.show_errors()
all_tests = sorted(self.script_tests + self.python_tests + self.bash_tests)
for test in all_tests:
if self.run_pre_test(test) != 0:
continue
if test in self.script_tests:
self.run_script(test)
elif test in self.bash_tests:
fails = self.run_bash_test(test)
self.fails += fails
elif test in self.python_tests:
fails, n_tests = doctest.testfile(test, module_relative=False)
self.fails += fails
self.run_post_test(test)
if self.last_script:
self.run_script(self.last_script)
return self.show_errors()
def show_errors(self):
'''Show the total errors'''
if self.fails:
print("*******************************************************")
print("Total errors: %d" % self.fails)
return self.fails
class MoreThanOneFirstScript(Exception):
def __init__(self):
super(MoreThanOneFirstScript, self).__init__(
"More than one %s script" % FIRST_SCRIPT)
class MoreThanOneLastScript(Exception):
def __init__(self):
super(MoreThanOneLastScript, self).__init__(
"More than one %s script" % LAST_SCRIPT)
def read_bash_tests(file_name):
'''Iterator that yields the found tests'''
fd = open(file_name)
command = ''
result = ''
tests = []
line_no = 0
command_line = 0
command_re = re.compile("^\$ (.*)\n")
for line in fd.readlines():
line_no += 1
match = command_re.match(line)
if match:
# Is it a command?
if command:
# If it is a command but we have a previous command to send
yield (command, result, command_line)
result = ''
command = match.group(1)
command_line = line_no
elif command:
# If not a command but we have a previous command
if line != "\n":
# It's a part of the result
if line == "<BLANKLINE>\n":
result += '\n'
else:
result += line
else:
# Or it's the end of the result, yielding
yield (command, result, command_line)
command = ''
result = ''
else:
# This line is a comment
pass
if command:
# Cheking if the last command was sent
yield (command, result, command_line)
fd.close()
if __name__ == "__main__":
r = RunTest()
r.run_tests()
|