-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbase.testbase.bmx
265 lines (203 loc) · 6.3 KB
/
base.testbase.bmx
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
SuperStrict
Import Brl.Retro
Import "base.configmap.bmx"
Import "base.processes.bmx"
Rem
Example:
local test:TTestBase = new TTestBase.Init("BasicTest")
test.commandUri = "ls -l /home/ronny"
test.doValidation = FALSE
test.Run()
END REM
Type TTestBase
Field name:String = "unnamed Test"
'the uri to the command to run
Field commandURI:String = ""
'the process of the command when executed
Field process:TCodeTesterProcess
'configuration (parameters and other things)
Field config:TConfigMap = New TConfigMap
Field receivedOutput:String = ""
Field receivedErrorOutput:String = ""
Field validationOutput:String = ""
Field expectedOutput:String = ""
Field result:Int = 0
'one might skip validation of output
'eg. only check if execution was possible
Field doValidation:Int = False
Field validated:Int = -1
Const RESULT_OK:Int = 1
Const RESULT_FAILED:Int = 0
Const RESULT_ERROR:Int = -1
Method Init:TTestBase(name:String="")
If name<>"" Then Self.name = name
validated = False
Return Self
End Method
Method GetName:String()
Return name
End Method
Method GetTestType:String()
Return "TEST"
End Method
'returns the complete commandline for process execution
'eg. "ls -l *.bat"
Method GetCommandline:String()
Local params:String = "" 'base has no params
Return commandURI + " " + params
End Method
'set what result is expected
Method SetExpectedOutput:Int(output:String="")
expectedOutput = output
'force validation
if expectedOutput <> "" then doValidation = True
End Method
Function LoadExpectedOutput:String(fileURI:String)
Local file:TStream = ReadFile(fileURI)
If Not file Then Return ""
Local content:String = ""
Local line:Int
While Not Eof(file)
If line > 0
content :+ "~n"
End If
content :+ ReadLine(file)
line :+ 1
Wend
Return content
End Function
'overrideable method to do additional processing
'of the received output (eg. to parse for certain messages)
Method ProcessOutput:Int()
'
End Method
'overrideable method run after the process finished
'eg. remove temporary files
Method CleanUp:Int()
'
End Method
'check if the test was passed
Method Validate:Int()
If Not doValidation
validationOutput = ""
Return True
EndIf
'by default we just check if the output corresponds
'to one we defined before
Print "expected: -"+expectedOutput+"-"
Print "received: -"+receivedOutput+"-"
validated = (expectedOutput = receivedOutput)
if validated
validationOutput = "OK"
else
validationOutput = "FAILED"
endif
Return validated
End Method
Method GetFormattedHeader:String(verbose:int = False)
local text:string
text :+ "* RUNNING " + GetTestType() + ": " + GetName() + "~n"
if verbose
text :+ " COMMAND: "+ GetCommandline()
endif
return text
End Method
Method GetFormattedResult:String(verbose:int = False)
local text:string
If result = RESULT_ERROR
text :+ " -> PROCESS FAILED !" + "~n"
text :+ " ERROR MESSAGE>>>" + "~n"
'output contains newline char already,skip adding it
'but add indention
text :+ " " + receivedErrorOutput.Replace("~n", "~n ") + "~n"
' text :+ receivedOutput
text :+ " <<<" + "~n"
return text
EndIf
if doValidation
text :+ " VALIDATION: " + validationOutput.Replace("~n", "~n ") + "~n"
endif
If result = RESULT_OK
text :+ " -> OK" + "~n"
ElseIf result = RESULT_FAILED
text :+ " -> FAILED" + "~n"
EndIf
return text
End Method
'checks if the given string is an error
'- eg. they need an "[ERROR:" at start
Method IsErrorLine:Int(line:String)
If line.Trim() = "" Then Return False
'this is no true error
'as bmk might auto-compile some modules
'eg. ar: Erzeugen von /path/to/BlitzMax/mod/pub.mod/oggvorbis.mod/oggvorbis.release.linux.x64.a
If line.Find("ar:") >= 0 and line.Find("/mod/") >= 0 and line.EndsWith(".a") then return False
Return True
End Method
Method GetErrorSummary:string()
if not receivedErrorOutput then return ""
local result:string
local lines:string[] = receivedErrorOutput.split("~n")
local filename:string = StripDir(name) 'name contains file name
local foundFileLine:int = False
For local i:int = 0 until lines.length
if lines[i].Find("[") = 0
local filePos:int = lines[i].Find(filename+";")
if filePos > 0
result = "["+Right(lines[i], (lines[i].length - filePos) - filename.length - 1) + " (" + result + ")"
foundFileLine = True
endif
endif
if not foundFileLine
result :+ lines[i]
endif
Next
return result
End Method
Method Run:Int()
'start the process execution
process = New TCodeTesterProcess.Init( GetCommandline(), HIDECONSOLE )
if not process then Throw "Run(): Failed to create new TCodeTesterProcess."
receivedOutput = ""
While not process.Eof() 'alive or having some output to process
If process.IOAvailable()
'append output from process if not empty
'prepend a newline if needed
'last line does not contain newline then !
local processOutput:string
'as soon as an error happens - set the result accordingly
If process.ErrorIOAvailable()
local errorOutput:string = process.ReadErrorIO()
If IsErrorLine(errorOutput)
processOutput :+ errorOutput
if receivedErrorOutput <> "" then receivedErrorOutput :+ "~n"
receivedErrorOutput :+ errorOutput
endif
EndIf
If process.StandardIOAvailable()
processOutput :+ process.ReadStandardIO()
EndIf
If processOutput <> ""
if receivedOutput <> "" then receivedOutput :+ "~n"
receivedOutput:+ processOutput
endif
EndIf
Wend
'if not done yet, finish up
process.Close()
if receivedErrorOutput
result = RESULT_ERROR
endif
'run validation (but skip processing if we already have an error...)
If result <> RESULT_ERROR
'maybe someone wants to receive additional information from
'the received text
ProcessOutput()
'run validation of the output
If Validate() Then result = RESULT_OK Else result = RESULT_FAILED
EndIf
'run cleanup - eg removing temporary files
CleanUp()
Return result
End Method
End Type