spacepackets/coverage.py

55 lines
1.6 KiB
Python
Raw Permalink Normal View History

2023-12-01 10:22:44 +01:00
#!/usr/bin/env python3
import os
2023-12-01 16:00:29 +01:00
import logging
2023-12-01 10:22:44 +01:00
import argparse
import webbrowser
2023-12-01 16:00:29 +01:00
_LOGGER = logging.getLogger()
2023-12-01 17:19:58 +01:00
def generate_cov_report(open_report: bool, format: str):
2023-12-01 16:00:29 +01:00
logging.basicConfig(level=logging.INFO)
2023-12-01 10:22:44 +01:00
os.environ["RUSTFLAGS"] = "-Cinstrument-coverage"
os.environ["LLVM_PROFILE_FILE"] = "target/coverage/%p-%m.profraw"
2023-12-01 16:00:29 +01:00
_LOGGER.info("Executing tests with coverage")
2023-12-06 01:12:33 +01:00
os.system("cargo test --all-features")
2023-12-01 17:19:58 +01:00
out_path = "./target/debug/coverage"
if format == "lcov":
out_path = "./target/debug/lcov.info"
2023-12-01 10:22:44 +01:00
os.system(
2023-12-01 17:19:58 +01:00
f"grcov . -s . --binary-path ./target/debug/ -t {format} --branch --ignore-not-existing "
f"-o {out_path}"
2023-12-01 10:22:44 +01:00
)
2023-12-01 17:19:58 +01:00
if format == "lcov":
os.system(
"genhtml -o ./target/debug/coverage/ --show-details --highlight --ignore-errors source "
"--legend ./target/debug/lcov.info"
)
2023-12-01 10:22:44 +01:00
if open_report:
coverage_report_path = os.path.abspath("./target/debug/coverage/index.html")
webbrowser.open_new_tab(coverage_report_path)
2023-12-01 16:00:29 +01:00
_LOGGER.info("Done")
2023-12-01 10:22:44 +01:00
def main():
2023-12-01 15:35:09 +01:00
parser = argparse.ArgumentParser(
description="Generate coverage report and optionally open it in a browser"
)
parser.add_argument(
"--open", action="store_true", help="Open the coverage report in a browser"
)
2023-12-01 17:19:58 +01:00
parser.add_argument(
"--format",
choices=["html", "lcov"],
default="html",
help="Choose report format (html or lcov)",
)
2023-12-01 10:22:44 +01:00
args = parser.parse_args()
2023-12-01 17:19:58 +01:00
generate_cov_report(args.open, args.format)
2023-12-01 10:22:44 +01:00
if __name__ == "__main__":
main()