From 77c434bc42950013963598d090b91d4efb4994b9 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 29 Aug 2021 09:44:22 +0200 Subject: [PATCH] Extract setup and teardown for tmux tests This was long overdue since the setup logic is much more complex than the actual tests. tmux-prompt.fish had extra logic to protect against XDG_CONFIG_HOME with leading double double-dot. I believe this is no longer necessary with the new test driver. We still use our own temp dir because we want to be able to run this independently of the test driver, This can be useful for debugging tests. For example we can insert a "$tmux attach" command in a test, and then run build/fish -C 'source tests/test_functions/isolated-tmux.fish' tests/checks/tmux-bind.fish This allows to inspect the state of the test and debug interactively. Attaching to the terminal doesn't work when running inside littlecheck because littlecheck consumes our output and doesn't give us a terminal. (Maybe there's an easy way to fix that?) --- tests/checks/tmux-bind.fish | 36 ++------------------ tests/checks/tmux-complete.fish | 45 +++++-------------------- tests/checks/tmux-prompt.fish | 41 +++------------------- tests/test_functions/isolated-tmux.fish | 45 +++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 105 deletions(-) create mode 100644 tests/test_functions/isolated-tmux.fish diff --git a/tests/checks/tmux-bind.fish b/tests/checks/tmux-bind.fish index 5e84df6d7..a25e8d635 100644 --- a/tests/checks/tmux-bind.fish +++ b/tests/checks/tmux-bind.fish @@ -1,39 +1,9 @@ -#RUN: %fish -C 'set -g fish %fish' %s +#RUN: %fish %s #REQUIRES: command -v tmux -set fish (builtin realpath $fish) - -# Isolated tmux. -set -g tmpdir (mktemp -d) - -# Don't CD elsewhere, because tmux socket file is relative to CWD. Using -# absolute path to socket file is prone to 'socket file name too long' error. -cd $tmpdir - -set -g tmux tmux -S .tmux-socket -f /dev/null - -set -g sleep sleep .1 -set -q CI && set sleep sleep 1 - - -$tmux new-session -x 80 -y 10 -d $fish -C ' - # This is similar to "tests/interactive.config". - function fish_greeting; end - function fish_prompt; printf "prompt $status_generation> "; end - # No autosuggestion from older history. - set fish_history "" -' -# Set the correct permissions for the newly created socket to allow future connections. -# This is required at least under WSL or else each invocation will return a permissions error. -chmod 777 .tmux-socket -$sleep # Let fish draw a prompt. - # Test moving around with up-or-search on a multi-line commandline. -$tmux send-keys 'echo 12' M-Enter 'echo ab' C-p 345 C-n cde +isolated-tmux send-keys 'echo 12' M-Enter 'echo ab' C-p 345 C-n cde $sleep -$tmux capture-pane -p +isolated-tmux capture-pane -p # CHECK: prompt 0> echo 12345 # CHECK: echo abcde - -$tmux kill-server -rm -r $tmpdir diff --git a/tests/checks/tmux-complete.fish b/tests/checks/tmux-complete.fish index b26e94090..91dfd0a37 100644 --- a/tests/checks/tmux-complete.fish +++ b/tests/checks/tmux-complete.fish @@ -1,57 +1,30 @@ -#RUN: %fish -C 'set -g fish %fish' %s +#RUN: %fish %s #REQUIRES: command -v tmux -# Resolve absolute path to fish (if needed) before changing directories -set fish (builtin realpath $fish) - -# Isolated tmux. tmux can't handle session sockets in paths that are too long, and macOS has a very -# long $TMPDIR, so use a relative path - except macOS doesn't have `realpath --relative-to`... -# We are cd'd into a unique temp dir created/assigned by the test driver, so this will work so long -# as `tmux` is only invoked from the same PWD. -set tmpdir (command mktemp -d ./tmp.XXXXXXXX) -cd $tmpdir -set -g tmux tmux -S ./.tmux-socket -f /dev/null - -set -g sleep sleep .1 -set -q CI && set sleep sleep 1 - - -$tmux new-session -x 80 -y 10 -d $fish -C ' - # This is similar to "tests/interactive.config". - function fish_greeting; end - function fish_prompt; printf "prompt $status_generation> "; end - # No autosuggestion from older history. - set fish_history "" -' -# Set the correct permissions for the newly created socket to allow future connections. -# This is required at least under WSL or else each invocation will return a permissions error. -chmod 777 .tmux-socket -$sleep # Let fish draw a prompt. +isolated-tmux # Don't escape existing token (#7526). echo >file-1 echo >file-2 -$tmux send-keys 'HOME=$PWD ls ~/' Tab +isolated-tmux send-keys 'HOME=$PWD ls ~/' Tab $sleep -$tmux capture-pane -p +isolated-tmux capture-pane -p # Note the contents may or may not have the autosuggestion appended - it is a race. # CHECK: prompt 0> HOME=$PWD ls ~/file-{{1?}} # CHECK: ~/file-1 ~/file-2 # No pager on single smartcase completion (#7738). -$tmux send-keys C-u C-l 'mkdir cmake CMakeFiles' Enter C-l \ +isolated-tmux send-keys C-u C-l 'mkdir cmake CMakeFiles' Enter C-l \ 'cat cmake' Tab $sleep -$tmux capture-pane -p +isolated-tmux capture-pane -p # CHECK: prompt 1> cat cmake/ # Correct case in pager when prefixes differ in case (#7743). -$tmux send-keys C-u C-l 'complete -c foo2 -a "aabc aaBd" -f' Enter C-l \ +isolated-tmux send-keys C-u C-l 'complete -c foo2 -a "aabc aaBd" -f' Enter C-l \ 'foo2 A' Tab $sleep -$tmux capture-pane -p -# The "bc" part is the autosuggestion - we could use "tmux capture-pane -e" to check colors. +isolated-tmux capture-pane -p +# The "bc" part is the autosuggestion - we could use "capture-pane -e" to check colors. # CHECK: prompt 2> foo2 aabc # CHECK: aabc aaBd - -$tmux kill-server diff --git a/tests/checks/tmux-prompt.fish b/tests/checks/tmux-prompt.fish index a32342df9..07f5e01af 100644 --- a/tests/checks/tmux-prompt.fish +++ b/tests/checks/tmux-prompt.fish @@ -1,49 +1,18 @@ -#RUN: %fish -C 'set -g fish %fish' %s +#RUN: %fish %s #REQUIRES: command -v tmux -set fish (builtin realpath $fish) - -# Isolated tmux. -# Note $XDG_CONFIG_HOME typically has a leading double-dot, -# so our uvars file will leak across runs; therefore -# descend more deeply into the tmpdir. -set -g tmpdir (mktemp -d)/inner1/inner2/ -mkdir -p $tmpdir - -# Don't CD elsewhere, because tmux socket file is relative to CWD. Using -# absolute path to socket file is prone to 'socket file name too long' error. -cd $tmpdir - -set -g tmux tmux -S .tmux-socket -f /dev/null - -set -g sleep sleep .1 -set -q CI && set sleep sleep 10 - -while set -e prompt_var -end - -$tmux new-session -x 80 -y 10 -d $fish -C ' - # This is similar to "tests/interactive.config". - function fish_greeting; end +set -g isolated_tmux_fish_extra_args -C ' function fish_prompt; printf "prompt $status_generation> <$prompt_var> "; end - # No autosuggestion from older history. - set fish_history "" function on_prompt_var --on-variable prompt_var commandline -f repaint end ' -# Set the correct permissions for the newly created socket to allow future connections. -# This is required at least under WSL or else each invocation will return a permissions error. -chmod 777 .tmux-socket -$sleep # Let fish draw a prompt. -$tmux capture-pane -p +isolated-tmux capture-pane -p # CHECK: prompt 0> <> +set -q CI && set sleep sleep 10 set -U prompt_var changed $sleep -$tmux capture-pane -p +isolated-tmux capture-pane -p # CHECK: prompt 0> - -$tmux kill-server -rm -r $tmpdir diff --git a/tests/test_functions/isolated-tmux.fish b/tests/test_functions/isolated-tmux.fish new file mode 100644 index 000000000..3412b839e --- /dev/null +++ b/tests/test_functions/isolated-tmux.fish @@ -0,0 +1,45 @@ +function isolated-tmux --inherit-variable tmpdir + set -l tmpdir (mktemp -d) + cd $tmpdir + + set -g sleep sleep .1 + set -q CI && set sleep sleep 1 + + # Evil hack - override ourselves, so we only run initialization once. + # We could do this outside the function, but then initialization is done too early for a + # command that wants to set $isolated_tmux_fish_extra_args first, like + # fish -C 'source tests/test_functions/isolated-tmux.fish' tests/checks/tmux-prompt.fish + function isolated-tmux --inherit-variable tmpdir + # tmux can't handle session sockets in paths that are too long, and macOS has a very long + # $TMPDIR, so use a relative path - except macOS doesn't have `realpath --relative-to`... + # Luckily, we don't need to call tmux from other directories, so just make sure no one + # does by accident. + if test $PWD != $tmpdir + echo "error: isolated-tmux must always be run from the same directory." >&2 + return 1 + end + tmux -S .tmux-socket -f /dev/null $argv + end + + function isolated-tmux-cleanup --on-event fish_exit --inherit-variable tmpdir + isolated-tmux kill-server + rm -r $tmpdir + end + + set -l fish (status fish-path) + isolated-tmux new-session -x 80 -y 10 -d $fish -C ' + # This is similar to "tests/interactive.config". + function fish_greeting; end + function fish_prompt; printf "prompt $status_generation> "; end + # No autosuggestion from older history. + set fish_history "" + ' $isolated_tmux_fish_extra_args + # Set the correct permissions for the newly created socket to allow future connections. + # This is required at least under WSL or else each invocation will return a permissions error. + chmod 777 .tmux-socket + $sleep # Let fish draw a prompt. + + if set -q argv[1] + isolated-tmux $argv + end +end