From 5502b68a1de42d4e23ba2192f900e5a7157d8900 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 25 Apr 2015 10:39:04 +0900 Subject: [PATCH] Test refactoring --- test/test_go.rb | 136 +++++++++++++++--------------------------------- 1 file changed, 42 insertions(+), 94 deletions(-) diff --git a/test/test_go.rb b/test/test_go.rb index 9a7055d..c9ca346 100644 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -4,6 +4,8 @@ require 'minitest/autorun' require 'fileutils' +DEFAULT_TIMEOUT = 20 + base = File.expand_path('../../', __FILE__) Dir.chdir base FZF = "#{base}/bin/fzf" @@ -22,26 +24,13 @@ class NilClass end end -module Temp - def readonce - name = self.class::TEMPNAME - waited = 0 - while waited < 5 - begin - system 'sync' - data = File.read(name) - return data unless data.empty? - rescue - sleep 0.1 - waited += 0.1 - end - end - raise "failed to read tempfile" - ensure - while File.exists? name - File.unlink name rescue nil - end +def wait + since = Time.now + while Time.now - since < DEFAULT_TIMEOUT + return if yield + sleep 0.05 end + throw 'timeout' end class Shell @@ -59,8 +48,6 @@ class Shell end class Tmux - include Temp - TEMPNAME = '/tmp/fzf-test.txt' attr_reader :win @@ -111,26 +98,19 @@ class Tmux go("send-keys -t #{target} #{args}") end - def capture opts = {} - timeout, pane = defaults(opts).values_at(:timeout, :pane) - waited = 0 - loop do - go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME}") - break if $?.exitstatus == 0 - - if waited > timeout - raise "Window not found" - end - waited += 0.1 - sleep 0.1 + def capture pane = 0 + File.unlink TEMPNAME while File.exists? TEMPNAME + wait do + go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME} 2> /dev/null") + $?.exitstatus == 0 end - readonce.split($/)[0, @lines].reverse.drop_while(&:empty?).reverse + File.read(TEMPNAME).split($/)[0, @lines].reverse.drop_while(&:empty?).reverse end - def until opts = {} + def until pane = 0 lines = nil - wait(opts) do - lines = capture(opts) + wait do + lines = capture(pane) class << lines def item_count self[-2] ? self[-2].strip.split('/').last.to_i : 0 @@ -152,37 +132,12 @@ class Tmux self.send_keys 'C-u' end private - def defaults opts - { timeout: 10, pane: 0 }.merge(opts) - end - - def wait opts = {} - timeout, pane = defaults(opts).values_at(:timeout, :pane) - waited = 0 - until yield - if waited > timeout - hl = '=' * 10 - puts hl - capture(opts).each_with_index do |line, idx| - puts [idx.to_s.rjust(2), line].join(': ') - end - puts hl - raise "timeout" - end - waited += 0.1 - sleep 0.1 - end - end - def go *args %x[tmux #{args.join ' '}].split($/) end end class TestBase < Minitest::Test - include Temp - - FIN = 'FIN' TEMPNAME = '/tmp/output' attr_reader :tmux @@ -190,10 +145,18 @@ class TestBase < Minitest::Test def setup ENV.delete 'FZF_DEFAULT_OPTS' ENV.delete 'FZF_DEFAULT_COMMAND' + File.unlink TEMPNAME while File.exists?(TEMPNAME) + end + + def readonce + wait { File.exists?(TEMPNAME) } + File.read(TEMPNAME) + ensure + File.unlink TEMPNAME while File.exists?(TEMPNAME) end def fzf(*opts) - fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}" + fzf!(*opts) + " > #{TEMPNAME}.tmp; mv #{TEMPNAME}.tmp #{TEMPNAME}" end def fzf!(*opts) @@ -224,8 +187,7 @@ class TestGoFZF < TestBase def test_vanilla tmux.send_keys "seq 1 100000 | #{fzf}", :Enter - tmux.until(timeout: 20) { |lines| - lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ } + tmux.until { |lines| lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ } lines = tmux.capture assert_equal ' 2', lines[-4] assert_equal '> 1', lines[-3] @@ -330,7 +292,6 @@ class TestGoFZF < TestBase :PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7 tmux.until { |lines| lines[-2].include? '(6)' } tmux.send_keys "C-M" - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal %w[3 2 5 6 8 7], readonce.split($/) tmux.close end @@ -351,13 +312,11 @@ class TestGoFZF < TestBase # However, the output must not be transformed if multi tmux.send_keys :BTab, :BTab, :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.split($/) else tmux.send_keys '^', '3' tmux.until { |lines| lines[-2].include?('1/2') } tmux.send_keys :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal [' 1st 2nd 3rd/'], readonce.split($/) end end @@ -370,20 +329,17 @@ class TestGoFZF < TestBase tmux.send_keys *110.times.map { rev ? :Down : :Up } tmux.until { |lines| lines.include? '> 100' } tmux.send_keys :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal '100', readonce.chomp end end def test_select_1 tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 5555, :'1'}", :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal ['5555', '55'], readonce.split($/) end def test_exit_0 tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 555555, :'0'}", :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal ['555555'], readonce.split($/) end @@ -392,7 +348,6 @@ class TestGoFZF < TestBase tmux.send_keys "seq 1 100 | #{fzf :print_query, :multi, :q, 5, *opt}", :Enter tmux.until { |lines| lines.last =~ /^> 5/ } tmux.send_keys :BTab, :BTab, :BTab, :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal ['5', '5', '15', '25'], readonce.split($/) end end @@ -401,7 +356,6 @@ class TestGoFZF < TestBase tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter tmux.until { |lines| lines[-2].include? '1/2' } tmux.send_keys :Enter - tmux.until { |lines| lines[-1].include?(FIN) } assert_equal ['가나다'], readonce.split($/) end @@ -550,15 +504,9 @@ class TestGoFZF < TestBase end private - def writelines path, lines, timeout = 10 - File.open(path, 'w') do |f| - f << lines.join($/) - f.sync - end - since = Time.now - while `cat #{path}`.split($/).length != lines.length && (Time.now - since) < 10 - sleep 0.1 - end + def writelines path, lines + File.unlink path while File.exists? path + File.open(path, 'w') { |f| f << lines.join($/) } end end @@ -574,26 +522,26 @@ module TestShell def test_ctrl_t tmux.prepare tmux.send_keys 'C-t', pane: 0 - lines = tmux.until(pane: 1) { |lines| lines.item_count > 0 } + lines = tmux.until(1) { |lines| lines.item_count > 0 } expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ') tmux.send_keys :BTab, :BTab, :Enter, pane: 1 - tmux.until(pane: 0) { |lines| lines[-1].include? expected } + tmux.until(0) { |lines| lines[-1].include? expected } tmux.send_keys 'C-c' # FZF_TMUX=0 new_shell tmux.send_keys 'C-t', pane: 0 - lines = tmux.until(pane: 0) { |lines| lines.item_count > 0 } + lines = tmux.until(0) { |lines| lines.item_count > 0 } expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ') tmux.send_keys :BTab, :BTab, :Enter, pane: 0 - tmux.until(pane: 0) { |lines| lines[-1].include? expected } + tmux.until(0) { |lines| lines[-1].include? expected } tmux.send_keys 'C-c', 'C-d' end def test_alt_c tmux.prepare tmux.send_keys :Escape, :c, pane: 0 - lines = tmux.until(pane: 1) { |lines| lines.item_count > 0 } + lines = tmux.until(1) { |lines| lines.item_count > 0 } expected = lines[-3][2..-1] p expected tmux.send_keys :Enter, pane: 1 @@ -610,9 +558,9 @@ module TestShell tmux.send_keys 'echo 3rd', :Enter; tmux.prepare tmux.send_keys 'echo 4th', :Enter; tmux.prepare tmux.send_keys 'C-r', pane: 0 - tmux.until(pane: 1) { |lines| lines.item_count > 0 } + tmux.until(1) { |lines| lines.item_count > 0 } tmux.send_keys '3d', pane: 1 - tmux.until(pane: 1) { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort + tmux.until(1) { |lines| lines[-3].end_with? 'echo 3rd' } # --no-sort tmux.send_keys :Enter, pane: 1 tmux.until { |lines| lines[-1] == 'echo 3rd' } tmux.send_keys :Enter @@ -637,7 +585,7 @@ class TestBash < TestBase tmux.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter tmux.prepare tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0 - tmux.until(pane: 1) { |lines| lines.item_count > 0 } + tmux.until(1) { |lines| lines.item_count > 0 } tmux.send_keys :BTab, :BTab, :Enter tmux.until do |lines| tmux.send_keys 'C-L' @@ -650,10 +598,10 @@ class TestBash < TestBase tmux.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter tmux.prepare tmux.send_keys 'cd /tmp/fzf-test/**', :Tab, pane: 0 - tmux.until(pane: 1) { |lines| lines.item_count > 0 } + tmux.until(1) { |lines| lines.item_count > 0 } tmux.send_keys :BTab, :BTab # BTab does not work here tmux.send_keys 55 - tmux.until(pane: 1) { |lines| lines[-2].start_with? ' 1/' } + tmux.until(1) { |lines| lines[-2].start_with? ' 1/' } tmux.send_keys :Enter tmux.until do |lines| tmux.send_keys 'C-L' @@ -679,9 +627,9 @@ class TestBash < TestBase pid = lines[-1].split.last tmux.prepare tmux.send_keys 'kill ', :Tab, pane: 0 - tmux.until(pane: 1) { |lines| lines.item_count > 0 } + tmux.until(1) { |lines| lines.item_count > 0 } tmux.send_keys 'sleep12345' - tmux.until(pane: 1) { |lines| lines[-3].include? 'sleep 12345' } + tmux.until(1) { |lines| lines[-3].include? 'sleep 12345' } tmux.send_keys :Enter tmux.until do |lines| tmux.send_keys 'C-L'