From 25c56a7d98f3a9254b69fc002f3ec848922d2c0c Mon Sep 17 00:00:00 2001 From: zoowii Date: Thu, 15 May 2025 17:39:53 +0800 Subject: [PATCH] test: add cargo coverage test --- .gitignore | 3 +++ coverage.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 coverage.sh diff --git a/.gitignore b/.gitignore index 15cb1e1..4c95e87 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ /lib /venv /node_modules + +# coverage +*.profraw diff --git a/coverage.sh b/coverage.sh new file mode 100755 index 0000000..0594d7a --- /dev/null +++ b/coverage.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -e + +# Prerequisites: +# rustup component add llvm-tools-preview +# cargo install rustfilt +# cargo install grcov@0.5.1 +# or download grcov from https://github.com/mozilla/grcov/releases + +export RUSTC_BOOTSTRAP=1 + +# Create the directory for coverage reports +mkdir -p target/coverage + +# Clean any previous coverage data +rm -rf target/coverage/* + +# Clean previous build artifacts to ensure coverage instrumentation is applied +cargo clean +rm -f *.profraw + +export RUSTFLAGS="-Cinstrument-coverage" + +# Build the project with coverage instrumentation + +cargo build --verbose + +# Set environment variables for coverage +export CARGO_INCREMENTAL=0 +export RUSTFLAGS="-C instrument-coverage -Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort" +export RUSTDOCFLAGS="-Cpanic=abort" +export LLVM_PROFILE_FILE="target/coverage/coverage-%p-%m.profraw" + +# Run tests with coverage instrumentation +cargo test --no-fail-fast --verbose + +# List all the profraw files to verify they were created +echo "Generated profraw files:" +# ls -la target/coverage/ + +# Generate coverage report +# target/coverage +grcov . --binary-path ./target/debug -s . -t html --branch --ignore-not-existing --ignore "/*" --ignore "target/*" --ignore "tests/*" --ignore "examples/*" -o target/coverage/html + +# Generate a summary report +grcov . --binary-path ./target/debug -s . -t lcov --branch --ignore-not-existing --ignore "/*" --ignore "target/*" --ignore "tests/*" --ignore "examples/*" -o target/coverage/lcov.info + +# Print a message with the location of the coverage report +echo "Coverage report generated at target/coverage/html/index.html" + +ls -la *.profraw +rm -rf *.profraw + +# Start a local web server to view the coverage report +cd target/coverage/html && python3 -m http.server 8080 +# open http://localhost:8080/src/yul2ir/index.html