feat(utils): add global test runner and helper help
This commit is contained in:
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