Files
crank/scripts/check-rust-code-health.sh
github-ops a31862aeeb
Deploy / deploy (push) Successful in 1m35s
CI / Rust Checks (push) Failing after 5m44s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Deployment Manifests (push) Has been skipped
Refactor large Rust integration tests
2026-06-21 07:12:33 +00:00

104 lines
3.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}"
INLINE_TEST_MAX_LINES="${CRANK_RUST_HEALTH_INLINE_TEST_MAX_LINES:-$(( DEFAULT_MAX_LINES / 2 ))}"
# 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 large inline test modules"
if ! python3 - "$ROOT_DIR" "$INLINE_TEST_MAX_LINES" <<'PY'
from pathlib import Path
import sys
root = Path(sys.argv[1])
limit = int(sys.argv[2])
status = 0
def scan_roots():
for base_name in ("apps", "crates"):
base = root / base_name
if base.exists():
yield from base.rglob("*.rs")
for path in sorted(scan_roots()):
parts = set(path.relative_to(root).parts)
if "target" in parts or "node_modules" in parts or "tests" in parts:
continue
lines = path.read_text(encoding="utf-8").splitlines()
for index, line in enumerate(lines):
if line.strip() != "mod tests {":
continue
brace_depth = 0
end_index = len(lines) - 1
for cursor in range(index, len(lines)):
brace_depth += lines[cursor].count("{") - lines[cursor].count("}")
if cursor > index and brace_depth == 0:
end_index = cursor
break
module_lines = end_index - index + 1
if module_lines > limit:
rel = path.relative_to(root)
print(
f"error: {rel} contains inline tests at line {index + 1} "
f"with {module_lines} lines; limit is {limit}; "
"move integration-style tests to tests/",
file=sys.stderr,
)
status = 1
raise SystemExit(status)
PY
then
status=1
fi
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, including in legacy large files;
- when touching a large file, extract modules or move integration-style tests to tests/.
EOF
fi
exit "$status"