Skip to content
Myr

Add basic test cases for shell extensions (#83)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Key bindings for bash, zsh, and fish - Fuzzy completion for bash (file, dir, process)

authored by Junegunn Choi · committed by Junegunn Choi · parent c1aa5c5 · Browse files · 2 files+241 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+241 added−21 removed

  • tests:+237 added−21 removed
  • other:+4 added−0 removed

2 files+241 added−21 removed

modified:test/test_go.rb+237 added-21 removed
1#!/usr/bin/env ruby2# encoding: utf-834require 'minitest/autorun'5require 'fileutils'67class NilClass8 def include? str9 false10 end1112 def start_with? str13 false14 end1516 def end_with? str17 false18 end19end2021module Temp22 def readonce
15 unmodified lines
38 end39 end40end4142class Shell43 class << self44 def bash45 'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.bash'46 end4748 def zsh49 FileUtils.mkdir_p '/tmp/fzf-zsh'50 FileUtils.cp File.expand_path('~/.fzf.zsh'), '/tmp/fzf-zsh/.zshrc'51 'PS1= PROMPT_COMMAND= HISTSIZE=100 ZDOTDIR=/tmp/fzf-zsh zsh'52 end53 end54end5556class Tmux57 include Temp5859 TEMPNAME = '/tmp/fzf-test.txt'6061 attr_reader :win6240 def initialize shell = 'bash'41 @win = go("new-window -d -P -F '#I' 'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.#{shell}'").first63 def initialize shell = :bash64 @win =65 case shell66 when :bash67 go("new-window -d -P -F '#I' '#{Shell.bash}'").first68 when :zsh69 go("new-window -d -P -F '#I' '#{Shell.zsh}'").first70 when :fish71 go("new-window -d -P -F '#I' 'fish'").first72 else73 raise "Unknown shell: #{shell}"74 end75 @lines = `tput lines`.chomp.to_i7677 if shell == :fish78 send_keys('function fish_prompt; end; clear', :Enter)79 self.until { |lines| lines.empty? }80 end81 end8283 def closed?84 !go("list-window -F '#I'").include?(win)85 end8649 def close timeout = 187 def close88 send_keys 'C-c', 'C-u', 'exit', :Enter51 wait(timeout) { closed? }89 wait { closed? }90 end9192 def kill93 go("kill-window -t #{win} 2> /dev/null")94 end9596 def send_keys *args97 target =98 if args.last.is_a?(Hash)99 hash = args.pop100 go("select-window -t #{win}")101 "#{win}.#{hash[:pane]}"102 else103 win104 end105 args = args.map { |a| %{"#{a}"} }.join ' '60 go("send-keys -t #{win} #{args}")106 go("send-keys -t #{target} #{args}")107 end10863 def capture64 go("capture-pane -t #{win} \\; save-buffer #{TEMPNAME}")65 raise "Window not found" if $?.exitstatus != 0109 def capture opts = {}110 timeout, pane = defaults(opts).values_at(:timeout, :pane)111 waited = 0112 loop do113 go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME}")114 break if $?.exitstatus == 0115116 if waited > timeout117 raise "Window not found"118 end119 waited += 0.1120 sleep 0.1121 end122 readonce.split($/)[0, @lines].reverse.drop_while(&:empty?).reverse123 end12469 def until timeout = 170 wait(timeout) { yield capture }125 def until opts = {}126 lines = nil127 wait(opts) do128 yield lines = capture(opts)129 end130 lines131 end132133 def prepare134 self.send_keys 'echo hello', :Enter135 self.until { |lines| lines[-1].start_with?('hello') }136 self.send_keys 'clear', :Enter137 self.until { |lines| lines.empty? }138 end139private74 def wait timeout = 1140 def defaults opts141 { timeout: 5, pane: 0 }.merge(opts)142 end143144 def wait opts = {}145 timeout, pane = defaults(opts).values_at(:timeout, :pane)146 waited = 0147 until yield77 waited += 0.178 sleep 0.1148 if waited > timeout149 hl = '=' * 10150 puts hl82 capture.each_with_index do |line, idx|151 capture(opts).each_with_index do |line, idx|152 puts [idx.to_s.rjust(2), line].join(': ')153 end154 puts hl155 raise "timeout"156 end157 waited += 0.1158 sleep 0.1159 end160 end161162 def go *args163 %x[tmux #{args.join ' '}].split($/)164 end165end16696class TestGoFZF < Minitest::Test167class TestBase < Minitest::Test168 include Temp169170 FIN = 'FIN'171 TEMPNAME = '/tmp/output'
2 unmodified lines
174175 def setup176 ENV.delete 'FZF_DEFAULT_OPTS'177 ENV.delete 'FZF_DEFAULT_COMMAND'107 @tmux = Tmux.new178 end179110 def teardown111 @tmux.kill112 end113180 def fzf(*opts)181 fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}"182 end183
10 unmodified lines
194 end195 }.compact196 "fzf #{opts.join ' '}"197 end198end199200class TestGoFZF < TestBase201 def setup202 super203 @tmux = Tmux.new204 end205206 def teardown207 @tmux.kill208 end209210 def test_vanilla211 tmux.send_keys "seq 1 100000 | #{fzf}", :Enter135 tmux.until(10) { |lines| lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }212 tmux.until(timeout: 10) { |lines|213 lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }214 lines = tmux.capture215 assert_equal ' 2', lines[-4]216 assert_equal '> 1', lines[-3]217 assert_equal ' 100000/100000', lines[-2]
206 unmodified lines
424 assert_equal %w[1000 900 800], readonce.split($/)425 end426end427428module TestShell429 def setup430 super431 end432433 def teardown434 @tmux.kill435 end436437 def test_ctrl_t438 tmux.prepare439 tmux.send_keys 'C-t', pane: 0440 lines = tmux.until(pane: 1) { |lines| lines[-1].start_with? '>' }441 expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')442 tmux.send_keys :BTab, :BTab, :Enter, pane: 1443 tmux.until(pane: 0) { |lines| lines[-1].include? expected }444 tmux.send_keys 'C-c'445446 # FZF_TMUX=0447 new_shell448 tmux.send_keys 'C-t', pane: 0449 lines = tmux.until(pane: 0) { |lines| lines[-1].start_with? '>' }450 expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')451 tmux.send_keys :BTab, :BTab, :Enter, pane: 0452 tmux.until(pane: 0) { |lines| lines[-1].include? expected }453 tmux.send_keys 'C-c', 'C-d'454 end455456 def test_alt_c457 tmux.prepare458 tmux.send_keys :Escape, :c459 lines = tmux.until { |lines| lines[-1].start_with? '>' }460 expected = lines[-3][2..-1]461 p expected462 tmux.send_keys :Enter463 tmux.prepare464 tmux.send_keys :pwd, :Enter465 tmux.until { |lines| p lines; lines[-1].end_with?(expected) }466 end467468 def test_ctrl_r469 tmux.prepare470 tmux.send_keys 'echo 1st', :Enter; tmux.prepare471 tmux.send_keys 'echo 2nd', :Enter; tmux.prepare472 tmux.send_keys 'echo 3d', :Enter; tmux.prepare473 tmux.send_keys 'echo 3rd', :Enter; tmux.prepare474 tmux.send_keys 'echo 4th', :Enter; tmux.prepare475 tmux.send_keys 'C-r'476 tmux.until { |lines| lines[-1].start_with? '>' }477 tmux.send_keys '3d'478 tmux.until { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort479 tmux.send_keys :Enter480 tmux.until { |lines| lines[-1] == 'echo 3rd' }481 tmux.send_keys :Enter482 tmux.until { |lines| lines[-1] == '3rd' }483 end484end485486class TestBash < TestBase487 include TestShell488489 def new_shell490 tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter491 tmux.prepare492 end493494 def setup495 super496 @tmux = Tmux.new :bash497 end498499 def test_file_completion500 tmux.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter501 tmux.prepare502 tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab503 tmux.until { |lines| lines[-1].start_with? '>' }504 tmux.send_keys :BTab, :BTab, :Enter505 tmux.until { |lines|506 lines[-1].include?('/tmp/fzf-test/10') &&507 lines[-1].include?('/tmp/fzf-test/100')508 }509 end510511 def test_dir_completion512 tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}', :Enter513 tmux.prepare514 tmux.send_keys 'cd /tmp/fzf-test/**', :Tab515 tmux.until { |lines| lines[-1].start_with? '>' }516 tmux.send_keys :BTab, :BTab # BTab does not work here517 tmux.send_keys 55518 tmux.until { |lines| lines[-2].start_with? ' 1/' }519 tmux.send_keys :Enter520 tmux.until { |lines| lines[-1] == 'cd /tmp/fzf-test/d55' }521 end522523 def test_process_completion524 tmux.send_keys 'sleep 12345 &', :Enter525 lines = tmux.until { |lines| lines[-1].start_with? '[1]' }526 pid = lines[-1].split.last527 tmux.prepare528 tmux.send_keys 'kill ', :Tab529 tmux.until { |lines| lines[-1].start_with? '>' }530 tmux.send_keys 'sleep12345'531 tmux.until { |lines| lines[-3].include? 'sleep 12345' }532 tmux.send_keys :Enter533 tmux.until { |lines| lines[-1] == "kill #{pid}" }534 end535end536537class TestZsh < TestBase538 include TestShell539540 def new_shell541 tmux.send_keys "FZF_TMUX=0 #{Shell.zsh}", :Enter542 tmux.prepare543 end544545 def setup546 super547 @tmux = Tmux.new :zsh548 end549end550551class TestFish < TestBase552 include TestShell553554 def new_shell555 tmux.send_keys 'env FZF_TMUX=0 fish', :Enter556 tmux.send_keys 'function fish_prompt; end; clear', :Enter557 tmux.until { |lines| lines.empty? }558 end559560 def setup561 super562 @tmux = Tmux.new :fish563 end564end565
modified:.travis.yml+4 added