This repository was archived by the owner on Sep 7, 2021. It is now read-only.
forked from livecode/livecode-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_testrunner.livecodescript
More file actions
462 lines (371 loc) · 13.8 KB
/
_testrunner.livecodescript
File metadata and controls
462 lines (371 loc) · 13.8 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
script "IDETestRunner"
t/*
Copyright (C) 2016 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
-- FIXME provide this on the command line
constant kLogFilename = "_test_suite.log"
on startup
local tTarget
if the target begins with "card" then
put the long id of the owner of the target into tTarget
else
put the long id of the target into tTarget
end if
if tTarget is the long id of me then
if the environment begins with "development" then
send "TestRunnerInvokeAll" to me in 0
else
send "TestRunnerMain" to me in 0
end if
end if
end startup
on openStack
if the long id of the target is the long id of this card of me then
send "TestRunnerInvokeAll" to me in 0
end if
end openStack
----------------------------------------------------------------
-- Command-line processing
----------------------------------------------------------------
private function getCommandLineInfo
local tRawArg, tSelfCommand, tSelfScript, tInArgs, tArgs
put false into tInArgs
-- Treat everything up to & including the first
-- ".livecodescript" file as the command for running the test
-- runner, and everything after it as test runner arguments
put the commandName into tSelfCommand[1]
repeat for each element tRawArg in the commandArguments
if tInArgs then
put tRawArg into tArgs[1 + the number of elements in tArgs]
else
put tRawArg into tSelfCommand[1 + the number of elements in tSelfCommand]
if tRawArg ends with ".livecodescript" then
put tRawArg into tSelfScript
put true into tInArgs
end if
end if
end repeat
local tInfo
put tSelfCommand into tInfo["self-command"]
put tSelfScript into tInfo["self-script"]
put tArgs into tInfo["args"]
return tInfo
end getCommandLineInfo
----------------------------------------------------------------
-- Top-level actions
----------------------------------------------------------------
command TestRunnerMain
local tInfo
put getCommandLineInfo() into tInfo
logInit
switch tInfo["args"][1]
case "run"
doRun tInfo
break
case "--help"
case "-h"
case "help"
doUsage 0
break
default
doUsage 1
break
end switch
quit 0
end TestRunnerMain
command TestRunnerInvokeAll
if there is not a stack "home" then
invokeLoadIDE
end if
invokeAllTests
end TestRunnerInvokeAll
private command doRun pInfo
if pInfo["args"][2] is empty then
doUsage 1
end if
runLoadLibrary pInfo
local tAnalysis
runAllTests pInfo
put the result into tAnalysis
-- Save the log to file.
-- We process to binary data ourselves to ensure encoding and
-- line endings are appropriate.
local tLogForWriting
put textEncode(tAnalysis["log"], "utf8") into tLogForWriting
if the platform is "win32" then
replace return with numToChar(13) & numToChar(10) in tLogForWriting
end if
put tLogForWriting into url ("binfile:" & kLogFilename)
if TesterTapGetWorstResult(tAnalysis) is "FAIL" then
quit 1
end if
end doRun
private command doUsage pStatus
write "Usage: _testrunner.livecodescript run DEV-ENGINE" & return to stderr
quit pStatus
end doUsage
on ErrorDialog pExecutionError
write "ERROR:" && pExecutionError & return to stderr
quit 1
end ErrorDialog
----------------------------------------------------------------
-- Support for invoking test commands
----------------------------------------------------------------
private command invokeLoadIDE
-- Compute the filename of the home stack
local tFilename
put the filename of me into tFilename
set the itemDelimiter to slash
put "Toolset/home.livecodescript" into item -2 to -1 of tFilename
local tStackName
put the name of stack tFilename into tStackName
insert the script of tStackName into back
dispatch "startup" to tStackName
open tStackName
-- Set the 'test environment' to true
revSetTestEnvironment true
end invokeLoadIDE
private command invokeAllTests
-- gRevDevelopment should be true by default
global gRevDevelopment
put true into gRevDevelopment
invokeLoadLibrary
local tTopLevel
put revIDESpecialFolderPath("IDE") & "/tests" into tTopLevel
local tFile, tAnalysis
repeat for each element tFile in TesterGetTestFileNames(tTopLevel)
invokeTestScript tTopLevel & slash & tFile
put TesterTapCombine(tAnalysis, the result) into tAnalysis
end repeat
quit 0
end invokeAllTests
-- Run the tests found in one specific script file
private command invokeTestScript pScriptFile
-- Don't output to stdout
TestOutputToVariable
local tCommand, tAnalysis, tOutput
repeat for each element tCommand in TesterParseTestCommandNames(pScriptFile)
invokeTestCommand pScriptFile, tCommand
put TestFetchAndClearOutput() into tOutput
write textEncode(tOutput,"utf-8") to stdout
runTestProcessOutput pScriptFile, tCommand, tOutput
put TesterTapCombine(tAnalysis, the result) into tAnalysis
end repeat
return tAnalysis
end invokeTestScript
-- Execute a test
private command invokeTestCommand pScriptFile, pCommand
-- Try and remove any stacks that have been opened
local tStacks
put the openstacks into tStacks
local tStackName, tResult
-- This should auto-load the test script
put the name of stack pScriptFile into tStackName
-- Check that the stack script actually compiles
lock messages
set the script of tStackname to the script of tStackname
put the result into tResult
unlock messages
if tResult is not empty then
TestDiagnostic tResult
end if
TestAssert "script compiles", tResult is empty
--Make sure this is not the default stack
create stack
set the defaultStack to the short name of it
-- Dispatch the test setup command, and check if skipping was requested
dispatch "TestSetup" to tStackName
put the result into tResult
if word 1 of tResult is "SKIP" then
TestSkip pCommand, word 2 to -1 of tResult
else
-- Run the actual test itself
try
dispatch pCommand to tStackName
catch tError
TestAssert "execution failure:" && tError, false
end try
-- Do common cleanup tasks
dispatch "TestTearDown" to tStackName
end if
global gRevStackStatus
repeat for each line tStack in the openstacks
if tStack is not among the lines of tStacks then
-- Ensure IDE doesn't prompt for save
put empty into gRevStackStatus[tStack]
delete stack tStack
end if
end repeat
end invokeTestCommand
-- Add the unit test library stack and the input library to the backscripts
private command invokeLoadLibrary
local tLibrary, tStackName, tStackFile, tRepoPath
put revEnvironmentRepositoryPath() into tRepoPath
repeat for each item tLibrary in "_testlib,_inputlib,_testerlib"
-- Compute the filename of the library stack and load it
put tRepoPath & slash & "tests" & slash & tLibrary & ".livecodescript" into tStackFile
put the name of stack tStackFile into tStackName
send "revLoadLibrary" to stack tStackname
end repeat
end invokeLoadLibrary
----------------------------------------------------------------
-- Support for running tests
----------------------------------------------------------------
-- Add the test runner library stack to the backscripts
private command runLoadLibrary pInfo
-- Compute the filename of the library stack
local tFilename
put pInfo["self-script"] into tFilename
set the itemDelimiter to slash
put "_testerlib.livecodescript" into item -1 of tFilename
put "../" before tFilename
-- Load the library
local tStackname
put the name of stack tFilename into tStackname
send "revLoadLibrary" to stack tStackname
end runLoadLibrary
-- Launch this stack in the IDE
private command runAllTests pInfo
local tAnalysis
-- Invoke the test in a subprocess. This ensures that we can detect
-- if a crash occurs
local tTestOutput, tTestExitStatus, tCommand
-- First put the target engine into the command to run
put pInfo["args"][2] into tCommand
-- Add the command line args
repeat with x = 3 to the number of elements in pInfo["args"]
put tCommand && pInfo["args"][x] into tCommand
end repeat
-- Add the target stack (this one)
put tCommand && pInfo["self-script"] into tCommand
-- Execute the shell command
put shell(tCommand) into tTestOutput
put the result into tTestExitStatus
-- The output from the subprocesses will be native encoded utf-8.
put textDecode(tTestOutput, "utf8") into tTestOutput
-- Check the exit status. If it suggests failure, add a "not ok" stanza
-- to the tail of the TAP output
if tTestExitStatus is not empty then
put return after tTestOutput
put "not ok # Subprocess exited with status" && \
tTestExitStatus & return after tTestOutput
end if
local tUsefulOutput
repeat for each line tLine in tTestOutput
if tLine begins with "PASS:" then
put tLine & return after tUsefulOutput
else if tLine begins with "FAIL:" then
put tLine & return after tUsefulOutput
else if tLine begins with "not ok" then
put tLine & return after tUsefulOutput
end if
end repeat
write tUsefulOutput to stdout
runTestProcessOutput "IDE", "All IDE tests", tTestOutput
put TesterTapCombine(tAnalysis, the result) into tAnalysis
runPrintSummary(tAnalysis)
return tAnalysis
end runAllTests
private command runTestProcessOutput pScriptfile, pCommand, pOutput
-- Create test log
local tTestLog
put "###" && TesterGetPrettyTestName(pScriptFile) && pCommand \
& return & return into tTestLog
put pOutput & return after tTestLog
-- Analyse the results and print a summary line
local tTapResults
put TesterTapAnalyse(tTestLog) into tTapResults
logSummaryLine tTapResults, (TesterGetPrettyTestName(pScriptFile) & ":" && pCommand)
return tTapResults
end runTestProcessOutput
-- Print out a table of statistics
private command runPrintSummary pAnalysis
local tSummaryString, tTotal, tDecoration
put TesterTapGetTestCount(pAnalysis) into tTotal
-- Format basic summary information
if pAnalysis["xfail"] is 0 and pAnalysis["fail"] is 0 then
put "All" && tTotal && "tests passed" into tSummaryString
else if pAnalysis["fail"] is 0 then
put "All" && tTotal && "tests behaved as expected" into tSummaryString
else
put pAnalysis["fail"] && "OF" && tTotal && "TESTS FAILED" into tSummaryString
end if
put return after tSummaryString
-- Add extra summary info from expected failure & skip directives
if pAnalysis["xpass"] > 0 then
put tab & pAnalysis["xpass"] && "unexpected passes" & return after tSummaryString
end if
if pAnalysis["xfail"] > 0 then
put tab & pAnalysis["xfail"] && "expected failures" & return after tSummaryString
end if
if pAnalysis["skip"] > 0 then
put tab & pAnalysis["skip"] && "skipped" & return after tSummaryString
end if
put "================================================================" into tDecoration
put tDecoration & return before tSummaryString
put tDecoration & return after tSummaryString
write tSummaryString to stdout
end runPrintSummary
----------------------------------------------------------------
-- Logging helpers
----------------------------------------------------------------
local sLogInfo
-- Figure out what the highlighting escape codes are for the terminal
--
-- FIXME this really doesn't work properly if LiveCode's stdout
-- *isn't* a TTY.
private command logInit
-- We can only do colour on Linux and OS X
if the platform is not "Linux" and the platform is not "MacOS" then
put false into sLogInfo
end if
-- Check if colouring is possible
local tTput
put shell("tput colors") into tTput
if the result is not empty or tTput <= 8 then
put false into sLogInfo
end if
-- Get colours
put shell("tput sgr0") into sLogInfo["normal"]
put shell("tput bold") into sLogInfo["bold"]
put shell("tput setaf 1") into sLogInfo["red"]
put shell("tput setaf 2") into sLogInfo["green"]
put shell("tput setaf 3") into sLogInfo["yellow"]
put shell("tput setaf 6") into sLogInfo["cyan"]
end logInit
private function logHighlight pString
if pString is "fail" then
return sLogInfo["red"] & sLogInfo["bold"] & pString & sLogInfo["normal"]
else if pString is "xfail" or pString is "xpass" then
return sLogInfo["yellow"] & pString & sLogInfo["normal"]
else if pString is "pass" then
return sLogInfo["green"] & pString & sLogInfo["normal"]
else if pString is "skip" then
return sLogInfo["cyan"] & pString & sLogInfo["normal"]
else
return pString
end if
end logHighlight
private command logSummaryLine pTapResults, pTestName
local tTotal, tPassed, tWorst, tMessage
put pTapResults["xpass"] + pTapResults["pass"] + pTapResults["skip"] into tPassed
put TesterTapGetTestCount(pTapResults) into tTotal
put TesterTapGetWorstResult(pTapResults) into tWorst
put logHighLight(the toUpper of tWorst) into tMessage
put ":" after tMessage
if the number of chars in tWorst < 5 then
put space after tMessage
end if
put space & pTestName after tMessage
put space & "[" & tPassed & "/" & tTotal & "]" after tMessage
write tMessage & return to stdout
end logSummaryLine