Files
gitea/tools/check-wiki-rules.sh
T
jiantw83 653d54476b test(check-wiki-rules): 頁型驗證涵蓋 SKILLSET,說明改為不綁類型數量
What:`tools/check-wiki-rules.sh` 的 `TYPES` 加入 `SKILLSET`,檔頭的「涵蓋全部十一種頁面類型」改成「涵蓋 `TYPES` 列的每一種頁面類型」,誘餌值那段註解的「其他八個類型」改成「其餘每一個類型」。

Why:驗證清單沒跟上,新頁型的三項判定(專用變數優先、退回共用變數、不得跨類型代用)就一次都沒驗到。註解裡寫死數字更麻煩:每加一種頁型就要記得改兩個數字,忘了改註解就開始騙人——事實上加 `CHECK` 與 `REPORT` 那次就已經漏了一個。

How:只加 `TYPES` 一個字串,三項判定與誘餌值都是照 `TYPES` 展開的,加進去就自動涵蓋,不必另外寫測項。註解一律改成指向 `TYPES` 這個變數,不再寫死數量,日後加頁型只要改一個地方。

Who:`jsc-gitea` 的 wiki 頁型解析驗證,以及日後新增頁面類型的人。
2026-08-27 16:34:17 +08:00

93 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env sh
# check-wiki-rules — 驗證 wiki repo 解析與 hash-id 規則。
# 涵蓋 TYPES 列的每一種頁面類型,每種三項:專用變數優先、退回共用變數、不得跨類型代用。
# 全部通過印 OK 並 exit 0;任一項不符印出差異並 exit 1。
set -eu
dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
gitea="$dir/gitea.sh"
hash_id="$dir/hash-id"
TYPES='QUESTION PLAN ANALYZE DELIVER MAINTAIN REPO LOG LEARN ERROR CHECK REPORT SKILLSET'
fail() {
printf '%s\n' "$1" >&2
exit 1
}
expect_eq() {
got=$1
want=$2
label=$3
[ "$got" = "$want" ] || fail "$label: want=$want got=$got"
}
check_repo() { # type label want [VAR=值...]
type=$1
label=$2
want=$3
shift 3
got=$(env -i PATH="${PATH:-/usr/bin:/bin}" "$@" "$gitea" wiki-repo "$type")
expect_eq "$got" "$want" "$label"
}
check_no_repo() { # type label [VAR=值...]:期望 exit 3 且不印出任何 {owner}/{repo}
type=$1
label=$2
shift 2
if got=$(env -i PATH="${PATH:-/usr/bin:/bin}" "$@" "$gitea" wiki-repo "$type" 2>/dev/null); then
code=0
else
code=$?
fi
[ "$code" -eq 3 ] || fail "$label: want exit=3 got exit=$code output=$got"
[ -z "$got" ] || fail "$label: want no output got=$got"
}
check_hash() {
input=$1
want=$2
got=$("$hash_id" "$input")
expect_eq "$got" "$want" "hash-id $input"
}
# 其餘每一個類型的誘餌值。跨類型代用一旦發生,回傳的就會是 decoy/{別的類型}。
decoys() { # $1=要排除的類型
for t in $TYPES; do
[ "$t" = "$1" ] || printf 'JSC_WIKI_REPO_%s=decoy/%s ' "$t" "$t"
done
}
for ty in $TYPES; do
# 1. 自己的變數贏過共用變數,也贏過其他類型的誘餌。
check_repo "$ty" "$ty specific wins" "own/$ty" \
$(decoys "$ty") "JSC_WIKI_REPO_$ty=own/$ty" JSC_WIKI_REPO='shared/wiki'
# 2. 自己的變數未設定時,只退回共用變數。
check_repo "$ty" "$ty falls back to shared only" 'shared/wiki' \
$(decoys "$ty") JSC_WIKI_REPO='shared/wiki'
# 3. 自己的變數與共用變數都沒有時,exit 3,不借用別的類型。
check_no_repo "$ty" "$ty never borrows another type" $(decoys "$ty")
done
# 未知類型 exit 2,與「設定不足」的 exit 3 分開。
if got=$(env -i PATH="${PATH:-/usr/bin:/bin}" JSC_WIKI_REPO='shared/wiki' \
"$gitea" wiki-repo NOSUCH 2>/dev/null); then
code=0
else
code=$?
fi
[ "$code" -eq 2 ] || fail "unknown type: want exit=2 got exit=$code"
[ -z "$got" ] || fail "unknown type: want no output got=$got"
# hash-id:首碼 0-9/A/B/C 改成 H 加前 7 碼;其餘首碼原樣輸出 8 碼大寫。
check_hash 'case-2' 'H5172CB7'
check_hash 'case-11' 'HA9A6662'
check_hash 'case-1' 'HB6EC7FD'
check_hash 'case-12' 'HCCA8D42'
check_hash 'case-3' 'D3E5AA27'
check_hash 'case-7' 'D794C002'
printf '%s\n' 'OK'