-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforunit.py
executable file
·284 lines (192 loc) · 7.12 KB
/
forunit.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import TestFileParser
import subprocess
import makeFortran as maker
import ShellFormat
# Array that holds the names of the test files
testFileNames = []
# Array that holds the names of the tests
testNames = []
# names of the fortran test modules created by the parser
testModuleNames = []
# array of every test routine in every file
testRoutines = []
# overall number of tests (for all files)
numberOfTests = 0
# overall number of failed tests (for all files)
numberOfFailedTests = 0
# array of TestFileParser instances
parsers = []
# gfortran link flags
linkFlags = []
# gfortran compiler flags
compilerFlags = []
# the used fortran extension
fortranExtension = '.f03'
fortranExtensions = ['.f70', '.f90', '.f95', '.f03']
# current working directory
cwd = './'
# Finds all .fu test files in the path
def findTestFiles(path='./'):
global fortranExtension
foundExtensions = []
# find all .fu testfiles
filenames = os.listdir(path)
for filename in filenames:
s = filename[-3:]
if s == '.fu':
testName = filename[:-3]
print 'found test '+filename
# found a forunit testfile, search for the module
extension = findOriginalModule(testName)
if extension not in foundExtensions:
foundExtensions.append(extension)
testFileNames.append(filename)
testNames.append(testName)
if len(testFileNames) == 0:
print 'Did not find any .fu test files.\n'
exit()
# make sure there is only one file extension
if len(foundExtensions) == 0:
# no fortran module found
message = 'Did not find a fortran module with a valid file extension: ' + ', '.join(fortranExtensions)
runtimeError(message)
exit()
elif len(foundExtensions) > 1:
# found more than one module
message = 'Found fortran modules with different fortran extensions: ' + '(' + ' '.join(fortranExtensions) + ').'
runtimeError(message)
exit()
else:
fortranExtension = foundExtensions[0]
# looks for testName.f70, testName.f90, ...
# and returns the extensions of the found file
#
def findOriginalModule(testName):
global fortranExtensions
foundExtensions = []
for extension in fortranExtensions:
try:
f = open(testName+extension)
foundExtensions.append(extension)
except IOError:
pass
if len(foundExtensions) == 0:
# no fortran module found
message = 'Did not find a fortran module for test ' + testName + ' using extensions ' + ', '.join(fortranExtensions)
runtimeError(message)
exit()
elif len(foundExtensions) > 1:
# found more than one module
message = 'Found more than one fortran file for test: ' + testName + '(' + ' '.join(fortranExtensions) + ').'
runtimeError(message)
exit()
else:
return foundExtensions[0]
# prints an error message
def runtimeError(message):
print ShellFormat.ERROR + 'ERROR: ' + message + '\n'
# Creates the fortran program TestRunner that will call every test subroutine
#
def createTestRunner():
s = 'program TestRunner\n'
# use every test module
for moduleName in testModuleNames:
s += '\tuse '+moduleName+'\n'
s += '\n\timplicit none\n\n'
# for every parsed test, call its test routines and teardown routines if one exists
for parser in parsers:
for testRoutine in parser.testRoutines:
s += '\tcall '+testRoutine+'()\n'
if parser.hasTeardown == True:
s += '\tcall ' + parser.subroutinePrefix + 'teardown()\n'
# print a FINISHED statement so the executing python program knows when the TestRunner finished testing
s += '\n\tWRITE(*,*) "FINISHED TESTRUNNER"'
s += '\nend program TestRunner'
f = open('TestRunner'+fortranExtension, 'w')
f.write(s)
f.close()
print 'created TestRunner'+fortranExtension
# This method uses the makeFortran module to create a makefile for the TestRunner
def createMakeFile():
global compilerFlags
global linkFlags
maker.target = 'TestRunner'
maker.compilerFlags = compilerFlags
maker.linkFlags = linkFlags
maker.makeFileName = 'makeTestRunner'
maker.createMakeFile()
# Compiles the tests using make
def compileTests():
output = subprocess.call('make -f makeTestRunner', shell=True)
if output != 0:
print ShellFormat.ERROR + '\n\tA COMPILER ERROR OCCURRED: code '+str(output)+ShellFormat.END+'\n'
return output
# Starts the test runner to run every test and parse the returned outcome
def runTestRunner():
p = subprocess.Popen(['./TestRunner.exe'], stdout=subprocess.PIPE)
output = []
# collect output lines until TestRunner prints the keyword FINISHED
while True:
line = p.stdout.readline()
if 'FINISHED TESTRUNNER' in line:
break
output.append(line)
p.kill()
# parse the collected test results for FAILED statements
parseTestResults(output)
return output
# Deletes all files created by this test environment
def cleanup():
deleteList = ['TestRunner.o', 'TestRunner'+fortranExtension, 'TestRunner.exe', '*ForUnitTest.*']
for testModule in testModuleNames:
deleteList.append(testModule+fortranExtension)
deleteList.append(testModule+'.o')
deleteList.append('makeTestRunner')
o = subprocess.call('rm '+' '.join(deleteList))
# Counts and prints FAILED statements of TestRunner
def parseTestResults(output):
global numberOfFailedTests
for line in output:
if line.find('FAILED') > -1:
numberOfFailedTests += 1
print ShellFormat.ERROR + line + ShellFormat.END
else:
print line
# Prints a summary of the test results
def printOverallResult():
global numberOfFailedTests
global numberOfTests
if numberOfFailedTests != 0:
msg = ShellFormat.BOLD + ShellFormat.ERROR + str(numberOfFailedTests) + ' OUT OF ' + str(numberOfTests) + ' TESTS FAILED!' + ShellFormat.END + '\n'
else:
msg = ShellFormat.BOLD + ShellFormat.OKGREEN + 'ALL ' + str(numberOfTests) + ' TESTS PASSED!' + ShellFormat.END + '\n'
print '+'*100+'\n'
print '\t' + msg
def run():
global numberOfTests
global testModuleNames
global testRoutines
global parsers
global testFileNames
global cwd
print ShellFormat.OKBLUE + ShellFormat.BOLD + '\n\tRUNNING FORUNIT TESTSUITE\n' + ShellFormat.END
findTestFiles(cwd)
print 'parsing test files...'
# parse each test file
for testFileName in testFileNames:
parser = TestFileParser.TestFileParser(testFileName)
parser.parseTestFile()
parsers.append(parser)
testModuleNames.append(parser.fortranModuleName)
testRoutines.extend(parser.testRoutines)
numberOfTests += parser.numberOfTests
print 'created test '+parser.testName+' as '+parser.fortranTestfileName
createTestRunner()
createMakeFile()
print '\n'+'+'*100+'\n\ncompiling...\n'
compileTests()
print 'run TestRunner...'
runTestRunner()
printOverallResult()
# run()