#!/usr/bin/env fish # 回归:ff 日常命令按间隔自动更新 ~/.dotfile set -g REPO_ROOT (cd (dirname (status filename))/../.. && pwd) source $REPO_ROOT/fish/functions/ff.fish set -e GIT_DIR GIT_WORK_TREE GIT_COMMON_DIR set -g _t_ok 0 set -g _t_fail 0 function t_ok --argument-names msg set -g _t_ok (math $_t_ok + 1) echo "ok - $msg" end function t_fail --argument-names msg set -g _t_fail (math $_t_fail + 1) echo "not ok - $msg" end function t_assert --argument-names cond msg if test $cond -eq 0 t_ok $msg else t_fail $msg end end function setup_env set -g TEST_HOME (mktemp -d) set -gx HOME $TEST_HOME set -e FF_NO_AUTO_UPDATE mkdir -p $HOME/.config/fish $HOME/.config/ff $HOME/.local/share/ff echo '# test fish config' >$HOME/.config/fish/config.fish set -g UPSTREAM $TEST_HOME/upstream mkdir -p $UPSTREAM/fish/functions echo init >$UPSTREAM/README echo '# stub' >$UPSTREAM/fish/functions/stub.fish git init -q -b main $UPSTREAM git -C $UPSTREAM config user.name Test git -C $UPSTREAM config user.email test@example.com git -C $UPSTREAM config commit.gpgsign false git -C $UPSTREAM add . git -C $UPSTREAM commit -q -m init git clone -q $UPSTREAM $HOME/.dotfile git -C $HOME/.dotfile config pull.ff only git -C $HOME/.dotfile config user.name Test git -C $HOME/.dotfile config user.email test@example.com git -C $HOME/.dotfile config commit.gpgsign false end function cleanup_env if set -q TEST_HOME rm -rf $TEST_HOME end end function add_upstream_marker --argument-names name echo $name >$UPSTREAM/$name git -C $UPSTREAM add $name git -C $UPSTREAM commit -q -m $name end function write_state --argument-names ts echo "last_auto_update=$ts" >$HOME/.local/share/ff/state end function capture_ff set -g FF_OUT $TEST_HOME/out.txt set -g FF_ERR $TEST_HOME/err.txt ff $argv >$FF_OUT 2>$FF_ERR set -g FF_STATUS $status end # --- tests --- setup_env add_upstream_marker MARKER_FIRST capture_ff help string match -q '*Usage: ff *' <$FF_OUT t_assert $status "interval elapsed: ff help still prints usage" test -f $HOME/.dotfile/MARKER_FIRST t_assert $status "interval elapsed: first command pulls new commit" test -f $HOME/.local/share/ff/state t_assert $status "interval elapsed: records last_auto_update" string match -q '*Auto-update:*' <$FF_OUT t_assert $status "ff help documents auto-update" string match -q '*已自动更新*' <$FF_ERR t_assert $status "interval elapsed: stderr says updated" cleanup_env setup_env add_upstream_marker MARKER_RECENT write_state (date +%s) capture_ff help test ! -f $HOME/.dotfile/MARKER_RECENT t_assert $status "interval not elapsed: does not pull" cleanup_env setup_env echo "enabled=false" >$HOME/.config/ff/config add_upstream_marker MARKER_DISABLED capture_ff help test ! -f $HOME/.dotfile/MARKER_DISABLED t_assert $status "enabled=false: does not pull" cleanup_env setup_env add_upstream_marker MARKER_DIRTY echo local >$HOME/.dotfile/dirty.txt capture_ff help string match -q '*Usage: ff *' <$FF_OUT t_assert $status "dirty tree: current command still runs" test ! -f $HOME/.dotfile/MARKER_DIRTY t_assert $status "dirty tree: skips pull" string match -q '*工作区不干净*' <$FF_ERR t_assert $status "dirty tree: stderr skip message" cleanup_env setup_env write_state (date +%s) add_upstream_marker MARKER_EXPLICIT capture_ff update test -f $HOME/.dotfile/MARKER_EXPLICIT t_assert $status "explicit ff update still pulls when interval not elapsed" set -l after_ts (string split -m 1 -- '=' (cat $HOME/.local/share/ff/state))[2] test -n "$after_ts" -a "$after_ts" -gt 0 t_assert $status "explicit ff update refreshes timestamp" cleanup_env setup_env add_upstream_marker MARKER_ENV set -gx FF_NO_AUTO_UPDATE 1 capture_ff help test ! -f $HOME/.dotfile/MARKER_ENV t_assert $status "FF_NO_AUTO_UPDATE=1: does not pull" set -e FF_NO_AUTO_UPDATE cleanup_env setup_env add_upstream_marker MARKER_FLAG capture_ff --no-auto-update help test ! -f $HOME/.dotfile/MARKER_FLAG t_assert $status "--no-auto-update: does not pull" cleanup_env setup_env add_upstream_marker MARKER_TRAILING capture_ff help --no-auto-update test -f $HOME/.dotfile/MARKER_TRAILING t_assert $status "trailing --no-auto-update after command is not a global skip" cleanup_env setup_env add_upstream_marker MARKER_FAIL git -C $HOME/.dotfile remote set-url origin /nonexistent/ff-auto-update.git capture_ff help string match -q '*Usage: ff *' <$FF_OUT t_assert $status "pull failure: current command still runs" test ! -f $HOME/.dotfile/MARKER_FAIL t_assert $status "pull failure: does not apply update" string match -q '*自动更新失败*' <$FF_ERR t_assert $status "pull failure: stderr skip message" cleanup_env setup_env echo "interval=24h" >$HOME/.config/ff/config write_state (math (date +%s) - 86401) add_upstream_marker MARKER_INTERVAL capture_ff help test -f $HOME/.dotfile/MARKER_INTERVAL t_assert $status "interval 24h elapsed: pulls" cleanup_env echo "" echo "$_t_ok passed, $_t_fail failed" if test $_t_fail -gt 0 exit 1 end exit 0