Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable coverage #829

Merged
merged 1 commit into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/actions/cargo-install-upload-artifacts/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ runs:
--root ${{ steps.metadata.outputs.out-dir }}
--bins
use-cross: true
env:
RUSTFLAGS: "" # Make sure to unset RUSTFLAGS

- name: Archive artifacts
id: archive
Expand Down
67 changes: 67 additions & 0 deletions .github/actions/cargo-llvm-cov/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: llvm coverage
description: Sets up everything that is needed for coverage. Makes artifacts available for processing later, prefixed with _coverage-
inputs:
name:
description: 'the name of the artifact'
required: true
outputs:
artifact-name:
description: 'the name of the artifact'
value: ${{ steps.cov.outputs.artifact-name }}
runs:
using: composite
steps:
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@v1
with:
tool: cargo-llvm-cov
- run: rustup component add llvm-tools-preview
shell: bash
- name: LLVM instrument coverage
id: cov
uses: ./.github/actions/post
if: always()
with:
main: |
pwd=$(pwd)
if which cygpath; then
pwd="$(cygpath -w "$(pwd)")"
fi
echo RUSTFLAGS=" -C instrument-coverage --remap-path-prefix ${pwd}=" >> $GITHUB_ENV
echo LLVM_PROFILE_FILE="${pwd}/target/cross-%m.profraw" >> $GITHUB_ENV
echo CARGO_INCREMENTAL="0" >> $GITHUB_ENV
echo RUST_TEST_THREADS="1" >> $GITHUB_ENV
echo "::set-output name=artifact-name::_coverage-${name}"
post: |
# XXX(emilgardis): Upload early?
pwd=$(pwd)
if which cygpath; then
pwd="$(cygpath -w "$(pwd)")"
fi
# No pwd needed here, we're in the root
export LLVM_PROFILE_FILE="${pwd}/target/cross-%m.profraw"
export CARGO_LLVM_COV_TARGET_DIR="${pwd}/target"
mkdir coverage
echo $(ls target)
cargo llvm-cov --no-run --remap-path-prefix --lcov --output-path "coverage/lcov.${name}.info" -vv || ( echo "::error title=Coverage merge failed::" && exit 0 )
rm target/*.profraw
npm install @actions/artifact
npm install glob

cat <<-EOT | node - || ( echo "::error title=Coverage upload failed::" && exit 0 )
(async function main() {
var artifact = require('@actions/artifact');
var glob = require('glob')
const artifactClient = artifact.create();
const artifactName = '_coverage-' + process.env.name;
const files = glob.sync("coverage/*");
if (!files.length) {
process.exit(0);
}
console.log("${files}")
const options = { retentionDays: 2 };
const upload = await artifactClient.uploadArtifact(artifactName, files, "coverage", options);
})()
EOT
env:
name: ${{ inputs.name }}
4 changes: 2 additions & 2 deletions .github/actions/cargo-publish/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ runs:
shell: bash
env:
GITHUB_TOKEN: ${{ inputs.github-token }}

- name: Create GitHub release
if: >
github.event_name == 'push' && (
Expand All @@ -54,7 +54,7 @@ runs:
prerelease: ${{ startsWith(github.ref, 'refs/tags/v') && contains(github.ref_name, '-') }}
draft: ${{ !startsWith(github.ref, 'refs/tags/v') && steps.changelog-reader.outputs.status == 'unreleased' }}
files: |
${{ steps.download-artifacts.outputs.download-path }}/*/*
${{ steps.download-artifacts.outputs.download-path }}/cross-*/*

- name: Publish crate
uses: actions-rs/cargo@v1
Expand Down
14 changes: 14 additions & 0 deletions .github/actions/post/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# adapted from https://github.com/actions/runner/issues/1478
name: With post step
description: 'Generic JS Action to execute a main command and set a command in a post step.'
inputs:
main:
description: 'Main command/script.'
required: true
post:
description: 'Post command/script.'
required: true
runs:
using: 'node16'
main: 'main.js'
post: 'main.js'
26 changes: 26 additions & 0 deletions .github/actions/post/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// adapted from https://github.com/actions/runner/issues/1478
const { exec } = require("child_process");

function run(cmd) {
exec(cmd, { shell: "bash" }, (error, stdout, stderr) => {
if (stdout.length != 0) {
console.log(`${stdout}`);
}
if (stderr.length != 0) {
console.error(`${stderr}`);
}
if (error) {
process.exitCode = error.code;
console.error(`${error}`);
}
});
}

if (process.env[`STATE_POST`] != undefined) {
// Are we in the 'post' step?
run(process.env.INPUT_POST);
} else {
// Otherwise, this is the main step
console.log(`::save-state name=POST::true`);
run(process.env.INPUT_MAIN);
}
14 changes: 14 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment: false
codecov:
branch: main
coverage:
status:
project:
default:
informational: true
patch:
default:
informational: true
flag_management:
default_rules: # the rules that will be followed for any flag added, generally
carryforward: true
57 changes: 53 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ jobs:
- uses: actions/checkout@v3

- uses: ./.github/actions/setup-rust
Emilgardis marked this conversation as resolved.
Show resolved Hide resolved
- uses: ./.github/actions/cargo-llvm-cov
with:
name: test-${{matrix.os}}

- name: Run unit tests
uses: actions-rs/cargo@v1
Expand Down Expand Up @@ -208,6 +211,7 @@ jobs:
outputs:
has-image: ${{ steps.prepare-meta.outputs.has-image }}
images: ${{ steps.build-docker-image.outputs.images && fromJSON(steps.build-docker-image.outputs.images) }}
coverage-artifact: ${{ steps.cov.outputs.artifact-name }}
steps:
- uses: actions/checkout@v3

Expand All @@ -217,12 +221,9 @@ jobs:
if: runner.os == 'Linux'
uses: docker/setup-buildx-action@v1

- name: Install cross
if: matrix.deploy
run: cargo install --path . --force

- name: Build xtask
run: cargo build -p xtask

- name: Prepare Meta
id: prepare-meta
timeout-minutes: 60
Expand All @@ -231,6 +232,18 @@ jobs:
TARGET: ${{ matrix.target }}
SUB: ${{ matrix.sub }}
shell: bash

- name: LLVM instrument coverage
id: cov
uses: ./.github/actions/cargo-llvm-cov
if: steps.prepare-meta.outputs.has-image
with:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably change to:

if: steps.prepare-meta.outputs.has-image && matrix.target == 'x86_64-unknown-linux-gnu'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need (or want) coverage on the test binaries, fixed this by clearing RUSTFLAGS, maybe it should be more specific though (i.e only remove instrument-coverage but not sure how to do that well)

name: cross-${{matrix.pretty}}

- name: Install cross
if: matrix.deploy
run: cargo install --path . --force --debug

- name: Docker Meta
if: steps.prepare-meta.outputs.has-image
id: docker-meta
Expand Down Expand Up @@ -321,3 +334,39 @@ jobs:
"$(jq -r 'all(.result as $result | (["success", "skipped"] | contains([$result])))' <<< "${needs}")"
env:
needs: ${{ toJson(needs) }}

code-cov:
name: Coverage
needs: [test, build, conclusion, generate-matrix]
# should check that there are any artifacts, if not skip
if: always() && (needs.build.result == 'success' || needs.build.result == 'skipped') && needs.test.result == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-rust
- uses: actions/download-artifact@v3
with:
path: ${{ runner.temp }}/artifacts
- name: Grab PR number
run: echo "::set-output name=pr::"$(echo $commit_message | sed -ne 's/.*\#\(.*\):/\1/p')
id: pr-number
if: ${{ !github.event.pull_request.number }}
env:
commit_message: >
${{
((
startsWith(github.event.head_commit.message, 'Try #') &&
github.event.head_commit.author.username == 'bors[bot]'
) && github.event.head_commit.message) || ''
}}
- name: Upload to codecov.io
run: |
set -x
curl -Os https://uploader.codecov.io/latest/linux/codecov
chmod +x codecov
sha_rev=$(git rev-parse HEAD)
find ${artifacts} -name "lcov.*.info" -exec ./codecov -F $(echo {} | sed -n 's/lcov\.\(.*\)\.info/\1/p') \
${pr:+-P ${pr}} -f {} --sha ${sha_rev} -n $(echo {} | sed -n 's/lcov\.\(.*\)\.info/\1/p') \;
Comment on lines +368 to +369
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may have to specify parent sha here, not sure if it really works when you have a staging/trying branch.

been trying to find prior art for codecov with bors integration but not able to find any good examples, only ones I find are my own previous tries :D

Comment on lines +368 to +369
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want this to never be able to fail, but on fail echo ::error title=Coverage upload failed::

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to do that well though

env:
pr: ${{ steps.pr-number.outputs.pr || '' }}
artifacts: ${{ runner.temp }}/artifacts
7 changes: 5 additions & 2 deletions ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ main() {
local td=

retry cargo fetch
cargo install --force --path .
cargo install --force --path . --debug

# Unset RUSTFLAGS
export RUSTFLAGS=""

export QEMU_STRACE=1

Expand Down Expand Up @@ -165,7 +168,7 @@ main() {
--depth 1 \
--recursive \
https://github.com/cross-rs/test-workspace "${td}"

pushd "${td}"
TARGET="${TARGET}" workspace_test --manifest-path="./workspace/Cargo.toml"
pushd "workspace"
Expand Down