完善本地质量门禁与资金链路测试

This commit is contained in:
yml2213
2026-09-02 13:50:19 +08:00
parent dd2b45bd2c
commit 208f12ddb4
80 changed files with 12245 additions and 11541 deletions
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKEND_DIR="${ROOT_DIR}/backend"
PROFILE="$(mktemp -t hfb-backend-cover.XXXXXX)"
OUTPUT="$(mktemp -t hfb-backend-cover-output.XXXXXX)"
trap 'rm -f "${PROFILE}" "${OUTPUT}"' EXIT
cd "${BACKEND_DIR}"
go test -p 1 -count=1 -coverprofile="${PROFILE}" ./... | tee "${OUTPUT}"
coverage_for_package() {
local package="$1"
awk -v package="${package}" '
$1 == "ok" && $2 == package {
for (i = 1; i <= NF; i++) {
if ($i == "coverage:") {
value = $(i + 1)
gsub(/%/, "", value)
print value
exit
}
}
}
' "${OUTPUT}"
}
require_at_least() {
local label="$1"
local actual="$2"
local minimum="$3"
if [[ -z "${actual}" ]]; then
printf '[coverage] 无法读取 %s 覆盖率\n' "${label}" >&2
return 1
fi
if ! awk -v actual="${actual}" -v minimum="${minimum}" 'BEGIN { exit !(actual + 0 >= minimum + 0) }'; then
printf '[coverage] %s 覆盖率 %s%%,低于门禁 %s%%\n' "${label}" "${actual}" "${minimum}" >&2
return 1
fi
printf '[coverage] %s %s%%(门禁 %s%%)\n' "${label}" "${actual}" "${minimum}"
}
total="$(go tool cover -func="${PROFILE}" | awk '/^total:/ { gsub(/%/, "", $3); print $3 }')"
require_at_least "后端总语句" "${total}" "30.0"
require_at_least "order" "$(coverage_for_package hfb_sys/backend/internal/modules/order)" "40.0"
require_at_least "payment" "$(coverage_for_package hfb_sys/backend/internal/modules/payment)" "20.0"
require_at_least "wallet" "$(coverage_for_package hfb_sys/backend/internal/modules/wallet)" "34.0"
require_at_least "withdrawal" "$(coverage_for_package hfb_sys/backend/internal/modules/withdrawal)" "30.0"
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MAX_LINES="${VUE_MAX_LINES:-1800}"
failed=0
while IFS= read -r -d '' file; do
lines="$(wc -l < "${file}")"
if (( lines > MAX_LINES )); then
printf '[vue-size] %s %s 行,超过上限 %s\n' "${file#${ROOT_DIR}/}" "${lines}" "${MAX_LINES}" >&2
failed=1
fi
done < <(find "${ROOT_DIR}/frontend/src" -name '*.vue' -print0)
if (( failed != 0 )); then
exit 1
fi
printf '[vue-size] 所有 Vue 页面均不超过 %s 行\n' "${MAX_LINES}"
+4 -2
View File
@@ -97,7 +97,7 @@ if [[ "${SKIP_BACKEND}" == "0" ]]; then
run_step "go build ./..." go build ./... || FAILED=1
if [[ "${SKIP_TESTS}" == "0" ]]; then
# -p 1 串行避免 SQLite 内存库并发; 真实 MySQL e2e 默认 skip
run_step "go test ./..." go test -p 1 -count=1 ./... || FAILED=1
run_step "go test + coverage" bash "${ROOT_DIR}/scripts/check-backend-coverage.sh" || FAILED=1
else
log_warn "跳过后端测试 (--skip-tests)"
fi
@@ -112,6 +112,8 @@ if [[ "${SKIP_FRONTEND}" == "0" ]]; then
log "前端检查目录: ${FRONTEND_DIR}"
cd "${FRONTEND_DIR}"
run_step "Vue 页面大小门禁" bash "${ROOT_DIR}/scripts/check-vue-size.sh" || FAILED=1
if [[ ! -d node_modules ]]; then
run_step "npm ci (首次安装依赖)" npm ci || FAILED=1
fi
@@ -119,7 +121,7 @@ if [[ "${SKIP_FRONTEND}" == "0" ]]; then
# npm run check = prettier --check + eslint --max-warnings=0 + vue-tsc
run_step "npm run check (prettier + eslint + typecheck)" npm run check || FAILED=1
if [[ "${SKIP_TESTS}" == "0" ]]; then
run_step "npm run test (vitest)" npm run test || FAILED=1
run_step "npm run test + coverage (vitest)" npm run test:coverage || FAILED=1
else
log_warn "跳过前端测试 (--skip-tests)"
fi
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# 将大型 SFC 的内联 scoped CSS 外置,保留 <style scoped src="..."> 的编译作用域。
while IFS= read -r -d '' file; do
if (( $(wc -l < "${file}") <= 900 )); then
continue
fi
if ! rg -q '<style scoped>' "${file}"; then
continue
fi
base="${file%.vue}"
css="${base}.css"
awk '
/<style scoped>/ { inside = 1; next }
/<\/style>/ { inside = 0; next }
inside { print }
' "${file}" > "${css}"
name="$(basename "${css}")"
perl -0pi -e "s#<style scoped>.*?</style>#<style scoped src=\"./${name}\"></style>#s" "${file}"
done < <(find "${ROOT_DIR}/frontend/src" -name '*.vue' -print0)