Test refactoring
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 5794fd4 · Browse files · 1 file·+42 added−94 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−94 removed
- tests:+42 added−94 removed
Provenance
- Commit
5502b68- Tree
40a8bdf- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
5794fd4
1 file changed+42 added−94 removed
modified:test/test_go.rb+42 added-94 removed
2 unmodified lines3 4 require 'minitest/autorun'5 require 'fileutils'6 7+DEFAULT_TIMEOUT = 208+9 base = File.expand_path('../../', __FILE__)10 Dir.chdir base11 FZF = "#{base}/bin/fzf"12 10 unmodified lines23 false24 end25 end26 25-module Temp26- def readonce27- name = self.class::TEMPNAME28- waited = 029- while waited < 530- begin31- system 'sync'32- data = File.read(name)33- return data unless data.empty?34- rescue35- sleep 0.136- waited += 0.137- end38- end39- raise "failed to read tempfile"40- ensure41- while File.exists? name42- File.unlink name rescue nil43- end27+def wait28+ since = Time.now29+ while Time.now - since < DEFAULT_TIMEOUT30+ return if yield31+ sleep 0.0532 end33+ throw 'timeout'34 end35 36 class Shell37 class << self9 unmodified lines47 end48 end49 50 class Tmux62- include Temp63-51 TEMPNAME = '/tmp/fzf-test.txt'52 53 attr_reader :win54 42 unmodified lines97 args = args.map { |a| %{"#{a}"} }.join ' '98 go("send-keys -t #{target} #{args}")99 end100 114- def capture opts = {}115- timeout, pane = defaults(opts).values_at(:timeout, :pane)116- waited = 0117- loop do118- go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME}")119- break if $?.exitstatus == 0120-121- if waited > timeout122- raise "Window not found"123- end124- waited += 0.1125- sleep 0.1101+ def capture pane = 0102+ File.unlink TEMPNAME while File.exists? TEMPNAME103+ wait do104+ go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME} 2> /dev/null")105+ $?.exitstatus == 0106 end127- readonce.split($/)[0, @lines].reverse.drop_while(&:empty?).reverse107+ File.read(TEMPNAME).split($/)[0, @lines].reverse.drop_while(&:empty?).reverse108 end109 130- def until opts = {}110+ def until pane = 0111 lines = nil132- wait(opts) do133- lines = capture(opts)112+ wait do113+ lines = capture(pane)114 class << lines115 def item_count116 self[-2] ? self[-2].strip.split('/').last.to_i : 0117 end13 unmodified lines131 end132 self.send_keys 'C-u'133 end134 private155- def defaults opts156- { timeout: 10, pane: 0 }.merge(opts)157- end158-159- def wait opts = {}160- timeout, pane = defaults(opts).values_at(:timeout, :pane)161- waited = 0162- until yield163- if waited > timeout164- hl = '=' * 10165- puts hl166- capture(opts).each_with_index do |line, idx|167- puts [idx.to_s.rjust(2), line].join(': ')168- end169- puts hl170- raise "timeout"171- end172- waited += 0.1173- sleep 0.1174- end175- end176-135 def go *args136 %x[tmux #{args.join ' '}].split($/)137 end138 end139 140 class TestBase < Minitest::Test183- include Temp184-185- FIN = 'FIN'141 TEMPNAME = '/tmp/output'142 143 attr_reader :tmux144 145 def setup146 ENV.delete 'FZF_DEFAULT_OPTS'147 ENV.delete 'FZF_DEFAULT_COMMAND'148+ File.unlink TEMPNAME while File.exists?(TEMPNAME)149 end150 151+ def readonce152+ wait { File.exists?(TEMPNAME) }153+ File.read(TEMPNAME)154+ ensure155+ File.unlink TEMPNAME while File.exists?(TEMPNAME)156+ end157+158 def fzf(*opts)196- fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}"159+ fzf!(*opts) + " > #{TEMPNAME}.tmp; mv #{TEMPNAME}.tmp #{TEMPNAME}"160 end161 162 def fzf!(*opts)163 opts = opts.map { |o|22 unmodified lines186 end187 188 def test_vanilla189 tmux.send_keys "seq 1 100000 | #{fzf}", :Enter227- tmux.until(timeout: 20) { |lines|228- lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }190+ tmux.until { |lines| lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }191 lines = tmux.capture192 assert_equal ' 2', lines[-4]193 assert_equal '> 1', lines[-3]194 assert_equal ' 100000/100000', lines[-2]96 unmodified lines291 'C-K', 'C-K', 'C-K', 'C-K', :BTab, :BTab, # 5, 6292 :PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7293 tmux.until { |lines| lines[-2].include? '(6)' }294 tmux.send_keys "C-M"333- tmux.until { |lines| lines[-1].include?(FIN) }295 assert_equal %w[3 2 5 6 8 7], readonce.split($/)296 tmux.close297 end298 12 unmodified lines311 312 # However, the output must not be transformed313 if multi314 tmux.send_keys :BTab, :BTab, :Enter354- tmux.until { |lines| lines[-1].include?(FIN) }315 assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.split($/)316 else317 tmux.send_keys '^', '3'318 tmux.until { |lines| lines[-2].include?('1/2') }319 tmux.send_keys :Enter360- tmux.until { |lines| lines[-1].include?(FIN) }320 assert_equal [' 1st 2nd 3rd/'], readonce.split($/)321 end322 end323 end4 unmodified lines328 tmux.until { |lines| lines.include? ' 100/100' }329 tmux.send_keys *110.times.map { rev ? :Down : :Up }330 tmux.until { |lines| lines.include? '> 100' }331 tmux.send_keys :Enter373- tmux.until { |lines| lines[-1].include?(FIN) }332 assert_equal '100', readonce.chomp333 end334 end335 336 def test_select_1337 tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 5555, :'1'}", :Enter380- tmux.until { |lines| lines[-1].include?(FIN) }338 assert_equal ['5555', '55'], readonce.split($/)339 end340 341 def test_exit_0342 tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 555555, :'0'}", :Enter386- tmux.until { |lines| lines[-1].include?(FIN) }343 assert_equal ['555555'], readonce.split($/)344 end345 346 def test_select_1_exit_0_fail347 [:'0', :'1', [:'1', :'0']].each do |opt|348 tmux.send_keys "seq 1 100 | #{fzf :print_query, :multi, :q, 5, *opt}", :Enter349 tmux.until { |lines| lines.last =~ /^> 5/ }350 tmux.send_keys :BTab, :BTab, :BTab, :Enter395- tmux.until { |lines| lines[-1].include?(FIN) }351 assert_equal ['5', '5', '15', '25'], readonce.split($/)352 end353 end354 355 def test_query_unicode356 tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter357 tmux.until { |lines| lines[-2].include? '1/2' }358 tmux.send_keys :Enter404- tmux.until { |lines| lines[-1].include?(FIN) }359 assert_equal ['가나다'], readonce.split($/)360 end361 362 def test_sync140 unmodified lines503 assert_equal 1, `echo Foo bar | #{FZF} -x -f "foo Fbar" | wc -l`.to_i504 end505 506 private553- def writelines path, lines, timeout = 10554- File.open(path, 'w') do |f|555- f << lines.join($/)556- f.sync557- end558- since = Time.now559- while `cat #{path}`.split($/).length != lines.length && (Time.now - since) < 10560- sleep 0.1561- end507+ def writelines path, lines508+ File.unlink path while File.exists? path509+ File.open(path, 'w') { |f| f << lines.join($/) }510 end511 end512 513 module TestShell7 unmodified lines521 522 def test_ctrl_t523 tmux.prepare524 tmux.send_keys 'C-t', pane: 0577- lines = tmux.until(pane: 1) { |lines| lines.item_count > 0 }525+ lines = tmux.until(1) { |lines| lines.item_count > 0 }526 expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')527 tmux.send_keys :BTab, :BTab, :Enter, pane: 1580- tmux.until(pane: 0) { |lines| lines[-1].include? expected }528+ tmux.until(0) { |lines| lines[-1].include? expected }529 tmux.send_keys 'C-c'530 531 # FZF_TMUX=0532 new_shell533 tmux.send_keys 'C-t', pane: 0586- lines = tmux.until(pane: 0) { |lines| lines.item_count > 0 }534+ lines = tmux.until(0) { |lines| lines.item_count > 0 }535 expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')536 tmux.send_keys :BTab, :BTab, :Enter, pane: 0589- tmux.until(pane: 0) { |lines| lines[-1].include? expected }537+ tmux.until(0) { |lines| lines[-1].include? expected }538 tmux.send_keys 'C-c', 'C-d'539 end540 541 def test_alt_c542 tmux.prepare543 tmux.send_keys :Escape, :c, pane: 0596- lines = tmux.until(pane: 1) { |lines| lines.item_count > 0 }544+ lines = tmux.until(1) { |lines| lines.item_count > 0 }545 expected = lines[-3][2..-1]546 p expected547 tmux.send_keys :Enter, pane: 1548 tmux.prepare8 unmodified lines557 tmux.send_keys 'echo 3d', :Enter; tmux.prepare558 tmux.send_keys 'echo 3rd', :Enter; tmux.prepare559 tmux.send_keys 'echo 4th', :Enter; tmux.prepare560 tmux.send_keys 'C-r', pane: 0613- tmux.until(pane: 1) { |lines| lines.item_count > 0 }561+ tmux.until(1) { |lines| lines.item_count > 0 }562 tmux.send_keys '3d', pane: 1615- tmux.until(pane: 1) { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort563+ tmux.until(1) { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort564 tmux.send_keys :Enter, pane: 1565 tmux.until { |lines| lines[-1] == 'echo 3rd' }566 tmux.send_keys :Enter567 tmux.until { |lines| lines[-1] == '3rd' }16 unmodified lines584 def test_file_completion585 tmux.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter586 tmux.prepare587 tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0640- tmux.until(pane: 1) { |lines| lines.item_count > 0 }588+ tmux.until(1) { |lines| lines.item_count > 0 }589 tmux.send_keys :BTab, :BTab, :Enter590 tmux.until do |lines|591 tmux.send_keys 'C-L'592 lines[-1].include?('/tmp/fzf-test/10') &&4 unmodified lines597 def test_dir_completion598 tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter599 tmux.prepare600 tmux.send_keys 'cd /tmp/fzf-test/**', :Tab, pane: 0653- tmux.until(pane: 1) { |lines| lines.item_count > 0 }601+ tmux.until(1) { |lines| lines.item_count > 0 }602 tmux.send_keys :BTab, :BTab # BTab does not work here603 tmux.send_keys 55656- tmux.until(pane: 1) { |lines| lines[-2].start_with? ' 1/' }604+ tmux.until(1) { |lines| lines[-2].start_with? ' 1/' }605 tmux.send_keys :Enter606 tmux.until do |lines|607 tmux.send_keys 'C-L'608 lines[-1] == 'cd /tmp/fzf-test/d55/'17 unmodified lines626 lines = tmux.until { |lines| lines[-1].start_with? '[1]' }627 pid = lines[-1].split.last628 tmux.prepare629 tmux.send_keys 'kill ', :Tab, pane: 0682- tmux.until(pane: 1) { |lines| lines.item_count > 0 }630+ tmux.until(1) { |lines| lines.item_count > 0 }631 tmux.send_keys 'sleep12345'684- tmux.until(pane: 1) { |lines| lines[-3].include? 'sleep 12345' }632+ tmux.until(1) { |lines| lines[-3].include? 'sleep 12345' }633 tmux.send_keys :Enter634 tmux.until do |lines|635 tmux.send_keys 'C-L'636 lines[-1] == "kill #{pid}"33 unmodified lines