Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 0 additions & 9 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,6 @@ begin_test() {
echo -e "[test] ${source%.*}::${1}"
}

# Allocates a temporary file, deleting on exit.
alloctmp() {
local tempath
tempath=$(mktemp)
# shellcheck disable=SC2064
trap "rm '$tempath'" EXIT
echo "$tempath"
}

# Simulates the response of `batch_get_options`. It accepts arguments in the
# same format as `batch_get_options`: each pair contains the variable name and
# its default value. If a variable is set in the execution context, then its
Expand Down
4 changes: 2 additions & 2 deletions src/helpers_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -eo pipefail
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# shellcheck source=../src/helpers.sh
# shellcheck source=./helpers.sh
source "$CURRENT_DIR/helpers.sh"

#=== test:parse_cmds ===#
Expand All @@ -19,7 +19,7 @@ test_parse_commands() {
local i=0
while [[ $# -gt 0 ]]; do
if [[ $1 != "${cmds[$i]}" ]]; then
git diff <(echo "$1") <(echo "${cmds[i]}")
git diff <(echo "${cmds[i]}") <(echo "$1")
failf "unexpected token at $((i + 1))"
fi
shift
Expand Down
203 changes: 93 additions & 110 deletions src/toggle_tests.sh
Original file line number Diff line number Diff line change
@@ -1,129 +1,112 @@
#!/usr/bin/env bash
# shellcheck disable=SC2030
# shellcheck disable=SC2031
# shellcheck disable=SC2034

set -eo pipefail
CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

# shellcheck source=../src/helpers.sh
# shellcheck source=./helpers.sh
source "$CURRENT_DIR/helpers.sh"

#=== test:tggles ===#

prepare_batch_options() {
fake_batch_options \
t_id_format="pane/path/{popup_name}" \
t_on_init="display 'on-init' ; run '#{@on_init}'" \
t_before_open="display 'before-open' ; run '#{@before_open}'" \
t_after_close="display 'after-close' ; run '#{@after_close}'" \
t_toggle_mode="switch" \
t_socket_name="popup_server1" \
t_socket_path="socket/path/popup_server2" \
t_opened_name="" \
t_caller_id_format="caller/id/format" \
t_caller_path="caller/session/pane" \
t_caller_pane_path="caller/pane/path" \
t_default_id_format="session/path/{popup_name}" \
t_default_shell="/usr/bin/fish" \
t_session_path="working/session/path" \
t_pane_path="working/pane/path"
}
declare delimiter=">>>END" exit_codes=(0 0 0 0) test_name
test_toggle() {
local i workdir f_call_id f_input f_output f_expected

declare delimiter=">>>END" exit_codes f_call_id f_output
tmux() {
# Bump call ID
local call_id
call_id=$(cat "$f_call_id")
echo "$((call_id + 1))" >"$f_call_id"

# The first call is always `batch_get_options`.
# Discard its output since not particular useful.
if [[ $call_id == 0 ]]; then
prepare_batch_options
return
fi
# Prepare inputs
workdir=$(mktemp -d)
# shellcheck disable=SC2064
trap "rm -rf '$workdir'" EXIT

# Appends arguments to output
{
echo ">>>TMUX:BEGIN($call_id)"
printf "%s\n" "$@"
echo "<<<TMUX:END($call_id)"
echo
} >>"$f_output"

# Fake tmux exit code
# shellcheck disable=SC2086
return ${exit_codes[$call_id]}
}
f_call_id="$workdir/call_id"
f_input="$workdir/iutput"
f_output="$workdir/output"

declare test_name
test_toggle() {
f_call_id=$(alloctmp)
f_output=$(alloctmp)
echo 0 >"$f_call_id"
source "$CURRENT_DIR/toggle.sh"
i=0
for code in "${exit_codes[@]}"; do
echo "$code" >"${f_input}_${i}"
i=$((i + 1))
done

# Do call popup-toggle
export delimiter f_call_id f_input f_output
command "$CURRENT_DIR/toggle.sh" "$@"

local expected="$CURRENT_DIR/toggle_tests/$test_name.stdout"
# Validate outputs
f_expected="$CURRENT_DIR/toggle_tests/$test_name.stdout"
if [[ $TEST_OVERWRITE = 1 ]]; then
mkdir -p "$(dirname "$expected")"
cp "$f_output" "$expected"
mkdir -p "$(dirname "$f_expected")"
cp "$f_output" "$f_expected"
else
git diff --exit-code "$f_output" "$expected"
git diff --exit-code "$f_expected" "$f_output"
fi
}

test_name="open_popup"
exit_codes=(0 0 0)
t_toggle_mode="switch"
t_opened_name=""
begin_test "$test_name"
test_toggle --name="p_open"

test_name="close_popup"
exit_codes=(0 0 0)
t_toggle_mode="switch"
t_opened_name="p_close"
begin_test "$test_name"
test_toggle --name="p_close"

test_name="switch_popup"
exit_codes=(0 0 0)
t_toggle_mode="switch"
t_opened_name="p_switch_1"
begin_test "$test_name"
test_toggle --name="p_switch_2"

test_name="switch_new_popup"
exit_codes=(0 1 0)
t_toggle_mode="switch"
t_opened_name="p_switch_1"
begin_test "$test_name"
test_toggle --name="p_switch_2"

test_name="force_close_popup"
exit_codes=(0 0 0)
t_toggle_mode="force-close"
t_opened_name="p_force_close_1"
begin_test "$test_name"
test_toggle --name="p_force_close_2"

test_name="open_nested_popup"
exit_codes=(0 0 0)
t_toggle_mode="force-open"
t_opened_name="p_open_nested_1"
begin_test "$test_name"
test_toggle --name="p_open_nested_2"

test_name="open_with_toggle_key"
exit_codes=(0 0 0)
t_toggle_mode="switch"
t_opened_name=""
begin_test "$test_name"
test_toggle --name="p_toggle_key" --toggle-key="-T root M-p" --toggle-key="-n M-o"
# Ensure out fake executable tmux is picked at first.
export PATH="$CURRENT_DIR/toggle_tests:$PATH"

# Force subshell to ensure modifications are temporary.
(
test_name="open_popup"
begin_test "$test_name"
test_toggle --name="p_open"
) || exit 1

(
export t_opened_name="p_close"

test_name="close_popup"
begin_test "$test_name"
test_toggle --name="p_close"
) || exit 1

(
export t_opened_name="p_switch_1"

test_name="switch_popup"
begin_test "$test_name"
test_toggle --name="p_switch_2"
) || exit 1

(
export t_opened_name="p_switch_1"
exit_codes=(0 1 0 0)

test_name="switch_new_popup"
begin_test "$test_name"
test_toggle --name="p_switch_2"
) || exit 1

(
export t_toggle_mode="force-close"
export t_opened_name="p_force_close_1"

test_name="force_close_popup"
begin_test "$test_name"
test_toggle --name="p_force_close_2"
) || exit 1

(
export t_toggle_mode="force-open"
export t_opened_name="p_open_nested_1"

test_name="open_nested_popup"
begin_test "$test_name"
test_toggle --name="p_open_nested_2"
) || exit 1

(
test_name="open_with_toggle_key"
begin_test "$test_name"
test_toggle --name="p_toggle_key" --toggle-key="-T root M-p" --toggle-key="-n M-o"
) || exit 1

# Open nested popups should not clean toggle keys.
test_name="open_nested_with_toggle_key"
exit_codes=(0 0 0)
t_toggle_mode="force-open"
t_opened_name="p_nested_toggle_key_1"
begin_test "$test_name"
test_toggle --name="p_nested_toggle_key_2" --toggle-key="-n M-o"
(
export t_toggle_mode="force-open"
export t_opened_name="p_nested_toggle_key_1"

test_name="open_nested_with_toggle_key"
begin_test "$test_name"
test_toggle --name="p_nested_toggle_key_2" --toggle-key="-n M-o"
) || exit 1
7 changes: 3 additions & 4 deletions src/toggle_tests/close_popup.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
>>>TMUX:BEGIN(1)
detach
<<<TMUX:END(1)

>>>TMUX:BEGIN[1]
detach
<<<TMUX:END[1]
7 changes: 3 additions & 4 deletions src/toggle_tests/force_close_popup.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
>>>TMUX:BEGIN(1)
detach
<<<TMUX:END(1)

>>>TMUX:BEGIN[1]
detach
<<<TMUX:END[1]
67 changes: 45 additions & 22 deletions src/toggle_tests/open_nested_popup.stdout
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
>>>TMUX:BEGIN(1)
display
before-open
;
run
#{@before_open}
;
set
default-shell
/bin/sh
;
display-popup
tmux set default-shell '/usr/bin/fish' ;export TMUX_POPUP_SERVER='popup_server2' ;export SHELL='/usr/bin/fish' ;exec tmux -S socket/path/popup_server2 new -As pane/path/p_open_nested_2 \; set @__popup_name p_open_nested_2 \; set @__popup_id_format pane/path/\{popup_name\} \; set @__popup_caller_path working/session/path \; set @__popup_caller_pane_path working/pane/path \; display on-init \; run \#\{@on_init\} >/dev/null
;
display
after-close
;
run
#{@after_close}
;
<<<TMUX:END(1)

>>>TMUX:BEGIN[1]
run
#{@popup_before_open}
;
set
default-shell
/bin/sh
;
display-popup
>>>TMUX:BEGIN[2]
set
default-shell
/default/shell
<<<TMUX:END[2]
>>>TMUX:BEGIN[3]
-L
popup
new
-As
default_id_format/p_open_nested_2
;
set
@__popup_name
p_open_nested_2
;
set
@__popup_id_format
default_id_format/{popup_name}
;
set
@__popup_caller_path
working_session_path
;
set
@__popup_caller_pane_path
working_pane_path
;
run
#{@popup_on_init}
<<<TMUX:END[3]
;
run
#{@popup_after_close}
;
<<<TMUX:END[1]
Loading