Skip to content

Commit 7759a20

Browse files
authored
report: Use grcov html output, fixes #244 (#393)
1 parent 96c7b81 commit 7759a20

File tree

2 files changed

+26
-68
lines changed

2 files changed

+26
-68
lines changed

report/firefox_code_coverage/codecoverage.py

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,19 @@ def generate_report(grcov_path, output_format, output_path, artifact_paths):
246246
i += 1
247247
time.sleep(60)
248248
mod_env["PATH"] = one_click_loaner_gcc + ":" + mod_env["PATH"]
249-
fout = open(output_path, "w")
250-
cmd = [grcov_path, "-t", output_format, "-p", "/home/worker/workspace/build/src/"]
249+
cmd = [
250+
grcov_path,
251+
"-t",
252+
output_format,
253+
"-p",
254+
"/home/worker/workspace/build/src/",
255+
"-o",
256+
output_path,
257+
]
251258
if output_format in ["coveralls", "coveralls+"]:
252259
cmd += ["--token", "UNUSED", "--commit-sha", "UNUSED"]
253260
cmd.extend(artifact_paths)
254-
proc = subprocess.Popen(cmd, stdout=fout, stderr=subprocess.PIPE, env=mod_env)
261+
proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, env=mod_env)
255262
i = 0
256263
while proc.poll() is None:
257264
if i % 60 == 0:
@@ -265,40 +272,6 @@ def generate_report(grcov_path, output_format, output_path, artifact_paths):
265272
raise Exception("Error while running grcov: {}\n".format(proc.stderr.read()))
266273

267274

268-
def generate_html_report(
269-
src_dir,
270-
info_file=os.path.join(os.getcwd(), "output.info"),
271-
output_dir=os.path.join(os.getcwd(), "report"),
272-
silent=False,
273-
style_file=None,
274-
):
275-
cwd = os.getcwd()
276-
os.chdir(src_dir)
277-
278-
with open(os.devnull, "w") as fnull:
279-
command = [
280-
os.path.join(cwd, "lcov-bin/usr/local/bin/genhtml"),
281-
"-o",
282-
output_dir,
283-
"--show-details",
284-
"--highlight",
285-
"--ignore-errors",
286-
"source",
287-
"--legend",
288-
info_file,
289-
]
290-
if style_file is not None:
291-
command += ["--css-file", style_file]
292-
ret = subprocess.call(
293-
command, stdout=fnull if silent else None, stderr=fnull if silent else None
294-
)
295-
296-
if ret != 0:
297-
raise Exception("Error while running genhtml.")
298-
299-
os.chdir(cwd)
300-
301-
302275
def download_grcov():
303276
local_path = "grcov"
304277
local_version = "grcov_ver"
@@ -337,21 +310,6 @@ def download_grcov():
337310
return local_path
338311

339312

340-
def download_genhtml():
341-
if os.path.isdir("lcov"):
342-
os.chdir("lcov")
343-
subprocess.check_call(["git", "pull"])
344-
else:
345-
subprocess.check_call(
346-
["git", "clone", "https://github.com/linux-test-project/lcov.git"]
347-
)
348-
os.chdir("lcov")
349-
350-
subprocess.check_call(["make", "install", "DESTDIR=../lcov-bin"])
351-
352-
os.chdir("..")
353-
354-
355313
def main():
356314
parser = argparse.ArgumentParser()
357315

@@ -418,8 +376,16 @@ def main():
418376
action="store_true",
419377
help="Only generate high-level stats, not a full HTML report",
420378
)
379+
parser.add_argument(
380+
"-o",
381+
"--output-dir",
382+
help="The output directory for generated report",
383+
default=os.path.join(os.getcwd(), "ccov-report"),
384+
)
421385
args = parser.parse_args()
422386

387+
os.makedirs(args.output_dir, exist_ok=True)
388+
423389
if (args.branch is None) != (args.commit is None):
424390
parser.print_help()
425391
return
@@ -444,9 +410,10 @@ def main():
444410
grcov_path = download_grcov()
445411

446412
if args.stats:
447-
generate_report(grcov_path, "coveralls", "output.json", artifact_paths)
413+
output = os.path.join(args.output_dir, "output.json")
414+
generate_report(grcov_path, "coveralls", output, artifact_paths)
448415

449-
with open("output.json", "r") as f:
416+
with open(output, "r") as f:
450417
report = json.load(f)
451418

452419
total_lines = 0
@@ -468,10 +435,7 @@ def main():
468435
)
469436
)
470437
else:
471-
generate_report(grcov_path, "lcov", "output.info", artifact_paths)
472-
473-
download_genhtml()
474-
generate_html_report(os.path.abspath(args.src_dir))
438+
generate_report(grcov_path, "html", args.output_dir, artifact_paths)
475439

476440

477441
if __name__ == "__main__":

report/tests/test.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ def test(self):
8484
codecoverage.generate_report("./grcov", "lcov", "output.info", artifact_paths)
8585
self.assertTrue(os.path.exists("output.info"))
8686

87-
codecoverage.download_genhtml()
88-
codecoverage.generate_html_report("tests", silent=True)
89-
self.assertTrue(os.path.isdir("report"))
87+
codecoverage.generate_report("./grcov", "html", "report_html", artifact_paths)
88+
self.assertTrue(os.path.isdir("report_html"))
89+
self.assertTrue(os.path.exists("report_html/index.html"))
90+
self.assertTrue(os.path.exists("report_html/grcov.css"))
9091

9192
def test_suite_name_from_task_name(self):
9293
cases = [
@@ -142,13 +143,6 @@ def test_download_grcov(self):
142143
with open("grcov_ver", "r") as f:
143144
self.assertEqual(ver, f.read())
144145

145-
def test_download_genhtml(self):
146-
codecoverage.download_genhtml()
147-
self.assertTrue(os.path.exists("./lcov-bin/usr/local/bin/genhtml"))
148-
149-
codecoverage.download_genhtml()
150-
self.assertTrue(os.path.exists("./lcov-bin/usr/local/bin/genhtml"))
151-
152146

153147
if __name__ == "__main__":
154148
unittest.main()

0 commit comments

Comments
 (0)