Skip to content

Commit

Permalink
add code coverage
Browse files Browse the repository at this point in the history
Push coverage (lines %) to a gist, which then backs a
https://shields.io badge.
  • Loading branch information
yshavit authored Jun 9, 2024
1 parent c5e4f60 commit 9bda4eb
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Coverage

on:
push:
branches: [ "main" ]

jobs:
coverage:
runs-on: ubuntu-latest
environment: Code Coverage Badge
env:
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v4
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Generate code coverage
run: |
percent_coverage="$(cargo llvm-cov --json | jq '.data[].totals.lines.percent')"
printf '::notice title=Coverage (lines %%)::%s' "$percent_coverage"
echo "LINES_PERCENT=$percent_coverage" >> "$GITHUB_ENV"
- name: Upload to gist
run: ./scripts/percent_to_shields_gist coverage "$LINES_PERCENT"
env:
GIST_URL: ${{ vars.GIST_URL }}
GH_TOKEN: ${{ secrets.API_TOKEN }}
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: cargo build
run: .github/workflows/cargo_to_gh rustc --message-format json -- -Awarnings
run: scripts/cargo_to_gh rustc --message-format json -- -Awarnings

check:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: cargo check
run: .github/workflows/cargo_to_gh check
run: scripts/cargo_to_gh check

test:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
![Code Coverage](https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2Fyshavit%2F53901a0115b596e015a891c41fb0f256%2Fraw%2Fmdq-coverage.json)

3 changes: 2 additions & 1 deletion .github/workflows/cargo_to_gh → scripts/cargo_to_gh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

source scripts/common.sh || exit 1

json_opts=()
if ! echo "$*" | grep -q -- '--message-format json' ; then
echo "NO: $*"
json_opts=(--message-format json)
fi

Expand Down
10 changes: 10 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set -euo pipefail

function msg() {
echo >&2 "$@"
}

function err() {
msg "$@"
exit 1
}
31 changes: 31 additions & 0 deletions scripts/percent_to_color
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

source scripts/common.sh || exit 1

if [ $# -ne 1 ]; then
err 'require exactly one argument'
fi
percent="$1"

percent_0_to_5="$(printf 'scale=0; %s / 20\n' "$percent" | bc)"

case "$percent_0_to_5" in
0) # 0 - 19%
echo darkred
;;
1) # 20 - 39%
echo crimson
;;
2) # 40 - 59%
echo orangered
;;
3) # 60 - 79%
echo yellowgreen
;;
4 | 5)
echo forestgreen
;;
*)
err 'invalid percent'
;;
esac
22 changes: 22 additions & 0 deletions scripts/percent_to_shields_gist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

source scripts/common.sh || exit 1

if [ -z "${GIST_URL:-}" ]; then
err "no GIST_URL env set"
fi
if [ $# -ne 2 ]; then
err 'requires exactly two arguments'
fi
label="$1"
percent="$2"

color="$(scripts/percent_to_color "$percent")"

json_text="$(echo '{}' |
jq -c '{schemaVersion: 1, label: $badge_label, color: $color, message: $percent}' \
--arg badge_label "$label" --arg color "$color" --arg percent "$(printf '%.1f%%' "$percent")" )"

gh gist edit "$GIST_URL" <(echo "$json_text")


0 comments on commit 9bda4eb

Please sign in to comment.