From 8142ae9c389095de9c3770ad18524a733b7df732 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 1 Dec 2023 22:21:12 +0100 Subject: [PATCH] add coverage --- README.md | 13 ++++++++++++ coverage.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 coverage.py diff --git a/README.md b/README.md index 0dd8276..172d0ce 100644 --- a/README.md +++ b/README.md @@ -43,3 +43,16 @@ Each project has its own `CHANGELOG.md`. packet protocol implementations. This repository is re-exported in the [`satrs-core`](https://egit.irs.uni-stuttgart.de/rust/satrs-launchpad/src/branch/main/satrs-core) crate. + +# Coverage + +Coverage was generated using [`grcov`](https://github.com/mozilla/grcov). If you have not done so +already, install the `llvm-tools-preview`: + +```sh +rustup component add llvm-tools-preview +cargo install grcov --locked +``` + +After that, you can simply run `coverage.py` to test the `satrs-core` crate with coverage. You can +optionally supply the `--open` flag to open the coverage report in your webbrowser. diff --git a/coverage.py b/coverage.py new file mode 100755 index 0000000..b7efbe9 --- /dev/null +++ b/coverage.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import os +import logging +import argparse +import webbrowser + + +_LOGGER = logging.getLogger() + + +def generate_cov_report(open_report: bool, format: str, package: str): + logging.basicConfig(level=logging.INFO) + os.environ["RUSTFLAGS"] = "-Cinstrument-coverage" + os.environ["LLVM_PROFILE_FILE"] = "target/coverage/%p-%m.profraw" + _LOGGER.info("Executing tests with coverage") + os.system(f"cargo test -p {package}") + + out_path = "./target/debug/coverage" + if format == "lcov": + out_path = "./target/debug/lcov.info" + os.system( + f"grcov . -s . --binary-path ./target/debug/ -t {format} --branch --ignore-not-existing " + f"-o {out_path}" + ) + if format == "lcov": + os.system( + "genhtml -o ./target/debug/coverage/ --show-details --highlight --ignore-errors source " + "--legend ./target/debug/lcov.info" + ) + if open_report: + coverage_report_path = os.path.abspath("./target/debug/coverage/index.html") + webbrowser.open_new_tab(coverage_report_path) + _LOGGER.info("Done") + + +def main(): + 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" + ) + parser.add_argument( + "-p", + "--package", + choices=["satrs-core"], + default="satrs-core", + help="Choose project to generate coverage for", + ) + parser.add_argument( + "--format", + choices=["html", "lcov"], + default="html", + help="Choose report format (html or lcov)", + ) + args = parser.parse_args() + generate_cov_report(args.open, args.format, args.package) + + +if __name__ == "__main__": + main() -- 2.43.0