From c64d8dc8aaed84755bc5bfae6551cfd0b5b7f2db Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Thu, 3 Dec 2020 13:36:40 -0800 Subject: [PATCH] switch to source based coverage (#1293) * switch to new llvm source based coverage * upload artifact and simplified * filter out irrelevant dependency coverage * enable the correct filters on coverage * correctly specify all binaries * remove sparse flag from coverage * update the coverage script organization * fix typo in coverage script --- .github/workflows/coverage.yml | 46 ++++++++++++++++++++++++++-------- .gitignore | 1 + zebra-utils/coverage | 34 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) create mode 100755 zebra-utils/coverage diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 10875a7e4..892f79d06 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -13,20 +13,46 @@ jobs: coverage: name: Coverage timeout-minutes: 30 - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly override: true - - name: Skip network tests on Ubuntu - # Ubuntu runners don't have network or DNS configured during test steps - run: echo "ZEBRA_SKIP_NETWORK_TESTS=1" >> $GITHUB_ENV - - name: Run cargo-tarpaulin - uses: actions-rs/tarpaulin@v0.1 + profile: minimal + components: llvm-tools-preview + + - name: Install rustfilt symbol demangler + run: | + cargo install rustfilt + + - name: Rerun tests for coverage + env: + RUSTFLAGS: -Zinstrument-coverage -C link-dead-code -C debuginfo=2 + LLVM_PROFILE_FILE: "${{ github.workspace }}/test.%p.profraw" + ZEBRA_SKIP_NETWORK_TESTS: 1 + run: | + cargo test + cargo test --no-run --message-format=json | jq -r "select(.profile.test == true) | .filenames[]" | grep -v dSYM - > filenames.txt + + - name: Merge coverage data + run: | + $(rustc --print target-libdir)/../bin/llvm-profdata merge test.*.profraw -o test.profdata + + - name: Generate detailed html coverage report for github artifact + run: | + $(rustc --print target-libdir)/../bin/llvm-cov show -format=html -ignore-filename-regex=".*/.cargo/registry/.*" -ignore-filename-regex=".*/.cargo/git/.*" -ignore-filename-regex=".*/.rustup/.*" -Xdemangler=rustfilt -show-instantiations -output-dir=./coverage -instr-profile=./test.profdata $(printf -- "-object %s " $(cat filenames.txt)) + + - uses: actions/upload-artifact@v2 with: - version: '0.16.0' - timeout: 600 + name: coverage + path: ./coverage + + - name: Generate lcov coverage report for codecov + run: | + $(rustc --print target-libdir)/../bin/llvm-cov export -format=lcov -instr-profile=test.profdata $(printf -- "-object %s " $(cat filenames.txt)) > "lcov.info" + - name: Upload coverage report to Codecov - uses: codecov/codecov-action@v1.0.15 + uses: codecov/codecov-action@v1 diff --git a/.gitignore b/.gitignore index 8be07b596..62d81cc24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Cargo files /target/ +/coverage-target/ # Firebase caches (?) .firebase/ # Emacs detritus diff --git a/zebra-utils/coverage b/zebra-utils/coverage new file mode 100755 index 000000000..8af3fdc16 --- /dev/null +++ b/zebra-utils/coverage @@ -0,0 +1,34 @@ +#! /bin/bash +set -e +set -o xtrace + +export CARGO_TARGET_DIR="coverage-target" +export ZEBRA_SKIP_NETWORK_TESTS=1 +export LLVM_PROFILE_FILE="${PWD}/$CARGO_TARGET_DIR/coverage/test.%m.profraw" +export RUSTFLAGS="-Zinstrument-coverage -C link-dead-code -C debuginfo=2" + +rm -rf ./"$CARGO_TARGET_DIR"/coverage +mkdir -p ./$CARGO_TARGET_DIR/coverage +cargo +nightly test +cargo +nightly test --no-run --message-format=json | jq -r "select(.profile.test == true) | .filenames[]" | grep -v dSYM - > ./$CARGO_TARGET_DIR/files.txt +$(rustc +nightly --print target-libdir)/../bin/llvm-profdata merge ./$CARGO_TARGET_DIR/coverage/test.*.profraw -o ./$CARGO_TARGET_DIR/coverage/test.profdata + +rm -rf ./$CARGO_TARGET_DIR/coverage/html/ + +$(rustc +nightly --print target-libdir)/../bin/llvm-cov show \ + -format=html \ + -Xdemangler=rustfilt \ + -show-instantiations \ + -output-dir=./$CARGO_TARGET_DIR/coverage/html \ + -ignore-filename-regex=".*/.cargo/registry/.*" \ + -ignore-filename-regex=".*/.cargo/git/.*" \ + -ignore-filename-regex=".*/.rustup/.*" \ + -instr-profile=./$CARGO_TARGET_DIR/coverage/test.profdata \ + $(printf -- "-object %s " $(cat ./$CARGO_TARGET_DIR/files.txt)) + +$(rustc +nightly --print target-libdir)/../bin/llvm-cov export \ + -format=lcov \ + -instr-profile=./$CARGO_TARGET_DIR/coverage/test.profdata \ + $(printf -- "-object %s " $(cat ./$CARGO_TARGET_DIR/files.txt)) > "./$CARGO_TARGET_DIR/coverage/lcov.info" + +xdg-open ./$CARGO_TARGET_DIR/coverage/html/index.html