Skip to content

Commit 17628e4

Browse files
kanarusonihilist
andauthored
chore: add a github page with coverage stats & shields.io coverage badge (#630)
* feat(coverage): init coverage * feat(coverage): CI coverage.yml file added * feat(coverage): CI coverage.yml file added * feat(coverage): handle all funcs/mods with a feature * fix(deploy): remove useless index.html * fix(deploy): remove useless index.html * fix(coverage): exec actions in Taskfile & remove actions-rs * fix(coverage): modify tasks & coverage.yml * fix(coverage): modify pages:build task name in coverage.yml * feat(ci|tasks): add task to install tools (llvm-cov) * fix(taskfile): outputs for llvm-cov * fix(taskfile): outputs for llvm-cov * fix(taskfile): split llvm-cov command for JSON & HTML * fix(taskfile): fix PERCENT line format * fix(coverage): remove script part from the Taskfile and move it to .github/scripts * fix(taskfile): fix the permission problem * organize `coverage:html`, `pages:build`, `tools` task & `generate_coverage_badge.sh` --------- Co-authored-by: onihilist <mashirooo.yt@gmail.com> Co-authored-by: o.nihilist <107046146+onihilist@users.noreply.github.com> Co-authed-by: kanarus <mail@kanarus.dev>
1 parent a72565f commit 17628e4

File tree

5 files changed

+203
-2
lines changed

5 files changed

+203
-2
lines changed

.github/workflows/coverage.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Coverage & GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
concurrency:
10+
group: "pages"
11+
cancel-in-progress: true
12+
13+
jobs:
14+
coverage:
15+
runs-on: ubuntu-latest
16+
17+
permissions:
18+
contents: read
19+
pages: write
20+
id-token: write
21+
22+
steps:
23+
- uses: actions/checkout@v4
24+
25+
- name: Configure Pages
26+
uses: actions/configure-pages@v5
27+
28+
- name: Use Rust stable
29+
run: rustup default stable
30+
31+
- name: Install Task
32+
uses: go-task/setup-task@v1
33+
34+
- name: Build Pages site (coverage + badge)
35+
run: task pages:build
36+
37+
- name: Upload Pages artifact
38+
uses: actions/upload-pages-artifact@v3
39+
with:
40+
path: site/
41+
42+
deploy:
43+
needs: coverage
44+
runs-on: ubuntu-latest
45+
46+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
47+
48+
permissions:
49+
pages: write
50+
id-token: write
51+
52+
environment:
53+
name: github-pages
54+
url: ${{ steps.deployment.outputs.page_url }}
55+
56+
steps:
57+
- name: Deploy to GitHub Pages
58+
id: deployment
59+
uses: actions/deploy-pages@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**/target
22
**/Cargo.lock
3+
/site
34
/memo.md

Taskfile.yaml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ vars:
44
native_rts: 'tokio smol nio glommio monoio compio'
55
non_wasm_rts: 'lambda {{.native_rts}}'
66
all_rts: 'worker {{.non_wasm_rts}}'
7-
maybe_nightly: { sh: cargo version | grep -q 'nightly' && echo 'nightly' || echo '' }
7+
maybe_nightly: { sh: cargo version | grep -q 'nightly' && echo 'nightly' || echo '' }
8+
coverage_features: 'DEBUG,rt_tokio,sse,ws,tls,openapi'
9+
github_pages_dir: 'site'
810

911
tasks:
1012
CI:
@@ -46,7 +48,6 @@ tasks:
4648
- cd benches && cargo bench --features DEBUG --no-run
4749
- for: { var: native_rts }
4850
cmd: cd benches_rt/{{.ITEM}} && cargo check
49-
5051
bench:
5152
status:
5253
- '[ "{{.maybe_nightly}}" != nightly ]' # skip if not nightly
@@ -126,3 +127,48 @@ tasks:
126127
- cargo clippy --target wasm32-unknown-unknown --features rt_worker,sse,{{.maybe_nightly}} -- --deny warnings
127128
- cargo clippy --target wasm32-unknown-unknown --features rt_worker,ws,{{.maybe_nightly}} -- --deny warnings
128129
- cargo clippy --target wasm32-unknown-unknown --features rt_worker,sse,ws,openapi,{{.maybe_nightly}} -- --deny warnings
130+
131+
#### coverage ####
132+
coverage:html:
133+
deps: [ tools:coverage ]
134+
cmds:
135+
- cargo llvm-cov clean --workspace
136+
- cargo llvm-cov --html --workspace --features {{.coverage_features}}
137+
generates:
138+
- target/llvm-cov/html/index.html
139+
140+
#### GiHub Pages ####
141+
pages:build:
142+
cmds:
143+
- task: pages:build:coverage
144+
145+
pages:build:coverage:
146+
deps: [ tools:coverage ]
147+
vars:
148+
coverage_dir: '{{.github_pages_dir}}/coverage'
149+
cmds:
150+
# setup
151+
- rm -rf {{.coverage_dir}}; mkdir -p {{.coverage_dir}}
152+
- chmod +x ./scripts/generate_coverage_badge.sh
153+
- cargo llvm-cov clean --workspace
154+
# file generation
155+
- |
156+
cargo llvm-cov --workspace --features {{.coverage_features}} --html --output-dir {{.coverage_dir}} \
157+
&& mv {{.coverage_dir}}/html/* {{.coverage_dir}}/ \
158+
&& rm -rf {{.coverage_dir}}/html
159+
- |
160+
cargo llvm-cov --workspace --features {{.coverage_features}} --json \
161+
| ./scripts/generate_coverage_badge.sh -o {{.github_pages_dir}}/coverage_badge.json
162+
163+
#### tools ####
164+
tools:
165+
deps:
166+
- tools:coverage
167+
168+
tools:coverage:
169+
cmds:
170+
- rustup component add llvm-tools-preview
171+
- cargo install cargo-llvm-cov
172+
status:
173+
- rustup component list --installed | grep -q 'llvm-tools'
174+
- command -qv cargo-llvm-cov

ohkami/src/response/headers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ impl Headers {
367367
unsafe { self.standard.delete(name as u8) };
368368
}
369369
}
370+
370371
pub(crate) fn remove_custom(&mut self, name: &'static str) {
371372
if let Some(c) = self.custom.as_mut()
372373
&& let Some(v) = c.remove(name)

scripts/generate_coverage_badge.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
USAGE=$(cat << EOS
5+
USAGE
6+
generate_coverage_badge.sh [OPTIONS] [json report path]
7+
8+
DESCRIPTION
9+
Generates a json file for [shilds.io's Endpoint Badge](https://shields.io/badges/endpoint-badge)
10+
from [cargo llvm-cov](https://github.com/taiki-e/cargo-llvm-cov)'s --json report.
11+
12+
ARGUMENTS
13+
A file path that holds the json report by cargo llvm-cov.
14+
If this is not passed, the json report content is expected to be passed via stdin.
15+
16+
OPTIONS
17+
-h or --help
18+
Show this message and exit.
19+
-o or --out <path>
20+
Set output file path. (default: coverage_badge.json)
21+
EOS
22+
)
23+
24+
OUT_FILE="$PWD/coverage_badge.json"
25+
JSON_REPORT_PATH=''
26+
27+
while [[ "$#" -gt 0 ]]; do
28+
case "$1" in
29+
-h|--help)
30+
echo "$USAGE"
31+
shift 1
32+
exit 0
33+
;;
34+
-o|--out)
35+
OUT_FILE="$2"
36+
shift 2
37+
;;
38+
*)
39+
JSON_REPORT_PATH="$1"
40+
shift 1
41+
if [[ "$#" -gt 0 ]]; then
42+
echo "unknown arguments: $*"
43+
echo "$USAGE"
44+
exit 1
45+
fi
46+
esac
47+
done
48+
49+
if [ -n "$JSON_REPORT_PATH" ]; then
50+
JSON_REPORT=$(cat "$JSON_REPORT_PATH")
51+
else
52+
read -r JSON_REPORT || [ -n "$JSON_REPORT" ]
53+
fi
54+
55+
read -r LINE FUNC REGION < <(
56+
echo "$JSON_REPORT" \
57+
| jq -r '
58+
[
59+
(.data[0].totals.lines.percent // 0),
60+
(.data[0].totals.functions.percent // 0),
61+
(.data[0].totals.regions.percent // 0)
62+
] | @tsv
63+
' \
64+
| awk -F'\t' '{
65+
for (i=1;i<=3;i++) {
66+
gsub(/[^0-9.]/, "", $i);
67+
if ($i == "" ) $i = 0;
68+
}
69+
print $1, $2, $3
70+
}'
71+
)
72+
73+
PERCENT="$(awk -v l="$LINE" -v f="$FUNC" -v r="$REGION" 'BEGIN{ printf("%.2f", (l+f+r)/3.0) }')"
74+
PERCENT_INT="$(awk -v p="$PERCENT" 'BEGIN{ printf("%d", (p + 0.5)) }')"
75+
76+
if [ "$PERCENT_INT" -ge 90 ]; then
77+
COLOR="brightgreen"
78+
elif [ "$PERCENT_INT" -ge 75 ]; then
79+
COLOR="yellowgreen"
80+
else
81+
COLOR="red"
82+
fi
83+
84+
mkdir -p "$(dirname OUT_DIR)"
85+
cat > "$OUT_FILE" <<EOF
86+
{
87+
"schemaVersion": 1,
88+
"label": "coverage",
89+
"message": "${PERCENT}%",
90+
"color": "${COLOR}"
91+
}
92+
EOF
93+
94+
echo "Wrote $OUT_FILE (LINE=$LINE FUNC=$FUNC REGION=$REGION AVG=$PERCENT COLOR=$COLOR)"

0 commit comments

Comments
 (0)