| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require 'shellwords' |
| 4 |
require 'tmpdir' |
| 5 |
require_relative 'lib/common' |
| 6 |
|
| 7 |
# Tests for running fzf in a tmux floating pane (--popup on tmux 3.7 or above) |
| 8 |
class TestTmux < TestInteractive |
| 9 |
def setup |
| 10 |
super |
| 11 |
# Cannot rely on the exit status; tmux versions before 3.7 exit |
| 12 |
# normally with empty output for an unknown command name |
| 13 |
supported = IO.popen(%w[tmux list-commands new-pane], err: File::NULL, &:read).include?('new-pane') |
| 14 |
skip('floating panes not supported') unless supported |
| 15 |
end |
| 16 |
|
| 17 |
def test_floating_pane |
| 18 |
tmux.send_keys "seq 100 | #{fzf('--popup center,80% --margin 0')}", :Enter |
| 19 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 20 |
# Border text is empty when no label is given |
| 21 |
pane = floating_pane |
| 22 |
refute_nil pane |
| 23 |
assert_equal "\#{@fzf-border-label}", pane_option(pane, 'pane-border-format') |
| 24 |
assert_equal '', pane_option(pane, '@fzf-border-label') |
| 25 |
tmux.send_keys '99' |
| 26 |
tmux.until { |lines| assert_equal 1, lines.match_count } |
| 27 |
tmux.send_keys :Enter |
| 28 |
assert_equal '99', fzf_output |
| 29 |
end |
| 30 |
|
| 31 |
def test_floating_pane_killed |
| 32 |
tmux.send_keys "seq 100 | #{FZF} --popup bottom,50% --margin 0; echo code:$?", :Enter |
| 33 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 34 |
pane = floating_pane |
| 35 |
refute_nil pane |
| 36 |
assert system('tmux', 'kill-pane', '-t', pane) |
| 37 |
tmux.until { |lines| assert lines.any_include?('code:130') } |
| 38 |
end |
| 39 |
|
| 40 |
def test_floating_pane_border_label |
| 41 |
tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --border-label ' #fzf-label 100% '))}", :Enter |
| 42 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 43 |
pane = floating_pane |
| 44 |
refute_nil pane |
| 45 |
# The label is held in a user option, not in the pane title, which is |
| 46 |
# left to the user. '#' is doubled so that the label is drawn as it is |
| 47 |
# written, instead of '#[...]' being taken as a style directive |
| 48 |
assert_equal ' ##fzf-label 100% ', pane_option(pane, '@fzf-border-label') |
| 49 |
assert_equal "\#{@fzf-border-label}", pane_option(pane, 'pane-border-format') |
| 50 |
title = IO.popen(['tmux', 'display-message', '-p', '-t', pane, "\#{pane_title}"], &:read) |
| 51 |
refute_equal ' #fzf-label 100% ', title.chomp |
| 52 |
tmux.send_keys :Enter |
| 53 |
assert_equal '1', fzf_output |
| 54 |
end |
| 55 |
|
| 56 |
def test_floating_pane_change_border_label |
| 57 |
tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --bind 'space:change-border-label( #[fg=red] 100% )'))}", :Enter |
| 58 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 59 |
pane = floating_pane |
| 60 |
refute_nil pane |
| 61 |
assert_equal '', pane_option(pane, '@fzf-border-label') |
| 62 |
tmux.send_keys :Space |
| 63 |
wait { assert_equal ' ##[fg=red] 100% ', pane_option(pane, '@fzf-border-label') } |
| 64 |
tmux.send_keys :Enter |
| 65 |
assert_equal '1', fzf_output |
| 66 |
end |
| 67 |
|
| 68 |
# A program running in the pane owns the pane title, but not the label |
| 69 |
def test_floating_pane_border_label_not_affected_by_title |
| 70 |
tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --border-label ' label ' --bind 'space:execute-silent(printf "\\033]2;hijacked\\033\\\\" > /dev/tty)'))}", :Enter |
| 71 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 72 |
pane = floating_pane |
| 73 |
refute_nil pane |
| 74 |
tmux.send_keys :Space |
| 75 |
wait do |
| 76 |
title = IO.popen(['tmux', 'display-message', '-p', '-t', pane, "\#{pane_title}"], &:read) |
| 77 |
assert_equal 'hijacked', title.chomp |
| 78 |
end |
| 79 |
assert_equal ' label ', pane_option(pane, '@fzf-border-label') |
| 80 |
tmux.send_keys :Enter |
| 81 |
assert_equal '1', fzf_output |
| 82 |
end |
| 83 |
|
| 84 |
def test_floating_pane_become |
| 85 |
tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --bind 'enter:become(echo became-{})'))}", :Enter |
| 86 |
tmux.until { |lines| assert_equal 100, lines.item_count } |
| 87 |
tmux.send_keys :Enter |
| 88 |
assert_equal 'became-1', fzf_output |
| 89 |
end |
| 90 |
|
| 91 |
def test_explicit_border_falls_back_to_popup |
| 92 |
# display-popup requires an attached client, which the test environment |
| 93 |
# may not have; intercept it with a tmux shim on PATH |
| 94 |
dir = Dir.mktmpdir |
| 95 |
real = `command -v tmux`.chomp |
| 96 |
shim = File.join(dir, 'tmux') |
| 97 |
File.write(shim, <<~SH) |
| 98 |
#!/bin/sh |
| 99 |
if [ "$1" = display-popup ]; then |
| 100 |
echo popup-used >&2 |
| 101 |
exit 0 |
| 102 |
fi |
| 103 |
exec #{real.shellescape} "$@" |
| 104 |
SH |
| 105 |
FileUtils.chmod(0o755, shim) |
| 106 |
tmux.send_keys "seq 100 | PATH=#{dir.shellescape}:$PATH #{FZF} --popup center --border rounded", :Enter |
| 107 |
tmux.until { |lines| assert lines.any_include?('popup-used') } |
| 108 |
refute floating_pane |
| 109 |
ensure |
| 110 |
FileUtils.remove_entry(dir) if dir |
| 111 |
end |
| 112 |
|
| 113 |
private |
| 114 |
|
| 115 |
# stderr is merged in so that an unset option, which prints 'invalid |
| 116 |
# option' to stderr and nothing to stdout, is not read as an empty value |
| 117 |
def pane_option(pane, name) |
| 118 |
IO.popen(['tmux', 'show-options', '-p', '-t', pane, '-v', name], err: %i[child out], &:read).chomp |
| 119 |
end |
| 120 |
|
| 121 |
def floating_pane |
| 122 |
format = "\#{pane_id} \#{pane_floating_flag}" |
| 123 |
lines = IO.popen(['tmux', 'list-panes', '-t', tmux.win, '-F', format]) { |io| io.readlines(chomp: true) } |
| 124 |
lines.filter_map { |line| line.split.first if line.end_with?(' 1') }.first |
| 125 |
end |
| 126 |
end |