Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions tests/test-motd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down