Skip to content
Myr

test_tmux.rb

This file

Path

test/test_tmux.rb

126 lines · 5.0 KiB · Ruby

Last changed by

Separate floating pane border label from pane titleJunegunn Choibefore adoption50e6c02

History of this file

Take it

1# frozen_string_literal: true23require 'shellwords'4require 'tmpdir'5require_relative 'lib/common'67# Tests for running fzf in a tmux floating pane (--popup on tmux 3.7 or above)8class TestTmux < TestInteractive9  def setup10    super11    # Cannot rely on the exit status; tmux versions before 3.7 exit12    # normally with empty output for an unknown command name13    supported = IO.popen(%w[tmux list-commands new-pane], err: File::NULL, &:read).include?('new-pane')14    skip('floating panes not supported') unless supported15  end1617  def test_floating_pane18    tmux.send_keys "seq 100 | #{fzf('--popup center,80% --margin 0')}", :Enter19    tmux.until { |lines| assert_equal 100, lines.item_count }20    # Border text is empty when no label is given21    pane = floating_pane22    refute_nil pane23    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 :Enter28    assert_equal '99', fzf_output29  end3031  def test_floating_pane_killed32    tmux.send_keys "seq 100 | #{FZF} --popup bottom,50% --margin 0; echo code:$?", :Enter33    tmux.until { |lines| assert_equal 100, lines.item_count }34    pane = floating_pane35    refute_nil pane36    assert system('tmux', 'kill-pane', '-t', pane)37    tmux.until { |lines| assert lines.any_include?('code:130') }38  end3940  def test_floating_pane_border_label41    tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --border-label ' #fzf-label 100% '))}", :Enter42    tmux.until { |lines| assert_equal 100, lines.item_count }43    pane = floating_pane44    refute_nil pane45    # The label is held in a user option, not in the pane title, which is46    # left to the user. '#' is doubled so that the label is drawn as it is47    # written, instead of '#[...]' being taken as a style directive48    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.chomp52    tmux.send_keys :Enter53    assert_equal '1', fzf_output54  end5556  def test_floating_pane_change_border_label57    tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --bind 'space:change-border-label( #[fg=red] 100% )'))}", :Enter58    tmux.until { |lines| assert_equal 100, lines.item_count }59    pane = floating_pane60    refute_nil pane61    assert_equal '', pane_option(pane, '@fzf-border-label')62    tmux.send_keys :Space63    wait { assert_equal ' ##[fg=red] 100% ', pane_option(pane, '@fzf-border-label') }64    tmux.send_keys :Enter65    assert_equal '1', fzf_output66  end6768  # A program running in the pane owns the pane title, but not the label69  def test_floating_pane_border_label_not_affected_by_title70    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)'))}", :Enter71    tmux.until { |lines| assert_equal 100, lines.item_count }72    pane = floating_pane73    refute_nil pane74    tmux.send_keys :Space75    wait do76      title = IO.popen(['tmux', 'display-message', '-p', '-t', pane, "\#{pane_title}"], &:read)77      assert_equal 'hijacked', title.chomp78    end79    assert_equal ' label ', pane_option(pane, '@fzf-border-label')80    tmux.send_keys :Enter81    assert_equal '1', fzf_output82  end8384  def test_floating_pane_become85    tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --bind 'enter:become(echo became-{})'))}", :Enter86    tmux.until { |lines| assert_equal 100, lines.item_count }87    tmux.send_keys :Enter88    assert_equal 'became-1', fzf_output89  end9091  def test_explicit_border_falls_back_to_popup92    # display-popup requires an attached client, which the test environment93    # may not have; intercept it with a tmux shim on PATH94    dir = Dir.mktmpdir95    real = `command -v tmux`.chomp96    shim = File.join(dir, 'tmux')97    File.write(shim, <<~SH)98      #!/bin/sh99      if [ "$1" = display-popup ]; then100        echo popup-used >&2101        exit 0102      fi103      exec #{real.shellescape} "$@"104    SH105    FileUtils.chmod(0o755, shim)106    tmux.send_keys "seq 100 | PATH=#{dir.shellescape}:$PATH #{FZF} --popup center --border rounded", :Enter107    tmux.until { |lines| assert lines.any_include?('popup-used') }108    refute floating_pane109  ensure110    FileUtils.remove_entry(dir) if dir111  end112113  private114115  # stderr is merged in so that an unset option, which prints 'invalid116  # option' to stderr and nothing to stdout, is not read as an empty value117  def pane_option(pane, name)118    IO.popen(['tmux', 'show-options', '-p', '-t', pane, '-v', name], err: %i[child out], &:read).chomp119  end120121  def floating_pane122    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') }.first125  end126end