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
|
#!/usr/bin/env python
from drivers import make_gnatcoll, TESTSUITE_ROOT_DIR
from drivers.basic import BasicTestDriver
from drivers.build_and_run import BuildAndRunDriver
from drivers.build_run_diff import BuildRunDiffDriver
from drivers.json_validation import JSONValidationDriver
from drivers.data_validation import DataValidationDriver
from drivers.gnatcov import list_to_file, produce_report
from e3.testsuite import Testsuite
from e3.fs import mkdir, ls, find, rm
from e3.os.process import Run
import re
import sys
import os
import logging
class MyTestsuite(Testsuite):
enable_cross_support = True
tests_subdir = 'tests'
test_driver_map = {
'json_validation': JSONValidationDriver,
'data_validation': DataValidationDriver,
'default': BasicTestDriver,
'build_and_run': BuildAndRunDriver,
'build_run_diff': BuildRunDiffDriver,
}
def add_options(self, parser):
parser.add_argument(
'--gcov',
help="compute code coverage of GNATcoll with gcov",
default=False,
action="store_true")
parser.add_argument(
'--gnatcov',
help="compute code coverage of GNATcoll with GNATcoverage",
default=False,
action="store_true")
parser.add_argument(
'--valgrind',
help="check memory usage with Valgrind (memcheck tool)",
action="store_true")
parser.add_argument(
'--recompile',
help="recompile debug version of gnatcoll for testing",
default=False,
action="store_true")
def set_up(self):
self.env.gcov = self.main.args.gcov
self.env.gnatcov = self.main.args.gnatcov
self.env.valgrind = self.main.args.valgrind
# Reject incompatible options
incompatible = [name for name in ('gcov', 'gnatcov', 'valgrind')
if getattr(self.env, name)]
if len(incompatible) > 1:
raise RuntimeError('The following options are incompatible: {}'
.format(' '.join(incompatible)))
# If doing a gcov or GNATcoverage-based testsuite run, rebuild GNATcoll
# accordingly and update directories.
if self.env.gcov or self.env.gnatcov:
if self.env.gcov:
subdir = 'gcov'
else:
subdir = 'gnatcov'
work_dir = os.path.join(TESTSUITE_ROOT_DIR, subdir)
gpr_dir, src_dir, obj_dir = make_gnatcoll(
work_dir, gcov=self.env.gcov, gnatcov=self.env.gnatcov)
self.env.gnatcoll_gpr_dir = gpr_dir
self.env.gnatcoll_src_dir = src_dir
self.env.gnatcoll_obj_dir = obj_dir
if self.env.gnatcov:
# Prepare a directory to host checkpoint files. Make sure it's
# empty so that we don't re-use coverage data from previous
# runs.
self.env.checkpoints_dir = os.path.join(
TESTSUITE_ROOT_DIR, 'gnatcov', 'checkpoints')
rm(self.env.checkpoints_dir, recursive=True)
mkdir(self.env.checkpoints_dir)
# Gather the list of ALI files
self.env.ali_files_list = os.path.join(
self.env.gnatcoll_obj_dir, 'ali_files.txt')
list_to_file(find(self.env.gnatcoll_obj_dir, '*.ali'),
self.env.ali_files_list)
else:
self.env.gnatcoll_gpr_dir = None
if self.main.args.recompile:
work_dir = os.path.join(TESTSUITE_ROOT_DIR, 'debug')
gpr_dir, _, _ = make_gnatcoll(work_dir, debug=True)
self.env.gnatcoll_debug_gpr_dir = gpr_dir
if self.env.gnatcoll_gpr_dir is None:
self.env.gnatcoll_gpr_dir = gpr_dir
else:
self.env.gnatcoll_debug_gpr_dir = None
def tear_down(self):
wd = TESTSUITE_ROOT_DIR
# If requested, produce coverage reports
if self.env.gcov:
# We need to call gcov on gcda present both in gnatcoll itself and
# tests (for generics coverage).
gcda_files = \
find(os.path.join(self.env.gnatcoll_obj_dir), '*.gcda') + \
find(os.path.join(self.env.working_dir), '*.gcda')
mkdir(os.path.join(wd, 'gcov', 'results'))
gcr = os.path.join(wd, 'gcov', 'results')
Run(['gcov'] + gcda_files,
cwd=os.path.join(wd, 'gcov', 'results'))
total_sources = 0
total_covered = 0
for source_file in ls(
os.path.join(self.env.gnatcoll_src_dir, '*')):
base_file = os.path.basename(source_file)
if not os.path.isfile(os.path.join(gcr, base_file + '.gcov')):
total = 1
covered = 0
with open(source_file) as fd:
total = len([line for line in fd
if line.strip() and
not re.match(r' *--', line)])
else:
with open(os.path.join(gcr, base_file + '.gcov')) as fd:
total = 0
covered = 0
for line in fd:
if re.match(r' *-:', line):
pass
elif re.match(r' *[#=]{5}:', line):
total += 1
else:
total += 1
covered += 1
total_sources += total
total_covered += covered
logging.info('%6.2f %% %8d/%-8d %s',
float(covered) * 100.0 / float(total),
covered,
total,
os.path.basename(source_file))
logging.info('%6.2f %% %8d/%-8d %s',
float(total_covered) * 100.0 / float(total_sources),
total_covered,
total_sources,
'TOTAL')
elif self.env.gnatcov:
checkpoint_list = os.path.join(wd, 'gnatcov', 'checkpoints',
'checkpoint_list.txt')
list_to_file(find(self.env.checkpoints_dir, '*.ckpt'),
checkpoint_list)
produce_report(os.path.join(wd, 'gnatcov', 'results'),
checkpoint_list,
self.env.gnatcoll_src_dir)
super(MyTestsuite, self).tear_down()
@property
def default_driver(self):
return 'default'
if __name__ == '__main__':
# Export location of Python used to run the testsuite. Some tests need
# to easily locate it.
os.environ["PYTHON_EXEC_PATH"] = os.path.abspath(sys.executable)
suite = MyTestsuite(os.path.dirname(__file__))
sys.exit(suite.testsuite_main())
|