7
7
This script will also generate a JUnit XML file, which can be used to integrate
8
8
with CI/CD pipelines.
9
9
10
- Usage: test.py [-h] [-m] [-s] [--version] [--no_clean | --coverage] [dir]
10
+ Usage: test.py [-h] [-m] [-s] [--version] [--no_clean] [ --coverage] [dir]
11
11
12
12
Example usage: scripts/test.py compiler_tests/_example
13
13
32
32
from pathlib import Path
33
33
from concurrent .futures import ThreadPoolExecutor , as_completed
34
34
from typing import List , Optional
35
+ from http .server import HTTPServer , SimpleHTTPRequestHandler
35
36
36
37
37
38
RED = "\033 [31m"
@@ -321,29 +322,56 @@ def clean() -> bool:
321
322
return False
322
323
return True
323
324
324
- def make (with_coverage : bool , silent : bool ) -> bool :
325
+ def make (silent : bool ) -> bool :
325
326
"""
326
327
Wrapper for make bin/c_compiler.
327
328
328
329
Return True if successful, False otherwise
329
330
"""
330
331
print (GREEN + "Running make..." + RESET )
332
+ return_code , error_msg , _ = run_subprocess (
333
+ cmd = ["make" , "-C" , PROJECT_LOCATION , "bin/c_compiler" ], timeout = BUILD_TIMEOUT_SECONDS , silent = silent
334
+ )
335
+ if return_code != 0 :
336
+ print (RED + "Error when making:" , error_msg + RESET )
337
+ return False
331
338
332
- cmd = ["make" , "-C" , PROJECT_LOCATION , "bin/c_compiler" ]
333
- if with_coverage :
334
- # Run coverage if needed
335
- print (GREEN + "Making with coverage..." + RESET )
336
- shutil .rmtree (COVERAGE_FOLDER , ignore_errors = True )
337
- cmd = ["make" , "-C" , PROJECT_LOCATION , "with_coverage" ]
339
+ return True
338
340
339
- return_code , error_msg , _ = run_subprocess (cmd = cmd , timeout = BUILD_TIMEOUT_SECONDS , silent = silent )
341
+ def coverage () -> bool :
342
+ """
343
+ Wrapper for make coverage.
340
344
345
+ Return True if successful, False otherwise
346
+ """
347
+ print (GREEN + "Running make coverage..." + RESET )
348
+ return_code , error_msg , _ = run_subprocess (
349
+ cmd = ["make" , "-C" , PROJECT_LOCATION , "coverage" ], timeout = BUILD_TIMEOUT_SECONDS , silent = True
350
+ )
341
351
if return_code != 0 :
342
- print (RED + "Error when making:" , error_msg + RESET )
352
+ print (RED + "Error when making coverage :" , error_msg + RESET )
343
353
return False
344
-
345
354
return True
346
355
356
+ def serve_coverage_forever (host : str , port : int ):
357
+ """
358
+ Starts a HTTP server which serves the coverage folder forever until Ctrl+C
359
+ is pressed.
360
+ """
361
+ class Handler (SimpleHTTPRequestHandler ):
362
+ def __init__ (self , * args , directory = None , ** kwargs ):
363
+ super ().__init__ (* args , directory = COVERAGE_FOLDER , ** kwargs )
364
+
365
+ def log_message (self , format , * args ):
366
+ pass
367
+
368
+ httpd = HTTPServer ((host , port ), Handler )
369
+ print (GREEN + "Serving coverage on" + RESET + f" http://{ host } :{ port } / ... (Ctrl+C to exit)" )
370
+ try :
371
+ httpd .serve_forever ()
372
+ except KeyboardInterrupt :
373
+ print (RED + "\n Server has been stopped!" + RESET )
374
+
347
375
def process_result (
348
376
result : Result ,
349
377
xml_file : JUnitXMLFile ,
@@ -402,7 +430,7 @@ def run_tests(args, xml_file: JUnitXMLFile):
402
430
print ("\n >> Test Summary: " + GREEN + f"{ passing } Passed, " + RED + f"{ total - passing } Failed" + RESET )
403
431
404
432
def parse_args ():
405
- """"
433
+ """
406
434
Wrapper for argument parsing.
407
435
"""
408
436
parser = argparse .ArgumentParser ()
@@ -433,16 +461,14 @@ def parse_args():
433
461
action = "version" ,
434
462
version = f"BetterTesting { __version__ } "
435
463
)
436
- # Coverage cannot be perfomed without rebuilding the compiler
437
- group = parser .add_mutually_exclusive_group (required = False )
438
- group .add_argument (
439
- "--no_clean" ,
464
+ parser .add_argument (
465
+ '--no_clean' ,
440
466
action = "store_true" ,
441
467
default = False ,
442
- help = "Do no clean the repository before testing. This will make it "
468
+ help = "Don't clean the repository before testing. This will make it "
443
469
"faster but it can be safer to clean if you have any compilation issues."
444
470
)
445
- group .add_argument (
471
+ parser .add_argument (
446
472
"--coverage" ,
447
473
action = "store_true" ,
448
474
default = False ,
@@ -461,12 +487,17 @@ def main():
461
487
# Clean the repo if required and exit if this fails.
462
488
exit (2 )
463
489
464
- if not make (with_coverage = args . coverage , silent = args .short ):
490
+ if not make (silent = args .short ):
465
491
exit (3 )
466
492
467
493
with JUnitXMLFile (J_UNIT_OUTPUT_FILE ) as xml_file :
468
494
run_tests (args , xml_file )
469
495
496
+ if args .coverage :
497
+ if not coverage ():
498
+ exit (4 )
499
+ serve_coverage_forever ('0.0.0.0' , 8000 )
500
+
470
501
if __name__ == "__main__" :
471
502
try :
472
503
main ()
0 commit comments