From ad39ccf6a5df06f3a6bc47687ffa680196432d44 Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Wed, 6 Dec 2023 08:48:01 +0100 Subject: [PATCH] Add coverage check --- .github/workflows/check.yml | 4 ++++ .gitignore | 1 + bin/check/coverage.sh | 11 +++++++++ bin/coverage_run.sh | 24 +++++++++++++++++++ bin/coverage_uncovered_lines.py | 42 +++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100755 bin/check/coverage.sh create mode 100755 bin/coverage_run.sh create mode 100755 bin/coverage_uncovered_lines.py diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index a49223b83b7..80134d60864 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -58,6 +58,10 @@ jobs: if: always() run: bin/check/rust_version.py 7 + - name: Coverage + if: always() + run: bin/check/coverage.sh + - name: Rustfmt if: always() run: bin/check/rustfmt.sh diff --git a/.gitignore b/.gitignore index 5898650a051..ede0f9a82fe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.swp *.swo *.pyc +*.profraw .mypy_cache/ target/ diff --git a/bin/check/coverage.sh b/bin/check/coverage.sh new file mode 100755 index 00000000000..70d5f8e013f --- /dev/null +++ b/bin/check/coverage.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -Eeuo pipefail +bin/install_prerequisites_ubuntu.sh +bin/install_grcov.sh +bin/test/test_prerequisites.sh +bin/coverage_run.sh +lines=$(bin/coverage_uncovered_lines.py packages/hurlfmt/src/format/json.rs) +if [ -n "$lines" ]; then + echo "$lines" + exit 1 +fi diff --git a/bin/coverage_run.sh b/bin/coverage_run.sh new file mode 100755 index 00000000000..2f7f0120dcf --- /dev/null +++ b/bin/coverage_run.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -Eeuo pipefail + +rm -rf target/profile +rm -rf target/coverage +cargo clean + +RUSTFLAGS="-Cinstrument-coverage" +export RUSTFLAGS +LLVM_PROFILE_FILE="$(pwd)/target/profile/test-integ-%p-%m.profraw" +export LLVM_PROFILE_FILE + +cargo build +PATH=$(pwd)/target/debug:$PATH +export PATH +bin/test/test_integ.sh +grcov target/profile \ + --binary-path target/debug \ + --source-dir . \ + --output-types html \ + --branch \ + --ignore-not-existing \ + --output-path target/coverage + diff --git a/bin/coverage_uncovered_lines.py b/bin/coverage_uncovered_lines.py new file mode 100755 index 00000000000..0e70018d92f --- /dev/null +++ b/bin/coverage_uncovered_lines.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +import sys +from bs4 import BeautifulSoup + +COVERAGE_DIR = "target/coverage" + + +def uncovered_lines(src_file): + html_file = COVERAGE_DIR + "/" + src_file + ".html" + sys.stderr.write(html_file + "\n") + html = open(html_file).read() + soup = BeautifulSoup(html, "html.parser") + elements = soup.select('div[role="row"]') + lines = [] + for element in elements: + line = parse_row(element) + if line is not None: + lines.append(line) + return lines + + +def parse_row(element): + uncovered = element.select(".has-background-danger-light") + if len(uncovered) > 0: + line_number = element.select("div:first-child")[0]["id"] + line = uncovered[0].select("pre")[0].text + return line_number, line + return None + + +def main(): + sys.stderr.write("Extracting uncovered lines\n") + for src_file in sys.argv[1:]: + lines = uncovered_lines(src_file) + if len(lines) > 0: + print(src_file) + for line_number, line in lines: + print("%s %s" % (line_number, line)) + + +if __name__ == "__main__": + main()