This repository was archived by the owner on Sep 11, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathruntests.py
More file actions
executable file
·601 lines (515 loc) · 18.2 KB
/
Copy pathruntests.py
File metadata and controls
executable file
·601 lines (515 loc) · 18.2 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import re
import os
import glob
import shutil
import signal
import sys
import difflib
import argparse
from io import StringIO
import subprocess
from multiprocessing import Pool
import time
import tempfile
import configparser
#
# test properties
#
class TestOptions:
testBuildFlags = []
testRoot = os.path.dirname(os.path.abspath(__file__))
runTestRoot = os.path.dirname(os.path.abspath(__file__))
ceramicCompiler = None
ceramicPlatform = None
cleanUpLater = []
#
# utils
#
def indented(txt):
return " " + txt.rstrip("\n").replace("\n", "\n ") + "\n"
#
# getCeramicCompiler
#
def getCeramicCompiler(opt):
buildPath = ["..", "build", "compiler"]
ceramicexe = None
if sys.platform == "win32":
ceramicexe = "ceramic.exe"
else:
ceramicexe = "ceramic"
compiler = os.path.join(opt.testRoot, *(buildPath + [ceramicexe]))
if not os.path.exists(compiler):
compiler = shutil.which(ceramicexe)
if compiler is None:
print("could not find the ceramic compiler")
sys.exit(1)
return compiler
#
# getCeramicPlatform, fileForPlatform
#
def getCeramicPlatform(opt):
with tempfile.NamedTemporaryFile(
suffix=".crm", delete=False, mode="w", encoding="utf-8"
) as platform_ceramic:
platform_ceramic.write(
"import core.platform.(OSString, OSFamilyString, CPUString, CPUBits);\n"
)
platform_ceramic.write("import printer.(println);\n")
platform_ceramic.write("main() {\n")
platform_ceramic.write(' println("[Target]");\n')
platform_ceramic.write(' println("platform: ", OSString);\n')
platform_ceramic.write(' println("platform-family: ", OSFamilyString);\n')
platform_ceramic.write(' println("architecture: ", CPUString);\n')
platform_ceramic.write(' println("bits: ", CPUBits);\n')
platform_ceramic.write(" return 0;\n")
platform_ceramic.write("}\n")
src_path = platform_ceramic.name
try:
cmd = [opt.ceramicCompiler] + opt.testBuildFlags + ["-o", "test.exe", src_path]
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
raise RuntimeError("could not build an executable with: " + " ".join(cmd))
try:
r = subprocess.run(
["./test.exe"], check=True, stdout=subprocess.PIPE, text=True
)
out_text = r.stdout
except subprocess.CalledProcessError as ex:
raise RuntimeError(f"executable failed with exit code {ex.returncode}")
cfg = configparser.ConfigParser()
cfg.read_string(out_text)
# cleanup
try:
os.unlink(src_path)
os.unlink("test.exe")
except OSError:
pass
return (
cfg.get("Target", "platform"),
cfg.get("Target", "platform-family"),
cfg.get("Target", "architecture"),
cfg.get("Target", "bits"),
)
def fileForPlatform(opt, folder, name, ext):
platform, family, arch, bits = opt.ceramicPlatform
platformNames = [
"%s.%s.%s.%s.%s" % (name, platform, arch, bits, ext),
"%s.%s.%s.%s.%s" % (name, family, arch, bits, ext),
"%s.%s.%s.%s" % (name, platform, arch, ext),
"%s.%s.%s.%s" % (name, platform, bits, ext),
"%s.%s.%s.%s" % (name, family, arch, ext),
"%s.%s.%s.%s" % (name, family, bits, ext),
"%s.%s.%s.%s" % (name, arch, bits, ext),
"%s.%s.%s" % (name, platform, ext),
"%s.%s.%s" % (name, family, ext),
"%s.%s.%s" % (name, arch, ext),
"%s.%s.%s" % (name, bits, ext),
]
for platformName in platformNames:
fullName = os.path.join(folder, platformName)
if os.path.isfile(fullName):
return fullName
return os.path.join(folder, "%s.%s" % (name, ext))
#
# RefText
#
def normalize(text):
text = text.replace("\r", "")
if not text or text.endswith("\n"):
return text
else:
return text + "\n"
class RefText(object):
def __init__(self, name, text):
self.name = name
text = normalize(text)
self.text = text
self.re = None
xx = text.split("\n", 1)
if len(xx) == 2 and xx[0] == "(re)":
restring = xx[1].replace("\r", "").strip()
self.re = re.compile(restring, re.DOTALL)
else:
self.re = None
@staticmethod
def load(name, path):
with open(path, encoding="utf-8") as f:
text = f.read().replace("\r", "")
return RefText(name=name, text=text)
@staticmethod
def empty(name):
return RefText(name=name, text="")
def compare(self, actual):
actual = normalize(actual)
if self.re is not None:
if self.re.match(actual):
return None
r = ""
r += "expecting to match regexp:\n"
r += indented(self.re.pattern)
r += self.name + ":\n"
r += indented(actual)
return r
else:
if actual == self.text:
return None
r = ""
for line in difflib.unified_diff(
self.text.splitlines(True),
actual.splitlines(True),
fromfile=self.name,
tofile="stdout",
):
r += indented(line)
return r
#
# TestCase
#
class TestCase(object):
allCases = []
opt = None
def __init__(self, opt, folder, testfile, base=None):
self.opt = opt
entries = os.listdir(folder)
self.testfile = os.path.basename(testfile)
self.path = folder
if os.path.isfile(testfile):
self.loadTest(testfile)
runscript = fileForPlatform(self.opt, folder, "run", "py")
if os.path.isfile(runscript):
self.runscript = runscript
else:
self.runscript = None
buildflags = fileForPlatform(self.opt, folder, "buildflags", "txt")
if os.path.isfile(buildflags):
with open(buildflags, encoding="utf-8") as f:
self.buildflags = f.read().split()
else:
self.buildflags = []
for entry in entries:
fullpath = os.path.join(folder, entry)
if not os.path.isdir(fullpath):
continue
findTestCase(self.opt, fullpath, self)
def loadTest(self, testfile):
TestCase.allCases.append(self)
self.testfile = testfile
def cmdline(self, ceramic):
r = (
[ceramic, "-I" + self.path, "-o", "test.exe", "-Dtest.minimal"]
+ self.opt.testBuildFlags
+ self.buildflags
+ [self.testfile]
)
return r
def pre_build(self):
pass
def post_build(self):
pass
def post_run(self):
for f in glob.glob("temp*") + glob.glob("*.data"):
try:
os.unlink(f)
except OSError:
self.opt.cleanUpLater.append(f)
def match(self, resultout, resulterr, returncode):
compilererrfile = fileForPlatform(self.opt, ".", "compilererr", "txt")
if os.path.isfile(compilererrfile):
with open(compilererrfile, encoding="utf-8") as f:
errpattern = f.read()
errpattern = errpattern.replace("\r", "").strip()
resulterr = resulterr.replace("\r", "")
if returncode != "compiler error":
self.testLogBuffer.write("Failure: compiler did not fail\n")
self.testLogBuffer.write("Expected:\n")
self.testLogBuffer.write(indented(errpattern))
return "compiler did not fail"
if re.search(errpattern, resulterr):
return "ok"
else:
self.testLogBuffer.write("Failure: unexpected compiler error\n")
self.testLogBuffer.write("Expected:\n")
self.testLogBuffer.write(indented(errpattern))
self.testLogBuffer.write("Actual:\n")
self.testLogBuffer.write(indented(resulterr))
return "unexpected compiler error"
else:
if returncode == "compiler error":
self.testLogBuffer.write("Failure: compiler error\n")
self.testLogBuffer.write("Error:\n")
self.testLogBuffer.write(indented(resulterr))
return "compiler error"
outfile = fileForPlatform(self.opt, ".", "out", "txt")
errfile = fileForPlatform(self.opt, ".", "err", "txt")
warnfile = fileForPlatform(self.opt, ".", "warn", "txt")
if not os.path.isfile(outfile):
self.testLogBuffer.write("Failure: out.txt missing\n")
self.testLogBuffer.write("Stdout:\n")
self.testLogBuffer.write(indented(resultout))
self.testLogBuffer.write("Stderr:\n")
self.testLogBuffer.write(indented(resulterr))
return "out.txt missing"
refout = RefText.load("out.txt", outfile)
referr = RefText.empty("err.txt")
if os.path.isfile(errfile):
referr = RefText.load("err.txt", errfile)
refwarn = RefText.empty("warn.txt")
if os.path.isfile(warnfile):
refwarn = RefText.load("warn.txt", warnfile)
outcompare = refout.compare(resultout)
errcompare = referr.compare(resulterr)
warncompare = refwarn.compare(self.compilerWarnings)
if not outcompare and not errcompare and not warncompare:
return "ok"
failures = []
if outcompare:
failures.append("out.txt")
if errcompare:
failures.append("err.txt")
if warncompare:
failures.append("warn.txt")
label = " and ".join(failures) + " mismatch"
self.testLogBuffer.write("Failure: %s\n" % label)
if outcompare:
self.testLogBuffer.write("Diff-Stdout:\n")
self.testLogBuffer.write(outcompare)
if errcompare:
self.testLogBuffer.write("Diff-Stderr:\n")
self.testLogBuffer.write(errcompare)
if warncompare:
self.testLogBuffer.write("Diff-Warnings:\n")
self.testLogBuffer.write(warncompare)
return label
def run(self):
self.testLogBuffer = StringIO()
os.chdir(self.path)
self.pre_build()
self.post_build()
resultout, resulterr, returncode = self.runtest()
self.post_run()
r = self.match(resultout, resulterr, returncode)
log = self.testLogBuffer.getvalue()
self.testLogBuffer.close()
return (r, log)
def name(self):
return os.path.relpath(self.path, self.opt.testRoot)
def runtest(self):
outfilename = "test.exe"
outfilename = os.path.join(".", outfilename)
self.compilerWarnings = ""
process = subprocess.Popen(
self.cmdline(self.opt.ceramicCompiler),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
compilerout, compilererr = process.communicate()
if process.returncode != 0:
return "", "%s\n%s" % (compilerout, compilererr), "compiler error"
# warnings land on stderr of a successful compile
self.compilerWarnings = compilererr
if self.runscript is None:
commandline = [outfilename]
else:
commandline = [
sys.executable,
self.runscript,
outfilename,
] + self.opt.testBuildFlags
process = subprocess.Popen(
commandline, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
resultout, resulterr = process.communicate()
self.removefile(outfilename)
return resultout, resulterr, process.returncode
def removefile(self, filename):
# on windows, sometimes, deleting a file
# immediately after executing it
# results in a 'access denied' error.
# so we wait and try again a few times.
attempts = 1
while (attempts <= 3) and os.path.exists(filename):
try:
os.unlink(filename)
except OSError:
time.sleep(1)
attempts += 1
class TestModuleCase(TestCase):
def match(self, resultout, resulterr, returncode):
if returncode == 0:
return "ok"
elif returncode == "compiler error":
self.testLogBuffer.write("Failure: compiler error\n")
self.testLogBuffer.write("Error:\n")
self.testLogBuffer.write(indented(resulterr))
return "compiler error"
else:
self.testLogBuffer.write("Failure: exit code %d\n" % returncode)
self.testLogBuffer.write("Stdout:\n")
self.testLogBuffer.write(indented(resultout))
self.testLogBuffer.write("Stderr:\n")
self.testLogBuffer.write(indented(resulterr))
return "exit code %d" % returncode
class TestDisabledCase(TestCase):
def runtest(self):
return "disabled", "", None
def match(self, resultout, resulterr, returncode):
return "disabled"
#
# runtests
#
def findTestCase(opt, folder, base=None):
testPath = fileForPlatform(opt, folder, "test", "crm")
mainPath = fileForPlatform(opt, folder, "main", "crm")
testDisabledPath = fileForPlatform(opt, folder, "test-disabled", "crm")
mainDisabledPath = fileForPlatform(opt, folder, "main-disabled", "crm")
if os.path.isfile(testPath):
TestModuleCase(opt, folder, testPath, base)
elif os.path.isfile(testDisabledPath):
TestDisabledCase(opt, folder, testDisabledPath, base)
elif os.path.isfile(mainDisabledPath):
TestDisabledCase(opt, folder, mainDisabledPath, base)
else:
TestCase(opt, folder, mainPath, base)
def findTestCases(opt):
findTestCase(opt, opt.runTestRoot)
return TestCase.allCases
def initWorker():
signal.signal(signal.SIGINT, signal.SIG_IGN)
def runTest(t):
return t.run()
def runTests(opt):
testcases = findTestCases(opt)
try:
pool = Pool(initializer=initWorker)
results = pool.imap(runTest, testcases)
except ImportError:
pool = None
results = (runTest(t) for t in testcases)
succeeded = []
failed = []
disabled = []
failure_logs = []
try:
print("[TargetPlatform]")
platform, family, arch, bits = opt.ceramicPlatform
print("platform:", platform)
print("cpu:", arch)
print("bits:", bits)
print()
print("[Compiler]")
print("exe:", opt.ceramicCompiler)
print()
print("[Tests]")
for test in testcases:
startTime = time.time()
res, log = next(results)
endTime = time.time()
print("%s: %s (%fs)" % (test.name(), res, (endTime - startTime)))
if res == "disabled":
disabled.append(test.name())
elif res != "ok":
failed.append("%s: %s" % (test.name(), res))
failure_logs.append(log)
else:
succeeded.append(test.name())
except KeyboardInterrupt:
print("\nInterrupted!")
if pool:
pool.terminate()
if failed:
print()
print("[Failed]")
for name, log in zip(failed, failure_logs):
print(name)
print(log)
print()
print("[Summary]")
print("Passed: %d" % len(succeeded))
if disabled:
print("Disabled: %d" % len(disabled))
if failed:
print("Failed: %d" % len(failed))
return len(failed)
def main():
opt = TestOptions()
argp = argparse.ArgumentParser(
description="Run the Ceramic test suite.",
usage="%(prog)s [options] [test/path]",
)
argp.add_argument(
"-I",
metavar="path",
dest="libCeramicPaths",
action="append",
help="Specifies the Ceramic module path(s) to use.",
)
argp.add_argument(
"--arch",
metavar="arch",
dest="arch",
help="Specifies a Darwin target architecture to build for. (Apple only)",
)
argp.add_argument(
"--target",
metavar="cpu-vendor-os",
dest="target",
help="Specifies the target triple to build for.",
)
argp.add_argument(
"--ceramic",
metavar="path",
dest="ceramicCompiler",
help="Use the specified ceramic compiler (defaults to ceramic on path, or in build dir).",
)
argp.add_argument(
"--buildflags",
nargs="+",
metavar="flags",
dest="buildFlags",
help="Additional build flags to pass through to the Ceramic compiler.",
)
argp.add_argument(
"--root",
metavar="path",
dest="testRoot",
help="Specifies the root directory out of which to run tests.",
)
argp.add_argument(
"testname",
nargs="?",
help="Only run tests under the specified subdirectory in the test root.",
)
args = argp.parse_args()
if args.arch is not None:
opt.testBuildFlags += ["-arch", args.arch]
if args.libCeramicPaths is not None:
opt.testBuildFlags += [
"-I" + os.path.abspath(path) for path in args.libCeramicPaths
]
if args.target is not None:
opt.testBuildFlags += ["-target", args.target]
if args.testRoot is not None:
opt.testRoot = os.path.abspath(args.testRoot)
if args.testname is not None:
opt.runTestRoot = os.path.join(opt.testRoot, args.testname)
else:
opt.runTestRoot = opt.testRoot
if args.ceramicCompiler is not None:
opt.ceramicCompiler = os.path.abspath(args.ceramicCompiler)
else:
opt.ceramicCompiler = getCeramicCompiler(opt)
startTime = time.time()
opt.ceramicPlatform = getCeramicPlatform(opt)
failures = runTests(opt)
for f in opt.cleanUpLater:
try:
os.unlink(f)
except OSError:
print("warning: unable to clean up temporary file", f)
endTime = time.time()
print()
print("time-taken-seconds: %f" % (endTime - startTime))
sys.exit(failures > 0)
if __name__ == "__main__":
main()