feat(utils): add global test runner and helper help
This commit is contained in:
@@ -47,6 +47,34 @@ render_template() {
|
||||
"$src" > "$dest"
|
||||
}
|
||||
|
||||
# 顯示說明
|
||||
show_help() {
|
||||
cat << EOF
|
||||
LeetCode Practice Helper
|
||||
|
||||
用法:
|
||||
$0 problem <題號> <題目名稱-kebab> <難度>
|
||||
$0 log [YYYY-MM]
|
||||
$0 readme
|
||||
$0 -h | --help
|
||||
|
||||
說明:
|
||||
problem 建立新題目(會建立 C#/Go 骨架、測試模板與 README)
|
||||
log 建立月度學習日誌(預設為本月,或指定 YYYY-MM)
|
||||
readme 顯示題目數量統計(可擴充為寫回 README)
|
||||
|
||||
範例:
|
||||
$0 problem 1 two-sum Easy
|
||||
$0 problem 3025 find-the-number-of-ways-to-place-people-i Medium
|
||||
$0 log 2025-09
|
||||
$0 readme
|
||||
|
||||
測試:
|
||||
建議使用 utils/run_tests.sh 執行,例:
|
||||
utils/run_tests.sh 3025 [all|csharp|go]
|
||||
EOF
|
||||
}
|
||||
|
||||
# 建立測試檔案(來自 templates/problem/test/*)
|
||||
create_test_files() {
|
||||
local problem_dir="$1"; local number="$2"; local name_slug="$3"; local difficulty="$4"
|
||||
@@ -71,10 +99,7 @@ create_test_files() {
|
||||
"$problem_dir/test/edge_cases.md" \
|
||||
"$number" "$number_pad" "$name_slug" "$name_title" "$difficulty" "$today" "$year_month"
|
||||
|
||||
render_template "$TEMPLATES_DIR/problem/test/run_tests.sh.tmpl" \
|
||||
"$problem_dir/test/run_tests.sh" \
|
||||
"$number" "$number_pad" "$name_slug" "$name_title" "$difficulty" "$today" "$year_month"
|
||||
chmod +x "$problem_dir/test/run_tests.sh"
|
||||
# 不再產生每題的 run_tests.sh,請改用 utils/run_tests.sh
|
||||
}
|
||||
|
||||
# 建立新題目
|
||||
@@ -118,7 +143,7 @@ create_problem() {
|
||||
echo "✅ 已建立題目: $problem_dir"
|
||||
echo "C#: cd problems/${folder_name}/csharp && dotnet run"
|
||||
echo "Go: cd problems/${folder_name}/go && go run main.go"
|
||||
echo "測試: cd problems/${folder_name}/test && ./run_tests.sh"
|
||||
echo "測試: utils/run_tests.sh ${number_pad}-${name_slug} [all|csharp|go]"
|
||||
}
|
||||
|
||||
# 更新主 README(目前僅統計列印)
|
||||
@@ -143,6 +168,13 @@ create_monthly_log() {
|
||||
}
|
||||
|
||||
# 入口
|
||||
|
||||
# --help
|
||||
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
||||
show_help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
case "${1:-}" in
|
||||
problem)
|
||||
create_problem "${2:-}" "${3:-}" "${4:-}" ;;
|
||||
@@ -151,10 +183,6 @@ case "${1:-}" in
|
||||
log)
|
||||
create_monthly_log "${2:-}" ;;
|
||||
*)
|
||||
echo "LeetCode Practice Helper"
|
||||
echo "使用:"
|
||||
echo " $0 problem <題號> <題目名稱-kebab> <難度>"
|
||||
echo " $0 readme"
|
||||
echo " $0 log [YYYY-MM]"
|
||||
show_help
|
||||
;;
|
||||
esac
|
||||
|
125
utils/run_tests.sh
Executable file
125
utils/run_tests.sh
Executable file
@@ -0,0 +1,125 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# Usage:
|
||||
# utils/run_tests.sh <problem> [all|csharp|go]
|
||||
# Examples:
|
||||
# utils/run_tests.sh 3025
|
||||
# utils/run_tests.sh 3025 csharp
|
||||
# utils/run_tests.sh problems/3025-find-the-number-of-ways-to-place-people-i go
|
||||
# utils/run_tests.sh 3516-find-closest-person all
|
||||
# Help:
|
||||
# utils/run_tests.sh -h | --help
|
||||
# List:
|
||||
# utils/run_tests.sh --list
|
||||
|
||||
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
|
||||
|
||||
show_help() {
|
||||
cat << EOF
|
||||
Run tests for a problem folder.
|
||||
|
||||
Usage:
|
||||
$0 <problem> [all|csharp|go]
|
||||
$0 --list
|
||||
|
||||
Where <problem> can be:
|
||||
- A number (e.g., 3025)
|
||||
- A 4-digit + slug folder name (e.g., 3025-find-the-number-of-ways-to-place-people-i)
|
||||
- A relative path under problems/ (e.g., problems/3025-find-the-number-of-ways-to-place-people-i)
|
||||
|
||||
Examples:
|
||||
$0 3025 # run both C# and Go tests
|
||||
$0 3025 csharp # only C#
|
||||
$0 3516 go # only Go
|
||||
$0 3516-find-closest-person all
|
||||
$0 problems/3516-find-closest-person all
|
||||
$0 --list # list available problems
|
||||
EOF
|
||||
}
|
||||
|
||||
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
||||
show_help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "${1:-}" == "--list" ]]; then
|
||||
echo "Available problems (from problems/):"
|
||||
# List directories matching 4-digit prefix
|
||||
find "$ROOT_DIR/problems" -maxdepth 1 -mindepth 1 -type d -printf "%f\n" \
|
||||
| sort \
|
||||
| awk 'BEGIN{printf("%-8s %s\n","Number","Slug"); printf("%-8s %s\n","------","----");}
|
||||
{num=substr($0,1,4); slug=substr($0,6); printf("%-8s %s\n", num, $0)}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
problem_arg="${1:-}"
|
||||
lang="${2:-all}"
|
||||
|
||||
if [[ -z "$problem_arg" ]]; then
|
||||
show_help >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
resolve_problem_dir() {
|
||||
local arg="$1"
|
||||
if [[ -d "$ROOT_DIR/$arg" ]]; then
|
||||
echo "$ROOT_DIR/$arg"
|
||||
return 0
|
||||
fi
|
||||
if [[ "$arg" =~ ^[0-9]+$ ]]; then
|
||||
local num=$(printf "%04d" "$arg")
|
||||
local match
|
||||
match=$(find "$ROOT_DIR/problems" -maxdepth 1 -type d -name "${num}-*" | head -n 1 || true)
|
||||
if [[ -n "$match" ]]; then
|
||||
echo "$match"; return 0
|
||||
fi
|
||||
fi
|
||||
# try exact name under problems
|
||||
if [[ -d "$ROOT_DIR/problems/$arg" ]]; then
|
||||
echo "$ROOT_DIR/problems/$arg"; return 0
|
||||
fi
|
||||
echo "Cannot resolve problem directory from '$arg'" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
PROB_DIR=$(resolve_problem_dir "$problem_arg")
|
||||
TEST_DIR="$PROB_DIR/test"
|
||||
CS_DIR="$PROB_DIR/csharp"
|
||||
GO_DIR="$PROB_DIR/go"
|
||||
|
||||
run_csharp() {
|
||||
if [[ ! -d "$CS_DIR" || ! -d "$TEST_DIR" ]]; then
|
||||
echo "[C#] Skipped (missing csharp or test folder)"; return 0
|
||||
fi
|
||||
echo "📋 C# 測試結果:"
|
||||
if (cd "$CS_DIR" && dotnet build >/dev/null 2>&1); then
|
||||
(cd "$TEST_DIR" && dotnet test --logger "console;verbosity=minimal") || { echo "❌ C# 測試失敗"; return 1; }
|
||||
echo "✅ C# 測試通過"
|
||||
else
|
||||
echo "❌ C# 編譯失敗"; return 1
|
||||
fi
|
||||
}
|
||||
|
||||
run_go() {
|
||||
if [[ ! -d "$GO_DIR" || ! -d "$TEST_DIR" ]]; then
|
||||
echo "[Go] Skipped (missing go or test folder)"; return 0
|
||||
fi
|
||||
echo "📋 Go 測試結果:"
|
||||
if (cd "$GO_DIR" && go build >/dev/null 2>&1); then
|
||||
(cd "$TEST_DIR" && go test -v) || { echo "❌ Go 測試失敗"; return 1; }
|
||||
echo "✅ Go 測試通過"
|
||||
else
|
||||
echo "❌ Go 編譯失敗"; return 1
|
||||
fi
|
||||
}
|
||||
|
||||
case "$lang" in
|
||||
all) run_csharp; run_go ;;
|
||||
csharp) run_csharp ;;
|
||||
go) run_go ;;
|
||||
*) echo "Unknown lang '$lang' (use all|csharp|go)" >&2; exit 1 ;;
|
||||
esac
|
||||
|
||||
echo "📊 測試完成: $PROB_DIR"
|
Reference in New Issue
Block a user