Skip to content

Commit f486cd3

Browse files
committed
tools: a few additional usability tweaks
1 parent fc25cca commit f486cd3

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

tools/generate_coverage.py

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Options(object):
5252
coverage_dir = ""
5353
build_dir = ""
5454
base_dir = ""
55+
with_examples = True
5556
tests = []
57+
example_tests = []
5658
tracefiles = []
5759
final_tracefile = ""
5860

@@ -98,13 +100,21 @@ def setup_and_init_options():
98100
" -Dpfasst_WITH_GCC_PROF=ON -Dpfasst_BUILD_TESTS=ON"
99101

100102
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=help_string)
101-
parser.add_argument('-d', '--build_dir', required=True,
103+
parser.add_argument('-d', '--build-dir', required=True,
102104
help="name of build directory containing a debug build with GCC and enabled profiling")
103105
parser.add_argument('-o', '--output', default='coverage',
104106
help="output directory for generated coverage report")
107+
parser.add_argument('--no-examples', default=False, action='store_true',
108+
help="whether to not run and include tests from the examples")
109+
parser.add_argument('--debug', default=False, action='store_true',
110+
help="enables more verbose debugging output")
105111

106112
_args = parser.parse_args()
107113

114+
if _args.debug:
115+
logging.getLogger().setLevel(logging.DEBUG)
116+
logging.debug("Debug mode enabled.")
117+
108118
get_project_root()
109119

110120
if not os.access(_args.build_dir, os.W_OK):
@@ -113,52 +123,63 @@ def setup_and_init_options():
113123
options.build_dir = os.path.abspath(_args.build_dir)
114124

115125
if not os.access(_args.output, os.W_OK):
116-
logging.info("output directory not found. creating: %s" % _args.output)
126+
logging.info("Output directory not found. Creating: %s" % _args.output)
117127
os.mkdir(_args.output)
118128
else:
119-
logging.warning("clearing out output directory: %s" % _args.output)
129+
logging.warning("Clearing out output directory: %s" % _args.output)
120130
shutil.rmtree(_args.output)
121131
os.mkdir(_args.output)
122132
options.coverage_dir = os.path.abspath(_args.output)
123133

134+
options.with_examples = not _args.no_examples
135+
if not options.with_examples:
136+
logging.debug("Not running and tracing tests from examples.")
137+
124138

125139
def get_test_directories():
126-
logging.info("looking for tests ...")
140+
logging.info("Looking for tests ...")
127141
for root, dirs, files in os.walk(options.build_dir + '/tests'):
128142
match_name = re.search('^.*/(?P<test_name>test_[a-zA-Z\-_]+)\.dir$', root)
129143
match_is_example = re.search('^.*/tests/examples/.*$', root)
130144
is_example = match_is_example is not None
131145
if match_name is not None:
132146
testname = match_name.groupdict()['test_name']
133-
options.tests.append({'path': root, 'name': testname, 'is_example': is_example})
134-
logging.info("%d tests found" % len(options.tests))
147+
if is_example:
148+
options.example_tests.append({'path': root, 'name': testname, 'is_example': is_example})
149+
else:
150+
options.tests.append({'path': root, 'name': testname, 'is_example': is_example})
151+
logging.info("%d tests found" % (len(options.tests) + len(options.example_tests)))
152+
logging.info(" %d general tests" % len(options.tests))
153+
if options.with_examples:
154+
logging.info(" %d tests for examples" % len(options.example_tests))
135155

136156

137157
def run_test(path, name, is_example):
138-
logging.info("running %s" % name)
139-
logging.debug("in %s" % path)
158+
logging.info("- %s" % name)
159+
logging.debug("Found in %s" % path)
140160
output_file = open('%s/%s.log' % (options.coverage_dir, name), mode='a')
161+
logging.debug("Output log: %s" % output_file.name)
141162

142163
os.chdir(os.path.abspath(path))
143-
logging.debug("deleting old tracing data ...")
164+
logging.debug("Deleting old tracing data ...")
144165
print('### deleting old tracing data ...', file=output_file, flush=True)
145166
sp.check_call('lcov --zerocounters --directory .', shell=True, stdout=output_file, stderr=output_file)
146167
print('### done.', file=output_file, flush=True)
147168

148169
os.chdir(options.build_dir)
149-
logging.debug("running test ...")
170+
logging.debug("Running test ...")
150171
print('### running test ...', file=output_file, flush=True)
151172
sp.check_call('ctest -R %s' % name, shell=True, stdout=output_file, stderr=output_file)
152173
print('### done.', file=output_file, flush=True)
153174

154175
os.chdir(os.path.abspath(path))
155-
logging.debug("capturing all tracing data ...")
176+
logging.debug("Capturing all tracing data ...")
156177
print('### capturing all tracing data ...', file=output_file, flush=True)
157178
sp.check_call('lcov --capture --directory . --output-file "%s.info.complete"' % name,
158179
shell=True, stdout=output_file, stderr=output_file)
159180
print('### done.', file=output_file, flush=True)
160181

161-
logging.debug("removing unnecessary data ...")
182+
logging.debug("Removing unnecessary data ...")
162183
print('### removing unnecessary data ...', file=output_file, flush=True)
163184
try:
164185
sp.check_call('lcov --remove "%s.info.complete" "%s/include/pfasst/easylogging++.h" --output-file %s.info.prelim'
@@ -168,7 +189,7 @@ def run_test(path, name, is_example):
168189
logging.warning(e)
169190
print('### done.', file=output_file, flush=True)
170191

171-
logging.debug("extracting interesting tracing data ...")
192+
logging.debug("Extracting interesting tracing data ...")
172193
print('### extracting interesting tracing data ...', file=output_file, flush=True)
173194
try:
174195
sp.check_call('lcov --extract "%s.info.prelim" "*%s/include/**/*" --output-file %s.info'
@@ -178,7 +199,7 @@ def run_test(path, name, is_example):
178199
except sp.CalledProcessError as e:
179200
logging.warning(e)
180201
if is_example:
181-
logging.debug("this test belongs to an example, thus also covering examples code")
202+
logging.debug("This test belongs to an example, thus also covering examples code")
182203
try:
183204
sp.check_call('lcov --extract "%s.info.prelim" "*%s/examples/**/*" --output-file %s.info.example'
184205
% (name, options.base_dir, name),
@@ -192,12 +213,23 @@ def run_test(path, name, is_example):
192213
output_file.close()
193214

194215

216+
def run_tests():
217+
logging.info("Running general tests ...")
218+
for test in options.tests:
219+
run_test(**test)
220+
if options.with_examples:
221+
logging.info("Running tests for examples ...")
222+
for example in options.example_tests:
223+
run_test(**example)
224+
225+
195226
def aggregate_tracefiles():
196-
logging.info("aggregating %d tracefiles ..." % len(options.tracefiles))
227+
logging.info("Aggregating %d tracefiles ..." % len(options.tracefiles))
197228
output_file = open('%s/aggegrating.log' % (options.coverage_dir,), mode='a')
229+
logging.debug("Output log: %s" % output_file.name)
198230
options.final_tracefile = "%s/all_tests.info" % options.coverage_dir
199231
for tracefile in options.tracefiles:
200-
logging.debug("adding tracefile: %s" % (tracefile))
232+
logging.debug("- %s" % (tracefile))
201233
print("### adding tracefile: %s" % (tracefile,), file=output_file, flush=True)
202234
if os.access(options.final_tracefile, os.W_OK):
203235
sp.check_call('lcov --add-tracefile "%s" --add-tracefile "%s" --output-file "%s"'
@@ -212,21 +244,21 @@ def aggregate_tracefiles():
212244

213245

214246
def generate_html():
215-
logging.info("generating HTML report ...")
247+
logging.info("Generating HTML report ...")
216248
output_file = open('%s/generate_html.log' % (options.coverage_dir,), mode='a')
217249
sp.check_call('genhtml --output-directory %s --demangle-cpp --num-spaces 2 --sort '
218250
'--title "PFASST++ Test Coverage" --prefix "%s" --function-coverage --legend "%s"'
219251
% (options.coverage_dir, options.base_dir, options.final_tracefile),
220252
shell=True, stdout=output_file, stderr=output_file)
221253
output_file.close()
222-
logging.info("coverage report can be found in: %s" % options.coverage_dir)
254+
logging.info("Coverage report can be found in: file://%s/index.html" % options.coverage_dir)
223255

224256

225257
if __name__ == "__main__":
226-
assert(is_lcov_available())
258+
if not is_lcov_available():
259+
raise RuntimeError("Required commands could not be found.")
227260
setup_and_init_options()
228261
get_test_directories()
229-
for test in options.tests:
230-
run_test(**test)
262+
run_tests()
231263
aggregate_tracefiles()
232264
generate_html()

0 commit comments

Comments
 (0)