diff --git a/server/.gitignore b/server/.gitignore index d98cbec7c..544282769 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -18,3 +18,6 @@ Cargo.lock Session.vim .env + +# grcov outputs +*.profraw diff --git a/server/README.md b/server/README.md index af1002d48..a097c8a6d 100644 --- a/server/README.md +++ b/server/README.md @@ -136,3 +136,9 @@ The easiest way to get these tests to pass is to: Alternatively, if you're only interested in running unit tests, you can just run `cargo test --lib`. These tests don't make any assumptions about the surrounding environment. To run only a specific test (e.g. only the application tests), you can use the `--test` flag to `cargo test` which supports common Unix glob patterns. For example: `cargo test --test '*app*'`. + +# Code Coverage +To get a code coverage report, first make sure you have the following installations: +- `cargo install grcov` +- `rustup component add llvm-tools-preview` +Then, run `./run-tests-with-grcov.sh`, which is a wrapper around `./run-tests.sh` that includes code coverage evaluation and report generation using `grcov`. When it finishes, it will print the location of the report HTML file to your shell. diff --git a/server/run-tests-with-grcov.sh b/server/run-tests-with-grcov.sh new file mode 100755 index 000000000..fd1574c40 --- /dev/null +++ b/server/run-tests-with-grcov.sh @@ -0,0 +1,19 @@ +#!/bin/sh -e + +# cleanly run the following commands in their own session +bash -c " +# tell Rust to run with coverage instrumentation +RUSTFLAGS=\"-Cinstrument-coverage\" +# give grcov a profile name template for output files +LLVM_PROFILE_FILE=\"svix-webhooks-%p-%m.profraw\" +# put the compiler in nightly mode +RUSTC_BOOTSTRAP=1 + +# run tests +./run-tests.sh +" || true + +# generate and open report output +grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing -o ./target/debug/coverage/ + +echo "coverage HTML file at $(pwd)/target/debug/coverage/index.html"