Add scripts to run code coverage

One script to run coverage for the whole smoke test suite,
the resulting file can be uploaded to codecov.io for consumption:
https://codecov.io/gh/denniskempin/crosvm

Another script to run tests + coverage for a single crate,
which is useful during development to keep track of coverage
while adding tests (IDEs can display the generated lcov.info file)

BUG=b:171082843
TEST=Manual testing of both scripts

Change-Id: I52384762400a146cb0e7deb3d54dccf59b6134db
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2492914
Reviewed-by: Zach Reizner <zachr@chromium.org>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Auto-Submit: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Dennis Kempin 2020-10-22 17:20:47 -07:00 committed by Commit Bot
parent 8ba5fc9819
commit 831b11e869
3 changed files with 76 additions and 0 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ target/
**/*.orig
**/Cargo.lock
!/Cargo.lock
lcov.info

36
bin/crate_coverage Executable file
View file

@ -0,0 +1,36 @@
#!/bin/bash
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Calculates coverage for the specified crate only
# Usage:
# $ ./bin/crate_coverage arch [additional arguments for cargo test]
# Requirements:
# $ rustup toolchain install nightly
# $ cargo install grcov rust-covfix
set -ex
cd "${0%/*}/../"
target_dir=$(
cargo metadata --no-deps --format-version 1 |
jq -r ".target_directory"
)
# Delete old coverage profiles
find "$target_dir/debug" -name "*.gcda" -delete
# Run test with coverage profiling
(cd $1 && CARGO_INCREMENTAL=0 \
RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off \
-Zpanic_abort_tests" \
cargo +nightly test "${@:2}")
# Calculate code coverage
grcov "$target_dir/debug" -s . \
--ignore "/*" --ignore-not-existing \
-t lcov --llvm --branch \
-o /tmp/lcov.info
# Apply code coverage fixes
rust-covfix /tmp/lcov.info >lcov.info

39
bin/smoke_test_coverage Executable file
View file

@ -0,0 +1,39 @@
#!/bin/bash
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Calculates test coverage of all unit tests in crosvm.
# Requirements:
# $ apt install jq
# $ rustup toolchain install nightly
# $ cargo install grcov rust-covfix
set -ex
cd "${0%/*}/../"
target_dir=$(
cargo metadata --no-deps --format-version 1 |
jq -r ".target_directory"
)
# Delete old coverage profiles
find "$target_dir/debug" -name "*.gcda" -delete
# Run --all tests with coverage profiling options enabled.
CARGO_INCREMENTAL=0 \
RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Coverflow-checks=off \
-Zpanic_abort_tests" \
cargo +nightly test \
--no-fail-fast --features default-no-sandbox,wl-dmabuf,gpu,tpm \
--all --exclude aarch64 \
-- \
--test-threads=1
# Calculate code coverage
grcov "$target_dir/debug" -s . \
--ignore "/*" --ignore-not-existing \
-t lcov --llvm --branch \
-o /tmp/lcov.info
# Apply code coverage fixes
rust-covfix /tmp/lcov.info >lcov.info