From f42617ef03fd43d56bae945a931c6ac93947224a Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Wed, 19 Apr 2017 19:46:00 +0200 Subject: [PATCH] rewrite specs in bash --- test/conf/fish.conf | 1 - test/conf/tmuxomatic.conf | 4 + test/helpers.sh | 59 ++++++++++++ test/provisioning/bsd.sh | 1 + test/run.sh | 2 +- test/specs/basic-yank_spec.exp | 23 ----- test/specs/basic-yank_spec.sh | 20 ++++ test/specs/fish-support_spec.exp | 23 ----- test/specs/fish-support_spec.sh | 18 ++++ test/specs/matches-custom-patterns_spec.exp | 32 ------- test/specs/matches-custom-patterns_spec.sh | 26 ++++++ .../more-than-one-match-per-line_spec.exp | 23 ----- .../more-than-one-match-per-line_spec.sh | 20 ++++ test/specs/preserve-zoom-state_spec.exp | 32 ------- test/specs/preserve-zoom-state_spec.sh | 23 +++++ test/specs/tmux-yank-integration_spec.exp | 27 ------ test/specs/tmux-yank-integration_spec.sh | 20 ++++ .../specs/works-with-custom-bindings_spec.exp | Bin 386 -> 0 bytes test/specs/works-with-custom-bindings_spec.sh | 21 +++++ test/tmuxomatic.rFvj4QV | 0 test/tmuxomatic.sh | 87 ++++++++++++++++++ tmuxomatic-test.sh | 4 + 22 files changed, 304 insertions(+), 162 deletions(-) create mode 100644 test/conf/tmuxomatic.conf create mode 100644 test/helpers.sh delete mode 100755 test/specs/basic-yank_spec.exp create mode 100755 test/specs/basic-yank_spec.sh delete mode 100755 test/specs/fish-support_spec.exp create mode 100755 test/specs/fish-support_spec.sh delete mode 100755 test/specs/matches-custom-patterns_spec.exp create mode 100755 test/specs/matches-custom-patterns_spec.sh delete mode 100755 test/specs/more-than-one-match-per-line_spec.exp create mode 100755 test/specs/more-than-one-match-per-line_spec.sh delete mode 100755 test/specs/preserve-zoom-state_spec.exp create mode 100755 test/specs/preserve-zoom-state_spec.sh delete mode 100755 test/specs/tmux-yank-integration_spec.exp create mode 100755 test/specs/tmux-yank-integration_spec.sh delete mode 100755 test/specs/works-with-custom-bindings_spec.exp create mode 100755 test/specs/works-with-custom-bindings_spec.sh create mode 100644 test/tmuxomatic.rFvj4QV create mode 100644 test/tmuxomatic.sh create mode 100644 tmuxomatic-test.sh diff --git a/test/conf/fish.conf b/test/conf/fish.conf index 2121c4e..5a92f92 100644 --- a/test/conf/fish.conf +++ b/test/conf/fish.conf @@ -1,4 +1,3 @@ set -g prefix C-a set -g @fingers-compact-hints '0' -set-option -g default-shell "/usr/bin/fish" run /home/vagrant/shared/tmux-fingers.tmux diff --git a/test/conf/tmuxomatic.conf b/test/conf/tmuxomatic.conf new file mode 100644 index 0000000..ea26666 --- /dev/null +++ b/test/conf/tmuxomatic.conf @@ -0,0 +1,4 @@ +set -g prefix F12 +set -g status off +set-window-option -g force-width 80 +set-window-option -g force-height 24 diff --git a/test/helpers.sh b/test/helpers.sh new file mode 100644 index 0000000..5650ca1 --- /dev/null +++ b/test/helpers.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash + +# this assumes tmuxomatic has been already sourced +TMUX_PREFIX=C-a + +function test_clean_up() { + tmuxomatic__exec "tmux kill-session -t test" +} + +function tmux_send() { + local key=$1 + sleep 0.5 + tmuxomatic send-keys "$TMUX_PREFIX" + sleep 0.5 + tmuxomatic send-keys "$key" + sleep 0.5 +} + +function tmux_paste() { + tmux_send "]" +} + +function init_pane() { + tmux_send "c" + tmuxomatic__exec "export PS1='# '; clear" +} + +function init_pane_fish() { + tmux_send "c" + tmuxomatic__exec "function fish_prompt; echo '# '; end" + tmuxomatic__exec "clear" +} + +function invoke_fingers() { + tmux_send "F" + sleep 1.0 +} + +function echo_yanked() { + sleep 0.5 + tmuxomatic__exec "clear" + tmuxomatic send-keys "echo yanked text is " + tmux_paste + tmuxomatic send-keys Enter +} + +function begin_with_conf() { + tmuxomatic__exec "tmux -f ./test/conf/$1.conf new -s test" +} + +function begin_hook() { + tmuxomatic set-window-option force-width 80 + tmuxomatic set-window-option force-height 24 + tmuxomatic__exec "tmux kill-session -t test" +} + +function end_hook() { + tmuxomatic__exec "tmux kill-session -t test" +} diff --git a/test/provisioning/bsd.sh b/test/provisioning/bsd.sh index e4a7a48..6ae2d9d 100644 --- a/test/provisioning/bsd.sh +++ b/test/provisioning/bsd.sh @@ -3,5 +3,6 @@ pkg install -y bash tmux expect fish gawk chsh -s bash vagrant +#TODO fuck /usr/bin/fish in ubuntu, /usr/local/bin/fish in BSD echo "fishman" | pw user add -n fishman -h 0 -s "/usr/local/bin/fish" echo "run /home/vagrant/shared/tmux-fingers.tmux" > .tmux.conf diff --git a/test/run.sh b/test/run.sh index fe21d52..908623f 100755 --- a/test/run.sh +++ b/test/run.sh @@ -12,7 +12,7 @@ if [[ "$target" == "within-vm" ]]; then stty cols 80 stty rows 24 fail_count=0 - for test_file in $(ls $CURRENT_DIR/specs/*_spec.exp); do + for test_file in $(ls $CURRENT_DIR/specs/*_spec.sh); do result="* $test_file ..." sleep 1 echo "Running $test_file" >> $SPEC_OUTPUT_LOG diff --git a/test/specs/basic-yank_spec.exp b/test/specs/basic-yank_spec.exp deleted file mode 100755 index 8ea5c7f..0000000 --- a/test/specs/basic-yank_spec.exp +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -spawn tmux -f ./test/conf/basic.conf new -s test; -sleep 0.5; - -init_pane -exec "cat ./test/fixtures/grep-output"; -invoke_fingers; -send "i"; -echo_yanked; - -expect { - "yanked text is scripts/hints.sh" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/basic-yank_spec.sh b/test/specs/basic-yank_spec.sh new file mode 100755 index 0000000..6d0d1ab --- /dev/null +++ b/test/specs/basic-yank_spec.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +#set -x + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +begin_with_conf "basic" +init_pane + +tmuxomatic__exec "cat ./test/fixtures/grep-output" +invoke_fingers +tmuxomatic send-keys "i" +echo_yanked + +tmuxomatic__expect "yanked text is scripts/hints.sh" +tmuxomatic__end end_hook diff --git a/test/specs/fish-support_spec.exp b/test/specs/fish-support_spec.exp deleted file mode 100755 index 1ee51c7..0000000 --- a/test/specs/fish-support_spec.exp +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -spawn sudo -u fishman SHELL=/usr/bin/fish tmux -f /home/vagrant/shared/test/conf/fish.conf new -s test; - -init_pane_fish -exec "cat ./test/fixtures/grep-output"; -invoke_fingers; -sleep 30 -send "i"; -echo_yanked; - -expect { - "yanked text is scripts/hints.sh" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/fish-support_spec.sh b/test/specs/fish-support_spec.sh new file mode 100755 index 0000000..fca3244 --- /dev/null +++ b/test/specs/fish-support_spec.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +tmuxomatic__exec "sudo su fishman" +tmuxomatic__exec "tmux -f /home/vagrant/shared/test/conf/basic.conf new -s test" +init_pane_fish +tmuxomatic__exec "cat ./test/fixtures/grep-output" +invoke_fingers +tmuxomatic send-keys "i" +echo_yanked + +tmuxomatic__expect "yanked text is scripts/hints.sh" +tmuxomatic__end end_hook diff --git a/test/specs/matches-custom-patterns_spec.exp b/test/specs/matches-custom-patterns_spec.exp deleted file mode 100755 index 3c03d7e..0000000 --- a/test/specs/matches-custom-patterns_spec.exp +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -spawn tmux -f ./test/conf/custom-patterns.conf new -s test; -sleep 0.5; - -init_pane -exec "cat ./test/fixtures/custom-patterns"; - -send "echo yanked text is " -invoke_fingers; -send "i"; -tmux_paste; - -invoke_fingers; -send "o"; -tmux_paste; - -send "\r"; -sleep 0.5; - -expect { - "yanked text is W00TW00TW00TYOLOYOLOYOLO" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/matches-custom-patterns_spec.sh b/test/specs/matches-custom-patterns_spec.sh new file mode 100755 index 0000000..01b4ada --- /dev/null +++ b/test/specs/matches-custom-patterns_spec.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +begin_with_conf "custom-patterns" +init_pane +tmuxomatic__exec "cat ./test/fixtures/custom-patterns" + +tmuxomatic send-keys "echo yanked text is " + +invoke_fingers +tmuxomatic send-keys "i" +tmux_paste + +invoke_fingers +tmuxomatic send-keys "o" +tmux_paste + +tmuxomatic send-keys Enter + +tmuxomatic__expect "yanked text is W00TW00TW00TYOLOYOLOYOLO" +tmuxomatic__end end_hook diff --git a/test/specs/more-than-one-match-per-line_spec.exp b/test/specs/more-than-one-match-per-line_spec.exp deleted file mode 100755 index 29ff30b..0000000 --- a/test/specs/more-than-one-match-per-line_spec.exp +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -spawn tmux -f ./test/conf/basic.conf new -s test; -sleep 0.5; - -init_pane -exec "cat ./test/fixtures/ip-output"; -invoke_fingers; -send "t"; -echo_yanked; - -expect { - "yanked text is 10.0.3.1" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/more-than-one-match-per-line_spec.sh b/test/specs/more-than-one-match-per-line_spec.sh new file mode 100755 index 0000000..92cdd13 --- /dev/null +++ b/test/specs/more-than-one-match-per-line_spec.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +begin_with_conf "basic" +init_pane + +tmuxomatic__exec "cat ./test/fixtures/ip-output" +sleep 1.0 +invoke_fingers + +tmuxomatic send-keys "t" +echo_yanked + +tmuxomatic__expect "yanked text is 10.0.3.1" +tmuxomatic__end end_hook diff --git a/test/specs/preserve-zoom-state_spec.exp b/test/specs/preserve-zoom-state_spec.exp deleted file mode 100755 index 7274086..0000000 --- a/test/specs/preserve-zoom-state_spec.exp +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -exec "rm -rf /tmp/tmux-yank-result" - -spawn tmux -f ./test/conf/basic.conf new -s test; -sleep 0.5; - -init_pane -tmux_send "%" -tmux_send "%" -tmux_send "%" -tmux_send "z" - -exec "cat ./test/fixtures/grep-output"; -invoke_fingers; - -send "" - -exec "echo \"current pane is \$(tmux list-panes -F '#{?window_zoomed_flag,zoomed,not_zoomed}' | head -1)\"" - -expect { - "current pane is zoomed" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/preserve-zoom-state_spec.sh b/test/specs/preserve-zoom-state_spec.sh new file mode 100755 index 0000000..2afaec0 --- /dev/null +++ b/test/specs/preserve-zoom-state_spec.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +begin_with_conf "basic" +init_pane + +tmux_send "%" +tmux_send "%" +tmux_send "%" +tmux_send "z" +tmuxomatic__exec "cat ./test/fixtures/grep-output" + +invoke_fingers +tmuxomatic send-keys C-c + +tmuxomatic__exec "echo \"current pane is \$(tmux list-panes -F '#{?window_zoomed_flag,zoomed,not_zoomed}' | head -1)\"" +tmuxomatic__expect "current pane is zoomed" +tmuxomatic__end end_hook diff --git a/test/specs/tmux-yank-integration_spec.exp b/test/specs/tmux-yank-integration_spec.exp deleted file mode 100755 index 9a47c49..0000000 --- a/test/specs/tmux-yank-integration_spec.exp +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env expect - -set timeout 1; -set tmux_prefix ""; - -source "./test/helpers.exp" - -exec "tmux kill-session -t test"; -exec "rm -rf /tmp/tmux-yank-result" - -spawn tmux -f ./test/conf/tmux-yank.conf new -s test; -sleep 0.5; - -init_pane -exec "cat ./test/fixtures/grep-output"; -invoke_fingers; -send "i"; - -sleep 0.5; -exec "cat /tmp/tmux-yank-result"; - -expect { - "tmux-yank yolo" { exit_ok } - timeout { exit_fail } -} - -exit $exit_code; diff --git a/test/specs/tmux-yank-integration_spec.sh b/test/specs/tmux-yank-integration_spec.sh new file mode 100755 index 0000000..2113f3f --- /dev/null +++ b/test/specs/tmux-yank-integration_spec.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $CURRENT_DIR/../tmuxomatic.sh +source $CURRENT_DIR/../helpers.sh + +tmuxomatic__begin begin_hook + +begin_with_conf "tmux-yank" +init_pane + +tmuxomatic__exec "cat ./test/fixtures/grep-output" +invoke_fingers +tmuxomatic send-keys "i" + +sleep 10 +tmuxomatic__exec "cat /tmp/tmux-yank-result" + +tmuxomatic__expect "tmux-yank yolo" +tmuxomatic__end end_hook diff --git a/test/specs/works-with-custom-bindings_spec.exp b/test/specs/works-with-custom-bindings_spec.exp deleted file mode 100755 index b5cc57bd720a0363c09fb6ad13add2c88c094e64..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 386 zcmXw#&2ob<41{~;DO9&taC_(}55rI+f(DR1mYmSZ>S=*FxpcUMkyO=4K91vXN6-E7sbHXf*F /dev/null + + if [[ $? -eq 1 ]]; then + tmuxomatic -f "$TMUXOMATIC_CURRENT_DIR/conf/tmuxomatic.conf" new-session -d -s tmuxomatic + wait + tmuxomatic__exec "export TMUX=''" + tmuxomatic__exec "clear" + fi + + call_hook "$1" +} + +function tmuxomatic__end() { + call_hook "$1" + tmuxomatic kill-server + exit $TMUXOMATIC_EXIT_CODE +} + +function call_hook() { + local fn_hook="$1" + + if [[ $(__fn_exists "$fn_hook") = "1" ]]; then + $fn_hook + fi +} + +function tmuxomatic__expect() { + local pattern=$1 + local n_matches + local expect_output + + TMUXOMATIC_FIRST_TS=$(__now) + + while [[ $(($(__now) - TMUXOMATIC_FIRST_TS)) -lt $TMUXOMATIC_TIMEOUT ]]; do + echo "Trying to match '$pattern' ..." + n_matches=$(tmuxomatic capture-pane -p | grep -E "$pattern" | wc -l) + + if [[ $n_matches -gt 0 ]]; then + # TODO echo when specified loglevel + echo "Matched '$pattern'! :)" + TMUXOMATIC_EXIT_CODE=0 + break + fi + sleep 0.5 + done + + if [[ $n_matches -le 0 ]]; then + # TODO echo when specified loglevel + # TODO dump pane and buffers + log_output_path=$(mktemp "$PWD/tmuxomatic.XXXXXXX") + mv "$log_output_path" "$log_output_path.log" + log_output_path="${log_output_path}.log" + + + tmuxomatic capture-pane -p > "$log_output_path" + echo "Timeout :( See log at $log_output_path" + TMUXOMATIC_EXIT_CODE=1 + fi +} + +# TODO not working in BSD, therefore end hook not being called and :skull: +function __fn_exists() { + local fn_type=$(type "$1" 2> /dev/null) + + echo "$fn_type" | head -n 1 | grep -c "^$1 is a function$" +} + +function __now() { + echo $(date +%s) +} diff --git a/tmuxomatic-test.sh b/tmuxomatic-test.sh new file mode 100644 index 0000000..e40a6e0 --- /dev/null +++ b/tmuxomatic-test.sh @@ -0,0 +1,4 @@ +source test/tmuxomatic.sh +source test/helpers.sh + +tmuxomatic__begin begin_hook