From e96837029979b2d9432a991871e0bc1ffae32982 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 3 Oct 2026 03:07:45 +0200 Subject: [PATCH] ci: publish current coverage badges --- .gitignore | 1 + Jenkinsfile | 106 +++++++++++++++++++++++++++++-- README.md | 31 ++++++++- scripts/publish-badges.sh | 83 ++++++++++++++++++++++++ scripts/render-coverage-badge.sh | 69 ++++++++++++++++++++ scripts/render-test-badge.sh | 56 ++++++++++++++++ scripts/render-version-badge.sh | 65 +++++++++++++++++++ tarpaulin.toml | 13 ++++ 8 files changed, 418 insertions(+), 6 deletions(-) create mode 100755 scripts/publish-badges.sh create mode 100755 scripts/render-coverage-badge.sh create mode 100755 scripts/render-test-badge.sh create mode 100755 scripts/render-version-badge.sh create mode 100644 tarpaulin.toml diff --git a/.gitignore b/.gitignore index c3c7408..8d7c4ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target/ /dist/ +/coverage/ /.venv/ .env gitea_token.txt diff --git a/Jenkinsfile b/Jenkinsfile index 1d42c1b..9eee01a 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,6 +6,8 @@ pipeline { environment { CARGO_TERM_COLOR = 'never' + CARGO_TARPAULIN_VERSION = '0.37.5' + BADGE_PUBLISH_ROOT = '/mnt/assets/coverage' } stages { @@ -17,7 +19,41 @@ pipeline { stage('Cargo Tests') { steps { - sh 'cargo test --locked --all' + sh ''' + set -eu + + cargo test --locked --all + mkdir -p coverage + : > "coverage/.tests-passed-${BUILD_NUMBER}" + ''' + } + } + + stage('Coverage') { + steps { + sh ''' + set -eu + + installed_tarpaulin=$(cargo tarpaulin --version 2>/dev/null || true) + if [ "${installed_tarpaulin}" != "cargo-tarpaulin-tarpaulin ${CARGO_TARPAULIN_VERSION}" ]; then + cargo install cargo-tarpaulin --locked --version "${CARGO_TARPAULIN_VERSION}" + fi + + cargo tarpaulin \ + --config tarpaulin.toml \ + --out Json \ + --output-dir coverage + + mv coverage/tarpaulin-report.json coverage/summary.json + coverage_percent=$(sed -n \ + 's/^.*],"coverage":\([0-9][0-9.]*\),"covered":[0-9][0-9]*,"coverable":[0-9][0-9]*}$/\1/p' \ + coverage/summary.json) + scripts/render-coverage-badge.sh \ + "${coverage_percent}" \ + coverage/coverage.svg + rm coverage/summary.json + : > "coverage/.coverage-ready-${BUILD_NUMBER}" + ''' } } } @@ -27,8 +63,8 @@ pipeline { publishChecks( conclusion: 'SUCCESS', name: 'Rust CI', - summary: 'Cargo tests passed.', - text: 'Rust tests passed without cross compilation.', + summary: 'Cargo tests, coverage, and badge generation passed.', + text: 'Rust tests and the enforced line-coverage gate passed without cross compilation; current badges were generated.', title: 'CI Successful' ) } @@ -37,8 +73,8 @@ pipeline { publishChecks( conclusion: 'FAILURE', name: 'Rust CI', - summary: 'Cargo tests failed.', - text: 'Check the Jenkins log for the Cargo test failure.', + summary: 'Cargo tests, coverage, or badge generation failed.', + text: 'Check the Jenkins log for the Rust test, coverage, or badge-generation failure.', title: 'CI Failed' ) } @@ -52,5 +88,65 @@ pipeline { title: 'CI Unstable' ) } + + cleanup { + sh ''' + set -eu + + mkdir -p coverage + test_status=failing + if [ -f "coverage/.tests-passed-${BUILD_NUMBER}" ]; then + test_status=passing + fi + scripts/render-test-badge.sh \ + "${test_status}" \ + coverage/test.svg + + if [ ! -f "coverage/.coverage-ready-${BUILD_NUMBER}" ]; then + scripts/render-coverage-badge.sh - coverage/coverage.svg + fi + + package_version=$(awk ' + $0 == "[package]" { in_package = 1; next } + substr($0, 1, 1) == "[" { in_package = 0 } + in_package && $1 == "version" && $2 == "=" { + gsub(/^"|"$/, "", $3) + print $3 + exit + } + ' Cargo.toml) + if [ -z "${package_version}" ]; then + echo "could not determine package version" >&2 + exit 2 + fi + scripts/render-version-badge.sh \ + "${package_version}" \ + coverage/version.svg + + rm -f \ + coverage/summary.json \ + coverage/tarpaulin-report.json \ + "coverage/.tests-passed-${BUILD_NUMBER}" \ + "coverage/.coverage-ready-${BUILD_NUMBER}" + ''' + + archiveArtifacts( + allowEmptyArchive: false, + artifacts: 'coverage/coverage.svg,coverage/test.svg,coverage/version.svg', + fingerprint: true + ) + + sh ''' + set -eu + + if [ "${BRANCH_NAME:-}" = "main" ]; then + scripts/publish-badges.sh \ + "${BADGE_PUBLISH_ROOT}" \ + coverage \ + test \ + version + fi + ''' + } } } diff --git a/README.md b/README.md index 91425a9..7b9d067 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # rust_realease +![version](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/version.svg) +![tests](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/test.svg) +![test coverage](https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/coverage.svg) + `rust_realease` builds and publishes the binaries from a Rust package to a Gitea release. Give it a local project directory or a Git URL. It inspects Cargo metadata, checks the version tag, builds the selected targets with Cross, creates cargo-binstall-compatible `.tgz` archives, directly downloadable executables, or both, writes `SHA256SUMS`, uploads the assets, and verifies release downloads. It can also prepare a project for release with explicit commit, tag, and push options. ## Install @@ -139,6 +143,31 @@ cargo clippy --all-targets --all-features -- -D warnings cargo test --all ``` -Jenkins runs Cargo tests only. Cross builds and live Gitea releases are separate release checks. The small [target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) is maintained in its own repository for fast target matrix experiments. Clone it separately when needed; it is not part of this checkout. +Jenkins runs Cargo tests and enforces a 65% line-coverage floor. Run the exact +coverage command locally with: + +```bash +cargo tarpaulin --config tarpaulin.toml --out Json --output-dir coverage +``` + +The current suite measures 66.12% (1,046/1,582 lines). `Cargo.toml` is the +authoritative version source. Jenkins temporarily renames Tarpaulin's JSON to +`coverage/summary.json`, extracts and validates the overall percentage, renders +badges locally, then removes the summary. It archives only +`coverage/version.svg`, `coverage/test.svg`, and `coverage/coverage.svg`, with +fingerprinting. Successful `main` builds publish those files atomically under +`/mnt/assets/coverage/idle-org/rust_realease/main/`, serving: + +- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/version.svg` +- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/test.svg` +- `https://assets.dandaedre.com/coverage/idle-org/rust_realease/main/coverage.svg` + +The asset server must mount published assets read-only, disable directory +listings, serve SVG with a restrictive content security policy, and revalidate +these mutable URLs or use a short cache lifetime. Cross builds and live Gitea +releases remain separate release checks. The small +[target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) +is maintained in its own repository for fast target matrix experiments. Clone +it separately when needed; it is not part of this checkout. With `cargo-binstall` installed, run the opt-in isolated install fixture for all three targets with `cargo test real_binstall_installs_local_release_fixture -- --ignored`. diff --git a/scripts/publish-badges.sh b/scripts/publish-badges.sh new file mode 100755 index 0000000..67e5b47 --- /dev/null +++ b/scripts/publish-badges.sh @@ -0,0 +1,83 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 [badge...]" >&2 + echo "badges: coverage test version" >&2 + exit 2 +} + +[ "$#" -ge 2 ] || usage +publication_root=$1 +shift + +case "$publication_root" in + /*) ;; + *) + echo "badge publication root must be an absolute path: $publication_root" >&2 + exit 2 + ;; +esac + +if [ ! -d "$publication_root" ] || [ ! -w "$publication_root" ]; then + echo "badge publication root must be an existing writable directory: $publication_root" >&2 + exit 2 +fi + +selected=' ' +for badge in "$@"; do + case "$badge" in + coverage|test|version) ;; + *) + echo "unsupported badge: $badge" >&2 + exit 2 + ;; + esac + case "$selected" in + *" $badge "*) + echo "badge selected more than once: $badge" >&2 + exit 2 + ;; + esac + selected="$selected$badge " + + source="coverage/$badge.svg" + if [ ! -f "$source" ] || [ ! -s "$source" ] || [ -L "$source" ]; then + echo "required badge must be a regular nonempty file, not a symlink: $source" >&2 + exit 2 + fi +done + +owner_root="$publication_root/idle-org" +project_root="$owner_root/rust_realease" +badge_root="$project_root/main" + +for directory in "$owner_root" "$project_root"; do + if [ -L "$directory" ] || { [ -e "$directory" ] && [ ! -d "$directory" ]; }; then + echo "badge destination component is not a directory: $directory" >&2 + exit 2 + fi + mkdir -p "$directory" + chmod 0755 "$directory" +done + +if [ -L "$badge_root" ]; then + unlink "$badge_root" +elif [ -e "$badge_root" ] && [ ! -d "$badge_root" ]; then + echo "badge destination is not a directory: $badge_root" >&2 + exit 2 +fi +mkdir -p "$badge_root" +chmod 0755 "$badge_root" + +temporary= +trap 'if [ -n "$temporary" ]; then rm -f "$temporary"; fi' EXIT HUP INT TERM +for badge in "$@"; do + temporary=$(mktemp "$badge_root/.$badge.svg.XXXXXX") + cp "coverage/$badge.svg" "$temporary" + chmod 0644 "$temporary" + mv -f "$temporary" "$badge_root/$badge.svg" + temporary= + echo "published $badge badge to $badge_root/$badge.svg" +done +trap - EXIT HUP INT TERM diff --git a/scripts/render-coverage-badge.sh b/scripts/render-coverage-badge.sh new file mode 100755 index 0000000..1927940 --- /dev/null +++ b/scripts/render-coverage-badge.sh @@ -0,0 +1,69 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 " >&2 + exit 2 +} + +[ "$#" -eq 2 ] || usage +percent=$1 +output=$2 + +if [ "$percent" = "-" ]; then + rounded=- + color='#9f9f9f' +else + if ! awk -v value="$percent" 'BEGIN { + valid = value ~ /^[0-9]+([.][0-9]+)?$/ && value >= 0 && value <= 100 + exit(valid ? 0 : 1) + }'; then + echo "coverage percentage must be - or a number from 0 through 100: $percent" >&2 + exit 2 + fi + + rounded=$(awk -v value="$percent" 'BEGIN { printf "%.0f", value }') + if [ "$rounded" -ge 90 ]; then + color='#4c1' + elif [ "$rounded" -ge 70 ]; then + color='#dfb317' + else + color='#e05d44' + fi +fi + +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi + +temporary="${output}.tmp.$$" +trap 'rm -f "$temporary"' EXIT HUP INT TERM + +sed \ + -e "s/@PERCENT@/${rounded}/g" \ + -e "s/@COLOR@/${color}/g" \ + >"$temporary" <<'EOF' + + coverage: @PERCENT@% + + + + + + + + + + + + + + coverage + @PERCENT@% + + +EOF + +mv "$temporary" "$output" +trap - EXIT HUP INT TERM diff --git a/scripts/render-test-badge.sh b/scripts/render-test-badge.sh new file mode 100755 index 0000000..997cc8a --- /dev/null +++ b/scripts/render-test-badge.sh @@ -0,0 +1,56 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 " >&2 + exit 2 +} + +[ "$#" -eq 2 ] || usage +status=$1 +output=$2 + +case "$status" in + passing) color='#4c1' ;; + failing) color='#e05d44' ;; + *) + echo "test badge status must be passing or failing: $status" >&2 + exit 2 + ;; +esac + +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi + +temporary="${output}.tmp.$$" +trap 'rm -f "$temporary"' EXIT HUP INT TERM + +sed \ + -e "s/@STATUS@/${status}/g" \ + -e "s/@COLOR@/${color}/g" \ + >"$temporary" <<'EOF' + + tests: @STATUS@ + + + + + + + + + + + + + + tests + @STATUS@ + + +EOF + +mv "$temporary" "$output" +trap - EXIT HUP INT TERM diff --git a/scripts/render-version-badge.sh b/scripts/render-version-badge.sh new file mode 100755 index 0000000..fcb868e --- /dev/null +++ b/scripts/render-version-badge.sh @@ -0,0 +1,65 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 " >&2 + exit 2 +} + +[ "$#" -eq 2 ] || usage +version=$1 +output=$2 + +case "$version" in + ''|*[!0-9A-Za-z.+-]*) + echo "package version contains unsupported badge characters: $version" >&2 + exit 2 + ;; +esac + +value_width=$(awk -v value="$version" 'BEGIN { + width = length(value) * 7 + 10 + print (width < 45 ? 45 : width) +}') +badge_width=$((59 + value_width)) +value_center=$(awk -v width="$value_width" 'BEGIN { + printf "%.1f", 59 + width / 2 +}') + +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi + +temporary="${output}.tmp.$$" +trap 'rm -f "$temporary"' EXIT HUP INT TERM + +sed \ + -e "s/@VERSION@/${version}/g" \ + -e "s/@BADGE_WIDTH@/${badge_width}/g" \ + -e "s/@VALUE_WIDTH@/${value_width}/g" \ + -e "s/@VALUE_CENTER@/${value_center}/g" \ + >"$temporary" <<'EOF' + + version: @VERSION@ + + + + + + + + + + + + + + version + @VERSION@ + + +EOF + +mv "$temporary" "$output" +trap - EXIT HUP INT TERM diff --git a/tarpaulin.toml b/tarpaulin.toml new file mode 100644 index 0000000..cca4fb2 --- /dev/null +++ b/tarpaulin.toml @@ -0,0 +1,13 @@ +[coverage] +all-features = true +engine = "Llvm" +fail-under = 65.0 +locked = true +run-types = ["Tests"] +workspace = true + +[report] +out = ["Stdout"] + +# This floor stays below the measured suite result without excluding production +# code or manufacturing tests for process-only and platform failure paths.