33
33
import platform
34
34
import re
35
35
import shutil
36
+ import shlex
36
37
import sys
37
38
38
39
HPY_IMPORT_ORPHAN_BRANCH_NAME = "hpy-import"
@@ -555,25 +556,49 @@ def run_python_unittests(python_binary, args=None, paths=None, aot_compatible=Tr
555
556
556
557
args += [_graalpytest_driver (), "-v" ]
557
558
558
- agent_args = " " .join (mx_gate .get_jacoco_agent_args () or [])
559
+ agent_args = ' ' .join (shlex . quote ( arg ) for arg in mx_gate .get_jacoco_agent_args () or [])
559
560
if agent_args :
560
- # if we leave the excludes, the string is too long and it will be ignored by
561
- # the JVM, which ignores JAVA_TOOL_OPTIONS long than 1024 chars silently on JDK8
562
- agent_args = re .sub ("excludes=[^,]+," , "" , agent_args )
563
- # we know these can be excluded
564
- agent_args += ",excludes=*NodeGen*:*LibraryGen*:*BuiltinsFactory*"
565
- assert len (agent_args ) < 1024
561
+ # We need to make sure the arguments get passed to subprocesses, so we create a temporary launcher
562
+ # with the arguments
563
+ basedir = os .path .realpath (os .path .join (os .path .dirname (python_binary ), '..' ))
564
+ jacoco_basedir = "%s-jacoco" % basedir
565
+ shutil .rmtree (jacoco_basedir , ignore_errors = True )
566
+ shutil .copytree (basedir , jacoco_basedir , symlinks = True )
567
+ launcher_path = os .path .join (jacoco_basedir , 'bin' , 'graalpython' )
568
+ with open (launcher_path , 'r' , encoding = 'ascii' , errors = 'ignore' ) as launcher :
569
+ lines = launcher .readlines ()
570
+ assert re .match (r'^#!.*bash' , lines [0 ]), "jacoco needs a bash launcher"
571
+ lines .insert (- 1 , 'jvm_args+=(%s)\n ' % agent_args )
572
+ with open (launcher_path , 'w' ) as launcher :
573
+ launcher .writelines (lines )
566
574
# jacoco only dumps the data on exit, and when we run all our unittests
567
575
# at once it generates so much data we run out of heap space
568
- env ['JAVA_TOOL_OPTIONS' ] = agent_args
569
576
for testfile in testfiles :
570
- mx .run ([python_binary ] + args + [testfile ], nonZeroIsFatal = True , env = env )
577
+ mx .run ([launcher_path ] + args + [testfile ], nonZeroIsFatal = True , env = env )
571
578
else :
572
579
args += testfiles
573
580
mx .logv (" " .join ([python_binary ] + args ))
574
581
return mx .run ([python_binary ] + args , nonZeroIsFatal = True , env = env )
575
582
576
583
584
+ def run_tagged_unittests (python_binary , env = None ):
585
+ if env is None :
586
+ env = os .environ
587
+ env = env .copy ()
588
+ env .update (
589
+ ENABLE_CPYTHON_TAGGED_UNITTESTS = "true" ,
590
+ ENABLE_THREADED_GRAALPYTEST = "true" ,
591
+ PYTHONPATH = os .path .join (_dev_pythonhome (), 'lib-python/3' ),
592
+ )
593
+ run_python_unittests (
594
+ python_binary ,
595
+ args = ["-v" ,
596
+ "--python.WithThread=true" ],
597
+ paths = ["test_tagged_unittests.py" ],
598
+ env = env ,
599
+ )
600
+
601
+
577
602
def graalpython_gate_runner (args , tasks ):
578
603
# JUnit tests
579
604
with Task ('GraalPython JUnit' , tasks , tags = [GraalPythonTags .junit ]) as task :
@@ -613,19 +638,7 @@ def graalpython_gate_runner(args, tasks):
613
638
614
639
with Task ('GraalPython Python tests' , tasks , tags = [GraalPythonTags .tagged ]) as task :
615
640
if task :
616
- env = os .environ .copy ()
617
- env .update (
618
- ENABLE_CPYTHON_TAGGED_UNITTESTS = "true" ,
619
- ENABLE_THREADED_GRAALPYTEST = "true" ,
620
- PYTHONPATH = os .path .join (_dev_pythonhome (), 'lib-python/3' ),
621
- )
622
- run_python_unittests (
623
- python_gvm (),
624
- args = ["-v" ,
625
- "--python.WithThread=true" ],
626
- paths = ["test_tagged_unittests.py" ],
627
- env = env ,
628
- )
641
+ run_tagged_unittests (python_gvm ())
629
642
630
643
# Unittests on SVM
631
644
with Task ('GraalPython tests on SVM' , tasks , tags = [GraalPythonTags .svmunit ]) as task :
@@ -1469,11 +1482,7 @@ def python_coverage(args):
1469
1482
{"args" : []},
1470
1483
{"args" : ["--python.EmulateJython" ], "paths" : ["test_interop.py" ]},
1471
1484
# {"args": ["--llvm.managed"]},
1472
- {
1473
- "args" : ["-v" , "--python.WithThread=true" ],
1474
- "paths" : ["test_tagged_unittests.py" ],
1475
- "tagged" : True
1476
- },
1485
+ {"tagged" : True },
1477
1486
]
1478
1487
outputlcov = "coverage.lcov"
1479
1488
if os .path .exists (outputlcov ):
@@ -1487,20 +1496,19 @@ def python_coverage(args):
1487
1496
if os .path .exists (outfile ):
1488
1497
os .unlink (outfile )
1489
1498
extra_args = [
1499
+ "--experimental-options" ,
1490
1500
"--coverage" ,
1491
1501
"--coverage.TrackInternal" ,
1492
- "--coverage.FilterFile=%s/* .%s" % ( prefix , pattern ) ,
1502
+ "--coverage.FilterFile=*/lib-*/* .%s" % pattern ,
1493
1503
"--coverage.Output=lcov" ,
1494
1504
"--coverage.OutputFile=%s" % outfile ,
1495
1505
]
1496
- with set_env (GRAAL_PYTHON_ARGS = " " .join (extra_args )):
1497
- with _dev_pythonhome_context (): # run all our tests in the dev-home, so that lcov has consistent paths
1498
- kwds ["args" ].append ("--python.CAPI=" + _get_capi_home ())
1499
- if kwds .pop ("tagged" , False ):
1500
- with set_env (ENABLE_CPYTHON_TAGGED_UNITTESTS = "true" , ENABLE_THREADED_GRAALPYTEST = "true" ):
1501
- run_python_unittests (executable , ** kwds )
1502
- else :
1503
- run_python_unittests (executable , ** kwds )
1506
+ env = os .environ .copy ()
1507
+ env ['GRAAL_PYTHON_ARGS' ] = " " .join (extra_args )
1508
+ if kwds .pop ("tagged" , False ):
1509
+ run_tagged_unittests (executable , env = env )
1510
+ else :
1511
+ run_python_unittests (executable , env = env , ** kwds )
1504
1512
1505
1513
# generate a synthetic lcov file that includes all sources with 0
1506
1514
# coverage. this is to ensure all sources actuall show up - otherwise,
@@ -1517,7 +1525,7 @@ def python_coverage(args):
1517
1525
for filename in filenames:
1518
1526
if filename.endswith(".py"):
1519
1527
fullname = os.path.join(dirpath, filename)
1520
- with open(fullname, 'r ') as f:
1528
+ with open(fullname, 'rb ') as f:
1521
1529
try:
1522
1530
compile(f.read(), fullname, 'exec')
1523
1531
except BaseException as e:
@@ -1537,9 +1545,16 @@ def python_coverage(args):
1537
1545
f .name
1538
1546
])
1539
1547
1548
+ home_launcher = os .path .join (os .path .dirname (os .path .dirname (executable )), 'jre/languages/python' )
1540
1549
# merge all generated lcov files
1541
1550
for f in os .listdir (SUITE .dir ):
1542
- if f .endswith (".lcov" ):
1551
+ if f .endswith (".lcov" ) and os .path .getsize (f ):
1552
+ # Normalize lib-graalpython path
1553
+ with open (f ) as lcov_file :
1554
+ lcov = lcov_file .read ()
1555
+ lcov = lcov .replace (home_launcher , prefix )
1556
+ with open (f , 'w' ) as lcov_file :
1557
+ lcov_file .write (lcov )
1543
1558
cmdargs += ["-a" , f ]
1544
1559
1545
1560
mx .run (cmdargs )
0 commit comments