1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-16 09:31:11 +00:00

feat: Add conditional colorization to BATS test output based on terminal capabilities and formatter.

This commit is contained in:
Tw93
2026-01-03 13:59:19 +08:00
parent 8cb1bc6fbe
commit 8db91730b3

View File

@@ -20,6 +20,15 @@ echo ""
FAILED=0 FAILED=0
report_unit_result() {
if [[ $1 -eq 0 ]]; then
printf "${GREEN}${ICON_SUCCESS} Unit tests passed${NC}\n"
else
printf "${RED}${ICON_ERROR} Unit tests failed${NC}\n"
((FAILED++))
fi
}
echo "1. Linting test scripts..." echo "1. Linting test scripts..."
if command -v shellcheck > /dev/null 2>&1; then if command -v shellcheck > /dev/null 2>&1; then
TEST_FILES=() TEST_FILES=()
@@ -49,19 +58,53 @@ if command -v bats > /dev/null 2>&1 && [ -d "tests" ]; then
if [[ $# -eq 0 ]]; then if [[ $# -eq 0 ]]; then
set -- tests set -- tests
fi fi
use_color=false
if [[ -t 1 && "${TERM:-}" != "dumb" ]]; then
use_color=true
fi
if bats --help 2>&1 | grep -q -- "--formatter"; then if bats --help 2>&1 | grep -q -- "--formatter"; then
if TERM="${TERM:-xterm-256color}" bats --formatter "${BATS_FORMATTER:-pretty}" "$@"; then formatter="${BATS_FORMATTER:-pretty}"
printf "${GREEN}${ICON_SUCCESS} Unit tests passed${NC}\n" if [[ "$formatter" == "tap" ]]; then
if $use_color; then
esc=$'\033'
if bats --formatter tap "$@" | \
sed -e "s/^ok /${esc}[32mok ${esc}[0m /" \
-e "s/^not ok /${esc}[31mnot ok ${esc}[0m /"; then
report_unit_result 0
else
report_unit_result 1
fi
else
if bats --formatter tap "$@"; then
report_unit_result 0
else
report_unit_result 1
fi
fi
else else
printf "${RED}${ICON_ERROR} Unit tests failed${NC}\n" # Pretty format for local development
((FAILED++)) if bats --formatter "$formatter" "$@"; then
report_unit_result 0
else
report_unit_result 1
fi
fi fi
else else
if TERM="${TERM:-xterm-256color}" bats --tap "$@" | sed -e 's/^ok /OK /' -e 's/^not ok /FAIL /'; then if $use_color; then
printf "${GREEN}${ICON_SUCCESS} Unit tests passed${NC}\n" esc=$'\033'
if bats --tap "$@" | \
sed -e "s/^ok /${esc}[32mok ${esc}[0m /" \
-e "s/^not ok /${esc}[31mnot ok ${esc}[0m /"; then
report_unit_result 0
else
report_unit_result 1
fi
else else
printf "${RED}${ICON_ERROR} Unit tests failed${NC}\n" if bats --tap "$@"; then
((FAILED++)) report_unit_result 0
else
report_unit_result 1
fi
fi fi
fi fi
else else