Test refactoring
This commit is contained in:
parent
5794fd42df
commit
5502b68a1d
136
test/test_go.rb
136
test/test_go.rb
@ -4,6 +4,8 @@
|
|||||||
require 'minitest/autorun'
|
require 'minitest/autorun'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
|
|
||||||
|
DEFAULT_TIMEOUT = 20
|
||||||
|
|
||||||
base = File.expand_path('../../', __FILE__)
|
base = File.expand_path('../../', __FILE__)
|
||||||
Dir.chdir base
|
Dir.chdir base
|
||||||
FZF = "#{base}/bin/fzf"
|
FZF = "#{base}/bin/fzf"
|
||||||
@ -22,26 +24,13 @@ class NilClass
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Temp
|
def wait
|
||||||
def readonce
|
since = Time.now
|
||||||
name = self.class::TEMPNAME
|
while Time.now - since < DEFAULT_TIMEOUT
|
||||||
waited = 0
|
return if yield
|
||||||
while waited < 5
|
sleep 0.05
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
throw 'timeout'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Shell
|
class Shell
|
||||||
@ -59,8 +48,6 @@ class Shell
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Tmux
|
class Tmux
|
||||||
include Temp
|
|
||||||
|
|
||||||
TEMPNAME = '/tmp/fzf-test.txt'
|
TEMPNAME = '/tmp/fzf-test.txt'
|
||||||
|
|
||||||
attr_reader :win
|
attr_reader :win
|
||||||
@ -111,26 +98,19 @@ class Tmux
|
|||||||
go("send-keys -t #{target} #{args}")
|
go("send-keys -t #{target} #{args}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def capture opts = {}
|
def capture pane = 0
|
||||||
timeout, pane = defaults(opts).values_at(:timeout, :pane)
|
File.unlink TEMPNAME while File.exists? TEMPNAME
|
||||||
waited = 0
|
wait do
|
||||||
loop do
|
go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME} 2> /dev/null")
|
||||||
go("capture-pane -t #{win}.#{pane} \\; save-buffer #{TEMPNAME}")
|
$?.exitstatus == 0
|
||||||
break if $?.exitstatus == 0
|
|
||||||
|
|
||||||
if waited > timeout
|
|
||||||
raise "Window not found"
|
|
||||||
end
|
end
|
||||||
waited += 0.1
|
File.read(TEMPNAME).split($/)[0, @lines].reverse.drop_while(&:empty?).reverse
|
||||||
sleep 0.1
|
|
||||||
end
|
|
||||||
readonce.split($/)[0, @lines].reverse.drop_while(&:empty?).reverse
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def until opts = {}
|
def until pane = 0
|
||||||
lines = nil
|
lines = nil
|
||||||
wait(opts) do
|
wait do
|
||||||
lines = capture(opts)
|
lines = capture(pane)
|
||||||
class << lines
|
class << lines
|
||||||
def item_count
|
def item_count
|
||||||
self[-2] ? self[-2].strip.split('/').last.to_i : 0
|
self[-2] ? self[-2].strip.split('/').last.to_i : 0
|
||||||
@ -152,37 +132,12 @@ class Tmux
|
|||||||
self.send_keys 'C-u'
|
self.send_keys 'C-u'
|
||||||
end
|
end
|
||||||
private
|
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
|
def go *args
|
||||||
%x[tmux #{args.join ' '}].split($/)
|
%x[tmux #{args.join ' '}].split($/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestBase < Minitest::Test
|
class TestBase < Minitest::Test
|
||||||
include Temp
|
|
||||||
|
|
||||||
FIN = 'FIN'
|
|
||||||
TEMPNAME = '/tmp/output'
|
TEMPNAME = '/tmp/output'
|
||||||
|
|
||||||
attr_reader :tmux
|
attr_reader :tmux
|
||||||
@ -190,10 +145,18 @@ class TestBase < Minitest::Test
|
|||||||
def setup
|
def setup
|
||||||
ENV.delete 'FZF_DEFAULT_OPTS'
|
ENV.delete 'FZF_DEFAULT_OPTS'
|
||||||
ENV.delete 'FZF_DEFAULT_COMMAND'
|
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
|
end
|
||||||
|
|
||||||
def fzf(*opts)
|
def fzf(*opts)
|
||||||
fzf!(*opts) + " > #{TEMPNAME} && echo #{FIN}"
|
fzf!(*opts) + " > #{TEMPNAME}.tmp; mv #{TEMPNAME}.tmp #{TEMPNAME}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def fzf!(*opts)
|
def fzf!(*opts)
|
||||||
@ -224,8 +187,7 @@ class TestGoFZF < TestBase
|
|||||||
|
|
||||||
def test_vanilla
|
def test_vanilla
|
||||||
tmux.send_keys "seq 1 100000 | #{fzf}", :Enter
|
tmux.send_keys "seq 1 100000 | #{fzf}", :Enter
|
||||||
tmux.until(timeout: 20) { |lines|
|
tmux.until { |lines| lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }
|
||||||
lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }
|
|
||||||
lines = tmux.capture
|
lines = tmux.capture
|
||||||
assert_equal ' 2', lines[-4]
|
assert_equal ' 2', lines[-4]
|
||||||
assert_equal '> 1', lines[-3]
|
assert_equal '> 1', lines[-3]
|
||||||
@ -330,7 +292,6 @@ class TestGoFZF < TestBase
|
|||||||
:PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7
|
:PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7
|
||||||
tmux.until { |lines| lines[-2].include? '(6)' }
|
tmux.until { |lines| lines[-2].include? '(6)' }
|
||||||
tmux.send_keys "C-M"
|
tmux.send_keys "C-M"
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal %w[3 2 5 6 8 7], readonce.split($/)
|
assert_equal %w[3 2 5 6 8 7], readonce.split($/)
|
||||||
tmux.close
|
tmux.close
|
||||||
end
|
end
|
||||||
@ -351,13 +312,11 @@ class TestGoFZF < TestBase
|
|||||||
# However, the output must not be transformed
|
# However, the output must not be transformed
|
||||||
if multi
|
if multi
|
||||||
tmux.send_keys :BTab, :BTab, :Enter
|
tmux.send_keys :BTab, :BTab, :Enter
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.split($/)
|
assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.split($/)
|
||||||
else
|
else
|
||||||
tmux.send_keys '^', '3'
|
tmux.send_keys '^', '3'
|
||||||
tmux.until { |lines| lines[-2].include?('1/2') }
|
tmux.until { |lines| lines[-2].include?('1/2') }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal [' 1st 2nd 3rd/'], readonce.split($/)
|
assert_equal [' 1st 2nd 3rd/'], readonce.split($/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -370,20 +329,17 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys *110.times.map { rev ? :Down : :Up }
|
tmux.send_keys *110.times.map { rev ? :Down : :Up }
|
||||||
tmux.until { |lines| lines.include? '> 100' }
|
tmux.until { |lines| lines.include? '> 100' }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal '100', readonce.chomp
|
assert_equal '100', readonce.chomp
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_select_1
|
def test_select_1
|
||||||
tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 5555, :'1'}", :Enter
|
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($/)
|
assert_equal ['5555', '55'], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exit_0
|
def test_exit_0
|
||||||
tmux.send_keys "seq 1 100 | #{fzf :with_nth, '..,..', :print_query, :q, 555555, :'0'}", :Enter
|
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($/)
|
assert_equal ['555555'], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -392,7 +348,6 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys "seq 1 100 | #{fzf :print_query, :multi, :q, 5, *opt}", :Enter
|
tmux.send_keys "seq 1 100 | #{fzf :print_query, :multi, :q, 5, *opt}", :Enter
|
||||||
tmux.until { |lines| lines.last =~ /^> 5/ }
|
tmux.until { |lines| lines.last =~ /^> 5/ }
|
||||||
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
tmux.send_keys :BTab, :BTab, :BTab, :Enter
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal ['5', '5', '15', '25'], readonce.split($/)
|
assert_equal ['5', '5', '15', '25'], readonce.split($/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -401,7 +356,6 @@ class TestGoFZF < TestBase
|
|||||||
tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter
|
tmux.send_keys "(echo abc; echo 가나다) | #{fzf :query, '가다'}", :Enter
|
||||||
tmux.until { |lines| lines[-2].include? '1/2' }
|
tmux.until { |lines| lines[-2].include? '1/2' }
|
||||||
tmux.send_keys :Enter
|
tmux.send_keys :Enter
|
||||||
tmux.until { |lines| lines[-1].include?(FIN) }
|
|
||||||
assert_equal ['가나다'], readonce.split($/)
|
assert_equal ['가나다'], readonce.split($/)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -550,15 +504,9 @@ class TestGoFZF < TestBase
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def writelines path, lines, timeout = 10
|
def writelines path, lines
|
||||||
File.open(path, 'w') do |f|
|
File.unlink path while File.exists? path
|
||||||
f << lines.join($/)
|
File.open(path, 'w') { |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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -574,26 +522,26 @@ module TestShell
|
|||||||
def test_ctrl_t
|
def test_ctrl_t
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys 'C-t', pane: 0
|
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(' ')
|
expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')
|
||||||
tmux.send_keys :BTab, :BTab, :Enter, pane: 1
|
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'
|
tmux.send_keys 'C-c'
|
||||||
|
|
||||||
# FZF_TMUX=0
|
# FZF_TMUX=0
|
||||||
new_shell
|
new_shell
|
||||||
tmux.send_keys 'C-t', pane: 0
|
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(' ')
|
expected = lines.values_at(-3, -4).map { |line| line[2..-1] }.join(' ')
|
||||||
tmux.send_keys :BTab, :BTab, :Enter, pane: 0
|
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'
|
tmux.send_keys 'C-c', 'C-d'
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_alt_c
|
def test_alt_c
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys :Escape, :c, pane: 0
|
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]
|
expected = lines[-3][2..-1]
|
||||||
p expected
|
p expected
|
||||||
tmux.send_keys :Enter, pane: 1
|
tmux.send_keys :Enter, pane: 1
|
||||||
@ -610,9 +558,9 @@ module TestShell
|
|||||||
tmux.send_keys 'echo 3rd', :Enter; tmux.prepare
|
tmux.send_keys 'echo 3rd', :Enter; tmux.prepare
|
||||||
tmux.send_keys 'echo 4th', :Enter; tmux.prepare
|
tmux.send_keys 'echo 4th', :Enter; tmux.prepare
|
||||||
tmux.send_keys 'C-r', pane: 0
|
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.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.send_keys :Enter, pane: 1
|
||||||
tmux.until { |lines| lines[-1] == 'echo 3rd' }
|
tmux.until { |lines| lines[-1] == 'echo 3rd' }
|
||||||
tmux.send_keys :Enter
|
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.send_keys 'mkdir -p /tmp/fzf-test; touch /tmp/fzf-test/{1..100}', :Enter
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys 'cat /tmp/fzf-test/10**', :Tab, pane: 0
|
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.send_keys :BTab, :BTab, :Enter
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
tmux.send_keys 'C-L'
|
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.send_keys 'mkdir -p /tmp/fzf-test/d{1..100}; touch /tmp/fzf-test/d55/xxx', :Enter
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys 'cd /tmp/fzf-test/**', :Tab, pane: 0
|
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 :BTab, :BTab # BTab does not work here
|
||||||
tmux.send_keys 55
|
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.send_keys :Enter
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
tmux.send_keys 'C-L'
|
tmux.send_keys 'C-L'
|
||||||
@ -679,9 +627,9 @@ class TestBash < TestBase
|
|||||||
pid = lines[-1].split.last
|
pid = lines[-1].split.last
|
||||||
tmux.prepare
|
tmux.prepare
|
||||||
tmux.send_keys 'kill ', :Tab, pane: 0
|
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.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.send_keys :Enter
|
||||||
tmux.until do |lines|
|
tmux.until do |lines|
|
||||||
tmux.send_keys 'C-L'
|
tmux.send_keys 'C-L'
|
||||||
|
Loading…
Reference in New Issue
Block a user