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
Provenance
- Commit
4d2d186- Tree
62ca9a2- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
c1aa5c5
2 files changed+241 added−21 removed
modified:test/test_go.rb+237 added-21 removed
1 #!/usr/bin/env ruby2 # encoding: utf-83 4 require 'minitest/autorun'5+require 'fileutils'6 7 class NilClass8 def include? str9 false10 end11+12+ def start_with? str13+ false14+ end15+16+ def end_with? str17+ false18+ end19 end20 21 module Temp22 def readonce15 unmodified lines38 end39 end40 end41 42+class Shell43+ class << self44+ def bash45+ 'PS1= PROMPT_COMMAND= bash --rcfile ~/.fzf.bash'46+ end47+48+ 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+ end54+end55+56 class Tmux57 include Temp58 59 TEMPNAME = '/tmp/fzf-test.txt'60 61 attr_reader :win62 40- 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_i76+77+ if shell == :fish78+ send_keys('function fish_prompt; end; clear', :Enter)79+ self.until { |lines| lines.empty? }80+ end81 end82 83 def closed?84 !go("list-window -F '#I'").include?(win)85 end86 49- def close timeout = 187+ def close88 send_keys 'C-c', 'C-u', 'exit', :Enter51- wait(timeout) { closed? }89+ wait { closed? }90 end91 92 def kill93 go("kill-window -t #{win} 2> /dev/null")94 end95 96 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 end108 63- 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 == 0115+116+ 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 end124 69- 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 end132 133+ 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+ end139 private74- def wait timeout = 1140+ def defaults opts141+ { timeout: 5, pane: 0 }.merge(opts)142+ end143+144+ 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 end161 162 def go *args163 %x[tmux #{args.join ' '}].split($/)164 end165 end166 96-class TestGoFZF < Minitest::Test167+class TestBase < Minitest::Test168 include Temp169 170 FIN = 'FIN'171 TEMPNAME = '/tmp/output'2 unmodified lines174 175 def setup176 ENV.delete 'FZF_DEFAULT_OPTS'177 ENV.delete 'FZF_DEFAULT_COMMAND'107- @tmux = Tmux.new178 end179 110- def teardown111- @tmux.kill112- end113-180 def fzf(*opts)181 fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}"182 end183 10 unmodified lines194 end195 }.compact196 "fzf #{opts.join ' '}"197 end198+end199 200+class TestGoFZF < TestBase201+ def setup202+ super203+ @tmux = Tmux.new204+ end205+206+ def teardown207+ @tmux.kill208+ end209+210 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 lines424 assert_equal %w[1000 900 800], readonce.split($/)425 end426 end427 428+module TestShell429+ def setup430+ super431+ end432+433+ def teardown434+ @tmux.kill435+ end436+437+ 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'445+446+ # 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+ end455+456+ 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+ end467+468+ 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+ end484+end485+486+class TestBash < TestBase487+ include TestShell488+489+ def new_shell490+ tmux.send_keys "FZF_TMUX=0 #{Shell.bash}", :Enter491+ tmux.prepare492+ end493+494+ def setup495+ super496+ @tmux = Tmux.new :bash497+ end498+499+ 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+ end510+511+ 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+ end522+523+ 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+ end535+end536+537+class TestZsh < TestBase538+ include TestShell539+540+ def new_shell541+ tmux.send_keys "FZF_TMUX=0 #{Shell.zsh}", :Enter542+ tmux.prepare543+ end544+545+ def setup546+ super547+ @tmux = Tmux.new :zsh548+ end549+end550+551+class TestFish < TestBase552+ include TestShell553+554+ 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+ end559+560+ def setup561+ super562+ @tmux = Tmux.new :fish563+ end564+end565+modified:.travis.yml+4 added
1234567891011121314151612 unmodified lineslanguage: rubyrvm:- 2.2.0
install:- sudo apt-get update- sudo apt-get install -y libncurses-dev lib32ncurses5-dev- sudo add-apt-repository -y ppa:pi-rho/dev- sudo apt-add-repository -y ppa:fish-shell/release-2- sudo apt-get update- sudo apt-get install -y tmux=1.9a-1~ppa1~p- sudo apt-get install -y zsh fish
script: | export GOROOT=~/go1.4 export GOPATH=~/go12 unmodified lines