From 704a5e9a41e86191192881a9d4915bae534ccd5e Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 15:35:25 +0300 Subject: [PATCH] Collect and upload code coverage when executing unit tests --- .github/workflows/ci.yml | 65 ++++++++++++++++++++++++++++++++++++++-- .lycheeignore | 1 + 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 .lycheeignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1375622..639f471 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -80,6 +80,8 @@ jobs: RUSTFLAGS="-D warnings" cargo check --release unit-test: + permissions: + pull-requests: write runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 @@ -93,13 +95,70 @@ jobs: uses: actions-rs/toolchain@v1 with: toolchain: ${{ env.RUSTC_VERSION }} + components: llvm-tools-preview profile: minimal override: true - uses: Swatinem/rust-cache@v2 - - name: Run cargo test - uses: actions-rs/cargo@v1 + - name: Install cargo-llvm-cov + uses: taiki-e/install-action@v2.27.14 + with: + tool: cargo-llvm-cov + + - name: Execute tests + id: coverage env: CRUNCH_CONFIG_FILENAME: environments/cc3/testnet/.env + run: | + PR_NUMBER=$(echo "$GITHUB_REF" | sed "s|refs/pull/||" | sed "s|/merge||") + DESTINATION_PATH="crunch/PR-$PR_NUMBER" + export DESTINATION_PATH + echo "**For full LLVM coverage report [click here](https://staticsitellvmhtml.z13.web.core.windows.net/$DESTINATION_PATH/html/)!**" > uncovered-lines.log + + cargo llvm-cov --workspace --html --show-missing-lines \ + --hide-instantiations --ignore-filename-regex "(tests.rs|mock.rs)" + + UNCOVERED_LINES=$(sed "s|$(pwd)|.|" uncovered-lines.log) + # workaround the fact that GitHub Actions doesn't support multi-line output + # https://trstringer.com/github-actions-multiline-strings/ + UNCOVERED_LINES="${UNCOVERED_LINES//'%'/'%25'}" + UNCOVERED_LINES="${UNCOVERED_LINES//$'\n'/'%0A'}" + UNCOVERED_LINES="${UNCOVERED_LINES//$'\r'/'%0D'}" + echo "uncovered_lines=$UNCOVERED_LINES" >> "$GITHUB_OUTPUT" + + - name: Azure login + if: env.GITHUB_TOKEN + env: + GITHUB_TOKEN: ${{ secrets.CREDITCOIN_GITHUB_API_TOKEN }} + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + allow-no-subscriptions: true + + - name: Upload coverage report to Azure Storage + if: env.GITHUB_TOKEN + env: + GITHUB_TOKEN: ${{ secrets.CREDITCOIN_GITHUB_API_TOKEN }} + run: | + AZURE_STORAGE_KEY=${{ secrets.LLVM_AZURE_STORAGE_KEY }} + export AZURE_STORAGE_KEY + PR_NUMBER=$(echo "$GITHUB_REF" | sed "s|refs/pull/||" | sed "s|/merge||") + DESTINATION_PATH="crunch/PR-$PR_NUMBER" + export DESTINATION_PATH + + az storage blob upload-batch --account-name staticsitellvmhtml --auth-mode key -d "\$web" --destination-path "$DESTINATION_PATH" --overwrite -s ./target/llvm-cov + + - name: Post comment to PR + if: env.GITHUB_TOKEN + uses: mshick/add-pr-comment@v2 + env: + GITHUB_TOKEN: ${{ secrets.CREDITCOIN_GITHUB_API_TOKEN }} + with: + message: ${{ steps.coverage.outputs.uncovered_lines }} + allow-repeats: false + + - name: Upload coverage report as artifact + uses: actions/upload-artifact@v4 with: - command: test + name: coverage-report + path: target/llvm-cov/html/ diff --git a/.lycheeignore b/.lycheeignore new file mode 100644 index 0000000..b437617 --- /dev/null +++ b/.lycheeignore @@ -0,0 +1 @@ +staticsitellvmhtml.z13.web.core.windows.net