From eacfc6ddc5bf9261606181d299594948335a6c9c Mon Sep 17 00:00:00 2001 From: FORGE Date: Mon, 30 Nov 2026 16:55:06 +0800 Subject: [PATCH] test(motd): pin dynamic inputs for the NO_COLOR comparison --- tests/test-motd.sh | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/test-motd.sh b/tests/test-motd.sh index 15771fb..98f6bd7 100644 --- a/tests/test-motd.sh +++ b/tests/test-motd.sh @@ -354,9 +354,40 @@ physical_out=$(COLUMNS=32 PATH="$TEST_DIR/bin-physical-wide:$PATH" bash "$TEST_D [[ "$(join_text "$physical_out")" == *"usnc-prod-edge-node-hkg-01.internal.example.com"* ]] || fail "long hostname lost at 32 columns"; pass "payload keeps physical core and thread semantics at 32 columns" -plain_out=$(env NO_COLOR=1 COLUMNS=80 bash "$TEST_DIR/t"); +# NO_COLOR 断言必须比较同一份输入:运行时间、负载、内存和根盘每次都会变, +# 直接对两次实时取样做整体字符串相等会随宿主波动误报。这里用一个最薄的包装脚本, +# 只把 /proc/uptime、/proc/loadavg、/proc/meminfo 和 df 换成固定值,其余命令照常, +# 再让带色与无色两次运行读同一夹具。断言仍是去除 ANSI 后的整体逐字节相等, +# 文字、列对齐和执行顺序的任何回归都会命中。 +mkdir "$TEST_DIR/bin-stable" +cat >"$TEST_DIR/bin-stable/motd-fixture"<<'FIXTURE' +#!/usr/bin/env bash +awk() { + case "${!#}" in + /proc/uptime) printf '%s\n' '86400.42 0.00 0.00' | command awk "${@:1:$#-1}" ;; + /proc/loadavg) printf '%s\n' '0.13 0.27 0.41 2/456 7890' | command awk "${@:1:$#-1}" ;; + /proc/meminfo) printf '%s\n' 'MemTotal: 32768000 kB' 'MemAvailable: 16384000 kB' | command awk "${@:1:$#-1}" ;; + *) command awk "$@" ;; + esac +} +df() { + if [[ "$1" == -Ph ]]; then + printf '%s\n' 'Filesystem Size Used Avail Use% Mounted on' '/dev/vda1 100G 75G 25G 75% /' + else + printf '%s\n' 'Filesystem 1024-blocks Used Available Capacity Mounted on' '/dev/vda1 104857600 78643200 26214400 75% /' + fi +} +. "$1" +FIXTURE +chmod 0700 "$TEST_DIR/bin-stable/motd-fixture"; +plain_out=$(env NO_COLOR=1 COLUMNS=80 bash "$TEST_DIR/bin-stable/motd-fixture" "$TEST_DIR/t"); +colored_out=$(COLUMNS=80 bash "$TEST_DIR/bin-stable/motd-fixture" "$TEST_DIR/t"); +colored_text=$(printf '%s' "$colored_out"|sed -E $'s/\033\[[0-9;]*m//g'); [[ "$plain_out" != *$'\033'* ]] || fail "NO_COLOR output still contains escape sequences"; -[[ "$(join_text "$plain_out")" == "$(join_text "$(COLUMNS=80 bash "$TEST_DIR/t")")" ]] || fail "NO_COLOR text differs from colored text"; +[[ "$colored_out" == *$'\033'* ]] || fail "colored run lost escape sequences"; +# 只钉一个动态字段,确认两次运行确实读到了夹具而不是宿主实时值。 +[[ "$colored_text" == *'0.13 / 0.27 / 0.41'* ]] || fail "stable fixture inputs were not applied"; +[[ "$plain_out" == "$colored_text" ]] || fail "NO_COLOR text differs from colored text"; pass "payload honours NO_COLOR without changing text" dumb_out=$(env TERM=dumb COLUMNS=80 bash "$TEST_DIR/t");