From 9ea1eecda847d3110804bb04260fce4f843f7aa9 Mon Sep 17 00:00:00 2001 From: Bastien Abadie Date: Wed, 29 Jan 2020 10:46:57 +0100 Subject: [PATCH 1/5] report: Use grcov html output, fixes #244 --- report/firefox_code_coverage/codecoverage.py | 68 ++++---------------- 1 file changed, 12 insertions(+), 56 deletions(-) diff --git a/report/firefox_code_coverage/codecoverage.py b/report/firefox_code_coverage/codecoverage.py index 123c0ee8c..5fb3df0e2 100644 --- a/report/firefox_code_coverage/codecoverage.py +++ b/report/firefox_code_coverage/codecoverage.py @@ -246,12 +246,19 @@ def generate_report(grcov_path, output_format, output_path, artifact_paths): i += 1 time.sleep(60) mod_env["PATH"] = one_click_loaner_gcc + ":" + mod_env["PATH"] - fout = open(output_path, "w") - cmd = [grcov_path, "-t", output_format, "-p", "/home/worker/workspace/build/src/"] + cmd = [ + grcov_path, + "-t", + output_format, + "-p", + "/home/worker/workspace/build/src/", + "-o", + output_path, + ] if output_format in ["coveralls", "coveralls+"]: cmd += ["--token", "UNUSED", "--commit-sha", "UNUSED"] cmd.extend(artifact_paths) - proc = subprocess.Popen(cmd, stdout=fout, stderr=subprocess.PIPE, env=mod_env) + proc = subprocess.Popen(cmd, stderr=subprocess.PIPE) i = 0 while proc.poll() is None: if i % 60 == 0: @@ -265,40 +272,6 @@ def generate_report(grcov_path, output_format, output_path, artifact_paths): raise Exception("Error while running grcov: {}\n".format(proc.stderr.read())) -def generate_html_report( - src_dir, - info_file=os.path.join(os.getcwd(), "output.info"), - output_dir=os.path.join(os.getcwd(), "report"), - silent=False, - style_file=None, -): - cwd = os.getcwd() - os.chdir(src_dir) - - with open(os.devnull, "w") as fnull: - command = [ - os.path.join(cwd, "lcov-bin/usr/local/bin/genhtml"), - "-o", - output_dir, - "--show-details", - "--highlight", - "--ignore-errors", - "source", - "--legend", - info_file, - ] - if style_file is not None: - command += ["--css-file", style_file] - ret = subprocess.call( - command, stdout=fnull if silent else None, stderr=fnull if silent else None - ) - - if ret != 0: - raise Exception("Error while running genhtml.") - - os.chdir(cwd) - - def download_grcov(): local_path = "grcov" local_version = "grcov_ver" @@ -337,21 +310,6 @@ def download_grcov(): return local_path -def download_genhtml(): - if os.path.isdir("lcov"): - os.chdir("lcov") - subprocess.check_call(["git", "pull"]) - else: - subprocess.check_call( - ["git", "clone", "https://github.com/linux-test-project/lcov.git"] - ) - os.chdir("lcov") - - subprocess.check_call(["make", "install", "DESTDIR=../lcov-bin"]) - - os.chdir("..") - - def main(): parser = argparse.ArgumentParser() @@ -468,10 +426,8 @@ def main(): ) ) else: - generate_report(grcov_path, "lcov", "output.info", artifact_paths) - - download_genhtml() - generate_html_report(os.path.abspath(args.src_dir)) + report_dir = tempfile.mkdtemp(suffix="html-report") + generate_report(grcov_path, "html", report_dir, artifact_paths) if __name__ == "__main__": From 4606c735d7e11d21000112930e34ee90fbb72f7b Mon Sep 17 00:00:00 2001 From: Bastien Abadie Date: Wed, 29 Jan 2020 13:33:09 +0100 Subject: [PATCH 2/5] Still use env --- report/firefox_code_coverage/codecoverage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report/firefox_code_coverage/codecoverage.py b/report/firefox_code_coverage/codecoverage.py index 5fb3df0e2..66daeb120 100644 --- a/report/firefox_code_coverage/codecoverage.py +++ b/report/firefox_code_coverage/codecoverage.py @@ -258,7 +258,7 @@ def generate_report(grcov_path, output_format, output_path, artifact_paths): if output_format in ["coveralls", "coveralls+"]: cmd += ["--token", "UNUSED", "--commit-sha", "UNUSED"] cmd.extend(artifact_paths) - proc = subprocess.Popen(cmd, stderr=subprocess.PIPE) + proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, env=mod_env) i = 0 while proc.poll() is None: if i % 60 == 0: From cfb52d8adee7ba550e9445ddf3bff926ffcf9f7a Mon Sep 17 00:00:00 2001 From: Bastien Abadie Date: Wed, 29 Jan 2020 13:34:34 +0100 Subject: [PATCH 3/5] Fix unit tests --- report/tests/test.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/report/tests/test.py b/report/tests/test.py index e16aa0186..eecaa184f 100644 --- a/report/tests/test.py +++ b/report/tests/test.py @@ -84,9 +84,9 @@ def test(self): codecoverage.generate_report("./grcov", "lcov", "output.info", artifact_paths) self.assertTrue(os.path.exists("output.info")) - codecoverage.download_genhtml() - codecoverage.generate_html_report("tests", silent=True) - self.assertTrue(os.path.isdir("report")) + codecoverage.generate_report("./grcov", "html", "report_html", artifact_paths) + self.assertTrue(os.path.isdir("report_html")) + self.assertListEqual(os.listdir("report_html"), ["index.html", "grcov.css"]) def test_suite_name_from_task_name(self): cases = [ @@ -142,13 +142,6 @@ def test_download_grcov(self): with open("grcov_ver", "r") as f: self.assertEqual(ver, f.read()) - def test_download_genhtml(self): - codecoverage.download_genhtml() - self.assertTrue(os.path.exists("./lcov-bin/usr/local/bin/genhtml")) - - codecoverage.download_genhtml() - self.assertTrue(os.path.exists("./lcov-bin/usr/local/bin/genhtml")) - if __name__ == "__main__": unittest.main() From a85428029cac355634b1d9f1605db8d4ab2d85df Mon Sep 17 00:00:00 2001 From: Bastien Abadie Date: Wed, 29 Jan 2020 13:37:02 +0100 Subject: [PATCH 4/5] Fix unit tests again --- report/tests/test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/report/tests/test.py b/report/tests/test.py index eecaa184f..0682c3325 100644 --- a/report/tests/test.py +++ b/report/tests/test.py @@ -86,7 +86,8 @@ def test(self): codecoverage.generate_report("./grcov", "html", "report_html", artifact_paths) self.assertTrue(os.path.isdir("report_html")) - self.assertListEqual(os.listdir("report_html"), ["index.html", "grcov.css"]) + self.assertTrue(os.path.exists("report_html/index.html")) + self.assertTrue(os.path.exists("report_html/grcov.css")) def test_suite_name_from_task_name(self): cases = [ From ae8b1e09f52350f9cf06b267ad19acae8e950056 Mon Sep 17 00:00:00 2001 From: Bastien Abadie Date: Wed, 29 Jan 2020 13:45:51 +0100 Subject: [PATCH 5/5] Add an option for the output directory --- report/firefox_code_coverage/codecoverage.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/report/firefox_code_coverage/codecoverage.py b/report/firefox_code_coverage/codecoverage.py index 66daeb120..4cc6d9498 100644 --- a/report/firefox_code_coverage/codecoverage.py +++ b/report/firefox_code_coverage/codecoverage.py @@ -376,8 +376,16 @@ def main(): action="store_true", help="Only generate high-level stats, not a full HTML report", ) + parser.add_argument( + "-o", + "--output-dir", + help="The output directory for generated report", + default=os.path.join(os.getcwd(), "ccov-report"), + ) args = parser.parse_args() + os.makedirs(args.output_dir, exist_ok=True) + if (args.branch is None) != (args.commit is None): parser.print_help() return @@ -402,9 +410,10 @@ def main(): grcov_path = download_grcov() if args.stats: - generate_report(grcov_path, "coveralls", "output.json", artifact_paths) + output = os.path.join(args.output_dir, "output.json") + generate_report(grcov_path, "coveralls", output, artifact_paths) - with open("output.json", "r") as f: + with open(output, "r") as f: report = json.load(f) total_lines = 0 @@ -426,8 +435,7 @@ def main(): ) ) else: - report_dir = tempfile.mkdtemp(suffix="html-report") - generate_report(grcov_path, "html", report_dir, artifact_paths) + generate_report(grcov_path, "html", args.output_dir, artifact_paths) if __name__ == "__main__":