From 0db65eb3af9a4b2cd30ca55064640d8a285f3eec Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sun, 4 Oct 2026 22:09:24 +0200 Subject: [PATCH] ci: generate and publish version test and coverage badges --- .cargo/config.toml | 4 + .gitignore | 3 + Jenkinsfile | 52 ++++++- README.md | 4 + docs/security-and-ci.md | 74 +++++++++ scripts/ci-coverage-badges.sh | 272 ++++++++++++++++++++++++++++++++++ scripts/collect-coverage.sh | 11 ++ scripts/finalize-badges.sh | 19 +++ scripts/publish-badges.sh | 46 ++++++ scripts/test-badges.py | 174 ++++++++++++++++++++++ 10 files changed, 656 insertions(+), 3 deletions(-) create mode 100755 scripts/ci-coverage-badges.sh create mode 100755 scripts/collect-coverage.sh create mode 100755 scripts/finalize-badges.sh create mode 100755 scripts/publish-badges.sh create mode 100644 scripts/test-badges.py diff --git a/.cargo/config.toml b/.cargo/config.toml index 9e92fd9..f78e77b 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -3,3 +3,7 @@ rustflags = ["-Clink-arg=-fuse-ld=lld"] [resolver] incompatible-rust-versions = "fallback" + +[alias] +# No Rust tests exist yet: report the real baseline without excluding source. +coverage = "llvm-cov --locked --all-targets --all-features --json --summary-only --fail-under-lines 0 --output-path coverage/summary.json" diff --git a/.gitignore b/.gitignore index 6a1edeb..1003552 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ target/ *.csv *.log config.toml + +/coverage/ +/target-ci/ diff --git a/Jenkinsfile b/Jenkinsfile index 0277470..35ce7b5 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,6 +4,11 @@ pipeline { agent { label 'rust_nodocker' } + options { + disableConcurrentBuilds() + skipDefaultCheckout(true) + } + environment { // Jenkins does not render Cargo ANSI sequences cleanly. CARGO_TERM_COLOR = 'never' @@ -16,6 +21,11 @@ pipeline { // Supply-chain tools are pinned for reproducible policy behavior. CARGO_AUDIT_VERSION = '0.22.2' CARGO_DENY_VERSION = '0.20.2' + CARGO_LLVM_COV_VERSION = '0.6.16' + BADGE_ROOT = '/mnt/assets/coverage' + BADGE_OWNER = 'idle-org' + BADGE_PROJECT = 'tiny-wb' + BADGE_BRANCH = 'main' // Keep CI build products separate. CARGO_TARGET_DIR = "${WORKSPACE}/target-ci" @@ -30,6 +40,7 @@ pipeline { stages { stage('Checkout') { steps { + sh 'rm -rf coverage && mkdir coverage' checkout scm sh ''' @@ -56,7 +67,8 @@ pipeline { rustup toolchain install stable \ --profile minimal \ --component rustfmt \ - --component clippy + --component clippy \ + --component llvm-tools-preview rustup toolchain install "${MSRV}" \ --profile minimal @@ -77,6 +89,15 @@ pipeline { cargo-deny fi + command -v jq >/dev/null 2>&1 || { + echo 'ERROR: jq must be installed on this worker.' >&2 + exit 1 + } + INSTALLED_COVERAGE_VERSION="$(cargo llvm-cov --version 2>/dev/null | awk '{print $NF}' || true)" + if [ "${INSTALLED_COVERAGE_VERSION}" != "${CARGO_LLVM_COV_VERSION}" ]; then + cargo install --locked --version "${CARGO_LLVM_COV_VERSION}" cargo-llvm-cov + fi + # --------------------------------------------------------- # LLD # --------------------------------------------------------- @@ -234,6 +255,17 @@ pipeline { } } + stage('Coverage') { + steps { + sh ''' + set -eu + scripts/collect-coverage.sh + touch "coverage/.tests-passed-${BUILD_NUMBER}" + touch "coverage/.coverage-ready-${BUILD_NUMBER}" + ''' + } + } + stage('Clippy') { steps { sh ''' @@ -340,7 +372,7 @@ pipeline { conclusion: 'SUCCESS', name: 'Rust CI', summary: 'tiny-wb passed CI.', - text: 'RustSec audit, dependency policy, MSRV, formatting, compilation checks, tests, Clippy and the release build completed successfully. The release runtime smoke test is currently diagnostic only.', + text: 'RustSec audit, dependency policy, MSRV, formatting, compilation checks, tests, coverage, Clippy and the release build completed successfully. Badge generation and archival run in cleanup. The release runtime smoke test is currently diagnostic only.', title: 'Build Successful' ) @@ -352,7 +384,7 @@ pipeline { conclusion: 'FAILURE', name: 'Rust CI', summary: 'tiny-wb failed CI.', - text: 'RustSec audit, dependency policy, MSRV, formatting, compilation, tests, Clippy, or the release build failed. Check the Jenkins log.', + text: 'RustSec audit, dependency policy, MSRV, formatting, compilation, tests, coverage, Clippy, or the release build failed. Cleanup generates and archives current badges, including failure fallbacks. Check the Jenkins log.', title: 'Build Failed' ) @@ -370,5 +402,19 @@ pipeline { echo '⚠️ tiny-wb CI is unstable.' } + cleanup { + sh 'scripts/finalize-badges.sh' + archiveArtifacts artifacts: 'coverage/version.svg,coverage/test.svg,coverage/coverage.svg', + fingerprint: true, allowEmptyArchive: false + script { + // BRANCH_NAME is supplied by Jenkins multibranch jobs. + // Fail closed for other job types; do not infer from detached HEAD. + if (!env.BRANCH_NAME) { + echo 'Badge publication skipped: BRANCH_NAME unavailable; verify multibranch job configuration.' + } else if (env.BRANCH_NAME == env.BADGE_BRANCH && !env.CHANGE_ID && !env.TAG_NAME) { + sh 'scripts/publish-badges.sh "$BADGE_ROOT" "$BADGE_OWNER" "$BADGE_PROJECT" "$BADGE_BRANCH" version test coverage' + } + } + } } } diff --git a/README.md b/README.md index 2b3f702..9e759be 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # tiny-wb +![Version](https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/version.svg) +![Tests](https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/test.svg) +![Coverage](https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/coverage.svg) + `tiny-wb` is an interactive terminal application that acquires a frequency sweep from a tinySA over its USB serial console and exports the result as CSV. It is written in Rust and uses Ratatui for the interface. diff --git a/docs/security-and-ci.md b/docs/security-and-ci.md index 8008914..a27b8a3 100644 --- a/docs/security-and-ci.md +++ b/docs/security-and-ci.md @@ -133,3 +133,77 @@ This is intentionally “semi-hardened” CI. The repository does not add `cargo-vet`, SBOM generation, dependency vendoring, registry proxies, provenance/signing systems, or additional security scanners. Expanding that scope needs a separate design and maintenance decision. + +## CI badges and coverage + +The README references version, tests and line coverage at these mutable URLs: + +- https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/version.svg +- https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/test.svg +- https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/coverage.svg + +Repository identity and `main` were verified against the origin remote, its +HEAD, and Gitea repository metadata. The version comes from `Cargo.toml` via +`cargo metadata --no-deps --locked --format-version 1`, never a Jenkins constant. + +Coverage uses pinned `cargo-llvm-cov` 0.6.16 and Rust's `llvm-tools-preview` +component. The worker also needs `jq`. Install the prerequisites and run: + +```console +rustup component add llvm-tools-preview +cargo install --locked --version 0.6.16 cargo-llvm-cov +mkdir -p coverage +cargo coverage +``` + +The exact alias is tracked in `.cargo/config.toml` and expands to: + +```console +cargo llvm-cov --locked --all-targets --all-features --json --summary-only --fail-under-lines 0 --output-path coverage/summary.json +``` + +The enforced line-coverage floor is **0%**: no Rust tests currently exist. +Local measurement on 2026-10-04 was **0 of 1,024 lines (0%)**. +This is an honest initial baseline, not evidence of application correctness. +No ordinary production sources are excluded. Raise the floor as meaningful +protocol and application tests are introduced. A passing test badge means the +required test commands succeeded, even when the Rust harness contains zero tests. + +`scripts/collect-coverage.sh` runs that command, validates the overall line +percentage from temporary `coverage/summary.json`, renders the coverage badge, +and deletes the summary, including on failure. Jenkins then creates the current +build's test and coverage markers only after both required test runs succeed. +The release smoke test remains a nonfatal diagnostic. + +At checkout, CI recreates ignored `coverage/` to remove stale workspace state. +In `post { cleanup { ... } }`, it uses only current-build markers to choose test +status and retain measured coverage, falling back to `failing` and neutral `-%` +when markers are absent. Cleanup reads the version, removes the summary and +markers, validates and archives exactly `coverage/version.svg`, +`coverage/test.svg` and `coverage/coverage.svg` with fingerprints and +`allowEmptyArchive: false`. A later lint/build failure can retain successful +test/coverage results; these badges report those individual stages. + +Only `main` publishes to `/mnt/assets/coverage/idle-org/tiny-wb/main/`, with +`/mnt/assets/coverage` passed as the complete publication root. Publication +runs in cleanup even after failure. Each file is replaced atomically, with +0755 new directories and 0644 files; the script never prunes shared assets. +Concurrent builds of this job are disabled to avoid workspace/marker races. + +The publication predicate requires a Jenkins **multibranch** job's `BRANCH_NAME` +and excludes change requests and tags. The actual job configuration is not in +this repository and remains unverified. When `BRANCH_NAME` is absent, +publication is skipped with a diagnostic; generation and archival still run. +Verify the job type and its branch variable before deployment. Real Jenkins +execution, access to the asset mount, and public URL retrieval are separate +unverified deployment checkpoints; README links alone do not make badges live. + +Server configuration is external: mount public assets read-only, disable +directory listing, serve SVG with a restrictive Content-Security-Policy, and +revalidate mutable URLs or give them a short cache lifetime. + +Run isolated badge checks with `python3 scripts/test-badges.py`. The renderer +was copied from `Dandaedre/agent-tasks` at revision +`fb9d8f7d16f100c2d1377f23394a2302eac9ab18` (retrieved 2026-10-04), with local +fixes for literal newlines in width measurement, oversized integer validation, +unique temporary files, directory-output rejection and signal termination. diff --git a/scripts/ci-coverage-badges.sh b/scripts/ci-coverage-badges.sh new file mode 100755 index 0000000..3a3495f --- /dev/null +++ b/scripts/ci-coverage-badges.sh @@ -0,0 +1,272 @@ +#!/bin/sh +set -eu + +usage() { + cat >&2 <<'EOF' +usage: $0 [options] + +Render one deterministic SVG badge. Defaults: + version --text version --value unknown --color #007ec6 + test --text tests --value passing --color #4c1 + coverage --text coverage --value - --color #9f9f9f --suffix % + +Options: + --text TEXT left-hand label + --value VALUE right-hand value + --color COLOR value background (#RGB or #RRGGBB) + --size PX font size, from 6 through 24 (default: 11) + --height PX badge height, from 14 through 64 (default: 20) + --label-width PX fixed label width instead of automatic sizing + --value-width PX fixed value width instead of automatic sizing + --suffix TEXT text appended to the displayed value + --output PATH output file (default: coverage/.svg) + -h, --help show this help + +Examples: + ./ci-coverage-badges.sh version --value 1.2.3 + ./ci-coverage-badges.sh test --value failing + ./ci-coverage-badges.sh coverage --value 87.4 + ./ci-coverage-badges.sh version --text release --value rc.12 --size 12 --color '#8a2be2' +EOF + exit "${1:-2}" +} + +[ "$#" -ge 1 ] || usage +kind=$1 +shift + +case "$kind" in + version) + text=version + value=unknown + color='#007ec6' + suffix= + output=coverage/version.svg + ;; + test) + text=tests + value=passing + color= + suffix= + output=coverage/test.svg + ;; + coverage) + text=coverage + value=- + color= + suffix=% + output=coverage/coverage.svg + ;; + -h|--help) usage 0 ;; + *) + echo "unsupported badge kind: $kind" >&2 + usage + ;; +esac + +font_size=11 +height=20 +label_width= +value_width= +color_set=false + +while [ "$#" -gt 0 ]; do + option=$1 + case "$option" in + --text|--value|--color|--size|--height|--label-width|--value-width|--suffix|--output) + [ "$#" -ge 2 ] || { + echo "missing value for $option" >&2 + exit 2 + } + option_value=$2 + shift 2 + case "$option" in + --text) text=$option_value ;; + --value) value=$option_value ;; + --color) color=$option_value; color_set=true ;; + --size) font_size=$option_value ;; + --height) height=$option_value ;; + --label-width) label_width=$option_value ;; + --value-width) value_width=$option_value ;; + --suffix) suffix=$option_value ;; + --output) output=$option_value ;; + esac + ;; + -h|--help) usage 0 ;; + *) + echo "unknown option: $option" >&2 + usage + ;; + esac +done + +validate_text() { + field_name=$1 + field_value=$2 + if ! printf '%sX\n' "$field_value" | LC_ALL=C awk ' + BEGIN { valid = 1 } + NR != 1 || $0 !~ /^[ -~]+X$/ || length($0) <= 1 { valid = 0 } + END { exit(valid == 0 ? 1 : 0) } + '; then + echo "$field_name must contain printable ASCII characters: $field_value" >&2 + exit 2 + fi +} + +validate_integer() { + field_name=$1 + field_value=$2 + minimum=$3 + maximum=$4 + case "$field_value" in + ''|*[!0-9]*) + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + ;; + esac + if ! awk -v value="$field_value" -v low="$minimum" -v high="$maximum" 'BEGIN { exit (value >= low && value <= high ? 0 : 1) }'; then + echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2 + exit 2 + fi +} + +validate_text text "$text" +validate_text value "$value" +if [ -n "$suffix" ]; then + validate_text suffix "$suffix" +fi +validate_integer size "$font_size" 6 24 +validate_integer height "$height" 14 64 +if [ -n "$label_width" ]; then + validate_integer label-width "$label_width" 20 1000 +fi +if [ -n "$value_width" ]; then + validate_integer value-width "$value_width" 20 1000 +fi +case "$output" in + ''|*/) echo "output must name an SVG file: $output" >&2; exit 2 ;; +esac + +case "$kind" in + test) + if [ "$color_set" = false ]; then + case "$value" in + passing) color='#4c1' ;; + failing) color='#e05d44' ;; + *) color='#9f9f9f' ;; + esac + fi + ;; + coverage) + if [ "$value" = '-' ]; then + [ "$color_set" = true ] || color='#9f9f9f' + else + if ! awk -v candidate="$value" 'BEGIN { + valid = candidate ~ /^[0-9]+([.][0-9]+)?$/ && candidate >= 0 && candidate <= 100 + exit(valid ? 0 : 1) + }'; then + echo "coverage value must be - or a number from 0 through 100: $value" >&2 + exit 2 + fi + value=$(awk -v candidate="$value" 'BEGIN { printf "%.0f", candidate }') + if [ "$color_set" = false ]; then + if [ "$value" -ge 90 ]; then + color='#4c1' + elif [ "$value" -ge 70 ]; then + color='#dfb317' + else + color='#e05d44' + fi + fi + fi + ;; +esac + +case "$color" in + \#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]|\#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]) ;; + *) echo "color must be #RGB or #RRGGBB: $color" >&2; exit 2 ;; +esac + +display_value=$value$suffix +if [ -z "$label_width" ]; then + label_width=$(printf '%s\n' "$text" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi +if [ -z "$value_width" ]; then + value_width=$(printf '%s\n' "$display_value" | awk -v size="$font_size" '{ + width = int(length($0) * size * 0.64 + 14) + print (width < 35 ? 35 : width) + }') +fi + +badge_width=$((label_width + value_width)) +label_center=$(awk -v width="$label_width" 'BEGIN { printf "%.1f", width / 2 }') +value_center=$(awk -v left="$label_width" -v width="$value_width" 'BEGIN { + printf "%.1f", left + width / 2 +}') +vertical_center=$(awk -v badge_height="$height" 'BEGIN { printf "%.1f", badge_height / 2 }') + +xml_escape() { + sed \ + -e 's/&/\&/g' \ + -e 's//\>/g' \ + -e 's/"/\"/g' +} + +sed_replacement_escape() { + sed -e 's/[\\&|]/\\&/g' +} + +escaped_text=$(printf '%s' "$text" | xml_escape | sed_replacement_escape) +escaped_value=$(printf '%s' "$display_value" | xml_escape | sed_replacement_escape) +escaped_title=$(printf '%s: %s' "$text" "$display_value" | xml_escape | sed_replacement_escape) + +[ ! -d "$output" ] || { echo "output must not be a directory: $output" >&2; exit 2; } +output_dir=${output%/*} +if [ "$output_dir" != "$output" ]; then + mkdir -p "$output_dir" +fi +temporary=$(mktemp "${output}.tmp.XXXXXX") +trap 'rm -f "$temporary"' 0 +trap 'exit 1' HUP INT TERM + +sed \ + -e "s/@BADGE_WIDTH@/$badge_width/g" \ + -e "s/@HEIGHT@/$height/g" \ + -e "s/@LABEL_WIDTH@/$label_width/g" \ + -e "s/@VALUE_WIDTH@/$value_width/g" \ + -e "s/@LABEL_CENTER@/$label_center/g" \ + -e "s/@VALUE_CENTER@/$value_center/g" \ + -e "s/@VERTICAL_CENTER@/$vertical_center/g" \ + -e "s/@FONT_SIZE@/$font_size/g" \ + -e "s/@COLOR@/$color/g" \ + -e "s|@TITLE@|$escaped_title|g" \ + -e "s|@TEXT@|$escaped_text|g" \ + -e "s|@VALUE@|$escaped_value|g" \ + >"$temporary" <<'EOF' + + @TITLE@ + + + + + + + + + + + + + + @TEXT@ + @VALUE@ + + +EOF + +mv "$temporary" "$output" +trap - 0 HUP INT TERM diff --git a/scripts/collect-coverage.sh b/scripts/collect-coverage.sh new file mode 100755 index 0000000..1ad1353 --- /dev/null +++ b/scripts/collect-coverage.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -eu +mkdir -p coverage +summary=coverage/summary.json +trap 'rm -f "$summary"' 0 +trap 'exit 1' HUP INT TERM +# Alias and honest initial floor live in .cargo/config.toml. +cargo coverage +percentage=$(jq -er '.data | if length == 1 then .[0].totals.lines.percent else error("expected one overall total") end | select(type == "number" and . >= 0 and . <= 100)' "$summary") +scripts/ci-coverage-badges.sh coverage --value "$percentage" +rm -f "$summary" diff --git a/scripts/finalize-badges.sh b/scripts/finalize-badges.sh new file mode 100755 index 0000000..c760adf --- /dev/null +++ b/scripts/finalize-badges.sh @@ -0,0 +1,19 @@ +#!/bin/sh +set -eu +: "${BUILD_NUMBER:?BUILD_NUMBER is required}" +case "$BUILD_NUMBER" in *[!0-9]*) exit 1 ;; esac +trap 'rm -f coverage/summary.json "coverage/.tests-passed-${BUILD_NUMBER}" "coverage/.coverage-ready-${BUILD_NUMBER}"' 0 +trap 'exit 1' HUP INT TERM +status=failing +[ ! -f "coverage/.tests-passed-${BUILD_NUMBER}" ] || status=passing +scripts/ci-coverage-badges.sh test --value "$status" +if [ ! -f "coverage/.coverage-ready-${BUILD_NUMBER}" ]; then + scripts/ci-coverage-badges.sh coverage --value - +fi +version=$(cargo metadata --no-deps --locked --format-version 1 | jq -er '.packages[] | select(.name == "tiny-wb") | .version') +scripts/ci-coverage-badges.sh version --value "$version" +rm -f coverage/summary.json "coverage/.tests-passed-${BUILD_NUMBER}" "coverage/.coverage-ready-${BUILD_NUMBER}" +for name in version test coverage; do + file="coverage/$name.svg" + [ ! -L "$file" ] && [ -f "$file" ] && [ -s "$file" ] || exit 1 +done diff --git a/scripts/publish-badges.sh b/scripts/publish-badges.sh new file mode 100755 index 0000000..b913b7a --- /dev/null +++ b/scripts/publish-badges.sh @@ -0,0 +1,46 @@ +#!/bin/sh +# Run from the repository root. The root already includes /coverage. +set -eu +fail() { echo "$*" >&2; exit 1; } +[ "$#" -ge 5 ] || fail 'usage: publish-badges.sh ROOT OWNER PROJECT BRANCH NAME...' +root=$1 owner=$2 project=$3 branch=$4 +shift 4 +case "$root" in /*) ;; *) fail 'publication root must be absolute' ;; esac +[ -d "$root" ] && [ -w "$root" ] || fail 'publication root must exist and be writable' +for component in "$owner" "$project" "$branch"; do + case "$component" in ''|.|..|*[!a-zA-Z0-9_.-]*) fail 'invalid path component' ;; esac +done +seen=' ' +for name do + case "$name" in coverage|test|version) ;; *) fail 'invalid badge name' ;; esac + case "$seen" in *" $name "*) fail 'duplicate badge name' ;; esac + seen="$seen$name " + source="coverage/$name.svg" + [ ! -L "$source" ] && [ -f "$source" ] && [ -s "$source" ] || fail "invalid source: $source" +done +# Explicit chmod makes newly created directories readable even under umask 077. +destination=${root%/} +for component in "$owner" "$project" "$branch"; do + destination="$destination/$component" + if [ "$destination" = "${root%/}/$owner/$project/$branch" ] && [ -L "$destination" ]; then + unlink "$destination" + fi + [ ! -L "$destination" ] || fail "symlink destination: $destination" + if [ ! -e "$destination" ]; then + mkdir -m 0755 "$destination" + chmod 0755 "$destination" + fi + [ -d "$destination" ] || fail "not a directory: $destination" +done +temporary= +trap '[ -z "$temporary" ] || rm -f "$temporary"' 0 +trap 'exit 1' HUP INT TERM +for name do + # Never let mv interpret an existing directory (or link to one) as a target. + [ ! -d "$destination/$name.svg" ] || fail 'badge destination is a directory' + temporary=$(mktemp "$destination/.$name.svg.XXXXXX") + cp "coverage/$name.svg" "$temporary" + chmod 0644 "$temporary" + mv -f "$temporary" "$destination/$name.svg" + temporary= +done diff --git a/scripts/test-badges.py b/scripts/test-badges.py new file mode 100644 index 0000000..de27f74 --- /dev/null +++ b/scripts/test-badges.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Isolated renderer/publication contract checks; no public assets are touched.""" +import os +from pathlib import Path +import stat +import shlex +import shutil +import subprocess +import tempfile +import xml.etree.ElementTree as ET + +REPO = Path(__file__).resolve().parent.parent +RENDER = str(REPO / 'scripts/ci-coverage-badges.sh') +PUBLISH = str(REPO / 'scripts/publish-badges.sh') + + +def run(*args, ok=True): + result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + assert (result.returncode == 0) == ok, (args, result.stderr.decode()) + + +with tempfile.TemporaryDirectory() as directory: + os.chdir(directory) + os.umask(0o077) + for kind in ('version', 'test', 'coverage'): + run(RENDER, kind) + for kind, value, color in [ + ('version', '1.2.3', '#007ec6'), + ('version', '1.2.3-very-long-prerelease.123456789', '#007ec6'), + ('test', 'passing', '#4c1'), ('test', 'failing', '#e05d44'), + ('test', 'pending', '#9f9f9f'), ('coverage', '0', '#e05d44'), + ('coverage', '69', '#e05d44'), ('coverage', '70', '#dfb317'), + ('coverage', '87.4', '#dfb317'), ('coverage', '90', '#4c1'), + ('coverage', '100', '#4c1'), ('coverage', '-', '#9f9f9f'), + ]: + run(RENDER, kind, '--value', value) + root = ET.parse(f'coverage/{kind}.svg').getroot() + assert color in Path(f'coverage/{kind}.svg').read_text() + if value == '1.2.3': + normal_width = int(root.attrib['width']) + if value == '1.2.3-very-long-prerelease.123456789': + assert int(root.attrib['width']) > normal_width + if value == '87.4': + assert root.attrib['aria-label'] == 'coverage: 87%' + run(RENDER, 'version', '--text', 'A<&"', '--value', 'B>&"', '--suffix', '&', + '--size', '14', '--height', '30', '--label-width', '80', + '--value-width', '120', '--color', '#abcdef', '--output', 'custom/badge.svg') + root = ET.parse('custom/badge.svg').getroot() + assert root.attrib['width'] == '200' and root.attrib['height'] == '30' + assert root.attrib['aria-label'] == 'A<&": B>&"&' + Path('directory-output').mkdir() + run(RENDER, 'version', '--output', 'directory-output', ok=False) + original = Path('coverage/coverage.svg').read_bytes() + for args in [ + ['unknown'], ['coverage', '--bogus'], ['coverage', '--value'], + *[['coverage', '--value', v] for v in ('-1', '101', 'nan', '1e2', 'x', '')], + *[['coverage', flag, v] for flag, v in ( + ('--size', '5'), ('--size', '25'), ('--height', '13'), ('--height', '65'), + ('--label-width', '19'), ('--value-width', '1001'), + ('--height', '999999999999999999999999999'), ('--color', 'red'), + ('--color', '#1234567'), ('--text', 'bad\ntext'), ('--value', '\t'))], + ]: + run(RENDER, *args, ok=False) + assert Path('coverage/coverage.svg').read_bytes() == original + assert not list(Path('.').rglob('*.tmp.*')) + for svg in Path('.').rglob('*.svg'): + ET.parse(svg) + root = Path(directory) / 'assets' + root.mkdir() + args = [PUBLISH, str(root), 'idle-org', 'tiny-wb', 'main'] + names = ['version', 'test', 'coverage'] + run(*args, *names) + destination = root / 'idle-org/tiny-wb/main' + for path in (root/'idle-org', root/'idle-org/tiny-wb', destination): + assert stat.S_IMODE(path.stat().st_mode) == 0o755 + old_inode = (destination/'test.svg').stat().st_ino + run(*args, *names) + assert (destination/'test.svg').stat().st_ino != old_inode + for name in names: + target = destination/f'{name}.svg' + assert target.read_bytes() == Path(f'coverage/{name}.svg').read_bytes() + assert stat.S_IMODE(target.stat().st_mode) == 0o644 + run(*args, 'test', 'test', ok=False) + run(*args, 'invalid', ok=False) + run(PUBLISH, 'relative', 'o', 'p', 'main', 'test', ok=False) + run(PUBLISH, str(root/'missing'), 'o', 'p', 'main', 'test', ok=False) + run(PUBLISH, str(root), '..', 'p', 'main', 'test', ok=False) + saved = Path('coverage/test.svg').read_bytes() + Path('coverage/test.svg').unlink() + Path('coverage/test.svg').symlink_to('version.svg') + run(*args, 'test', ok=False) + Path('coverage/test.svg').unlink() + Path('coverage/test.svg').touch() + run(*args, 'test', ok=False) + Path('coverage/test.svg').write_bytes(saved) + (root/'linked').symlink_to(root/'idle-org', target_is_directory=True) + run(PUBLISH, str(root), 'linked', 'tiny-wb', 'main', 'test', ok=False) + (root/'idle-org/linked').symlink_to(root/'idle-org/tiny-wb', target_is_directory=True) + run(PUBLISH, str(root), 'idle-org', 'linked', 'main', 'test', ok=False) + (root/'file').touch() + run(PUBLISH, str(root), 'file', 'p', 'main', 'test', ok=False) + # Migration unlinks the old branch link, preserving its target and contents. + old = destination.with_name('old') + destination.rename(old) + destination.symlink_to(old, target_is_directory=True) + run(*args, *names) + assert destination.is_dir() and not destination.is_symlink() + assert (old/'test.svg').read_bytes() == saved + # Failed copy, chmod and rename must preserve the published badge and + # remove the unfinished temporary file. + tools = Path(directory) / 'fake-bin' + tools.mkdir() + old_path = os.environ['PATH'] + for command in ('cp', 'chmod', 'mv'): + stub = tools / command + stub.write_text('#!/bin/sh\nexit 1\n') + stub.chmod(0o755) + os.environ['PATH'] = str(tools) + ':' + old_path + run(*args, 'test', ok=False) + os.environ['PATH'] = old_path + stub.unlink() + assert (destination/'test.svg').read_bytes() == saved + assert not list(destination.glob('.*.svg.*')) + stub = tools / 'mv' + stub.write_text('#!/bin/sh\nkill -TERM "$PPID"\nexit 1\n') + stub.chmod(0o755) + os.environ['PATH'] = str(tools) + ':' + old_path + run(*args, 'test', ok=False) + run(RENDER, 'coverage', '--value', '50', ok=False) + os.environ['PATH'] = old_path + stub.unlink() + assert Path('coverage/coverage.svg').read_bytes() == original + assert not list(Path('coverage').glob('*.tmp.*')) + assert not list(destination.glob('.*.svg.*')) + # Exercise cleanup with real Cargo metadata from this repository while + # keeping all generated files in the isolated directory. + Path('scripts').symlink_to(REPO/'scripts', target_is_directory=True) + cargo = tools/'cargo' + cargo.write_text('#!/bin/sh\nexec ' + shlex.quote(shutil.which('cargo')) + + ' metadata --manifest-path ' + shlex.quote(str(REPO/'Cargo.toml')) + + ' --no-deps --locked --format-version 1\n') + cargo.chmod(0o755) + os.environ['PATH'] = str(tools) + ':' + old_path + os.environ['BUILD_NUMBER'] = '123' + for marker in ('tests-passed', 'coverage-ready'): + Path(f'coverage/.{marker}-122').touch() + run(str(REPO/'scripts/finalize-badges.sh')) + assert ET.parse('coverage/test.svg').getroot().attrib['aria-label'] == 'tests: failing' + assert ET.parse('coverage/coverage.svg').getroot().attrib['aria-label'] == 'coverage: -%' + run(RENDER, 'coverage', '--value', '87.4') + for marker in ('tests-passed', 'coverage-ready'): + Path(f'coverage/.{marker}-123').touch() + run(str(REPO/'scripts/finalize-badges.sh')) + assert ET.parse('coverage/test.svg').getroot().attrib['aria-label'] == 'tests: passing' + assert ET.parse('coverage/coverage.svg').getroot().attrib['aria-label'] == 'coverage: 87%' + assert not list(Path('coverage').glob('.*-123')) + os.environ['PATH'] = old_path + cargo.unlink() + root.chmod(0o555) + if os.geteuid() != 0: + run(*args, 'test', ok=False) + root.chmod(0o700) + (destination/'test.svg').unlink() + (destination/'test.svg').mkdir() + run(*args, 'test', ok=False) + assert not list(destination.glob('.*.svg.*')) + +readme = (REPO/'README.md').read_text() +for name in ('version', 'test', 'coverage'): + assert readme.count(f'https://assets.dandaedre.com/coverage/idle-org/tiny-wb/main/{name}.svg') == 1 +pipeline = (REPO/'Jenkinsfile').read_text() +assert "artifacts: 'coverage/version.svg,coverage/test.svg,coverage/coverage.svg'" in pipeline +assert 'fingerprint: true, allowEmptyArchive: false' in pipeline +print('Badge rendering, XML, validation, publication, README and archive checks passed.')