Files
crank/scripts/check-rust-code-health.sh
T
github-ops 81efa2f0fb
Deploy / deploy (push) Successful in 29s
CI / Rust Checks (push) Failing after 4m51s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Deployment Manifests (push) Has been skipped
Enforce Rust code health gate
2026-06-20 20:53:46 +00:00

72 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="${CRANK_RUST_HEALTH_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
DEFAULT_MAX_LINES="${CRANK_RUST_HEALTH_DEFAULT_MAX_LINES:-1000}"
# Existing debt baseline. New files must stay under DEFAULT_MAX_LINES. These
# files are allowed to exist, but CI fails if they grow further.
declare -A FILE_LIMITS=(
["apps/admin-api/src/service.rs"]=4112
["apps/admin-api/src/app.rs"]=3823
["apps/mcp-server/src/main.rs"]=3505
["crates/crank-runtime/src/executor.rs"]=2829
["crates/crank-registry/src/postgres/mod.rs"]=2154
["crates/crank-community-mcp/src/app.rs"]=1647
["crates/crank-registry/src/postgres/operation.rs"]=1022
)
status=0
echo "Rust code health: checking file size limits"
while IFS= read -r file; do
rel="${file#"$ROOT_DIR"/}"
lines="$(wc -l < "$file" | tr -d ' ')"
limit="${FILE_LIMITS[$rel]:-$DEFAULT_MAX_LINES}"
if (( lines > limit )); then
echo "error: $rel has $lines lines; limit is $limit" >&2
status=1
fi
done < <(
find "$ROOT_DIR" \
-path "$ROOT_DIR/target" -prune -o \
-path "$ROOT_DIR/apps/ui/node_modules" -prune -o \
-name '*.rs' -print | sort
)
echo "Rust code health: checking new large inline test modules"
while IFS=: read -r file line _; do
rel="${file#"$ROOT_DIR"/}"
if [[ -n "${FILE_LIMITS[$rel]:-}" ]]; then
continue
fi
lines="$(wc -l < "$file" | tr -d ' ')"
if (( lines > DEFAULT_MAX_LINES / 2 )); then
echo "error: $rel contains inline tests and has $lines lines; move integration-style tests to tests/" >&2
status=1
fi
done < <(
grep -RIn --include='*.rs' '^[[:space:]]*mod tests[[:space:]]*{' \
"$ROOT_DIR/apps" "$ROOT_DIR/crates" || true
)
if (( status != 0 )); then
cat >&2 <<'EOF'
Rust code health failed.
Rules:
- new Rust files must stay at or below 1000 lines;
- existing large files are pinned to the current baseline and must not grow;
- large inline test modules are forbidden outside the legacy allowlist;
- when touching a large file, extract modules or move integration-style tests to tests/.
EOF
fi
exit "$status"