Skip to content

Commit

Permalink
[test] Prefer match_count over item_count
Browse files Browse the repository at this point in the history
match_count can lag behind item_count and can cause intermittent failures.
  • Loading branch information
junegunn committed Jan 26, 2025
1 parent 7cf45af commit 4b74f88
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
18 changes: 9 additions & 9 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_multi_order
def test_multi_max
tmux.send_keys "seq 1 10 | #{FZF} -m 3 --bind A:select-all,T:toggle-all --preview 'echo [{+}]/{}'", :Enter

tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }

tmux.send_keys '1'
tmux.until do |lines|
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_multi_max

def test_multi_action
tmux.send_keys "seq 10 | #{FZF} --bind 'a:change-multi,b:change-multi(3),c:change-multi(xxx),d:change-multi(0)'", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 ') }
tmux.send_keys 'a'
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (0)') }
Expand Down Expand Up @@ -1176,7 +1176,7 @@ def test_select_deselect

def test_unbind_rebind
tmux.send_keys "seq 100 | #{FZF} --bind 'c:clear-query,d:unbind(c,d),e:rebind(c,d)'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 'ab'
tmux.until { |lines| assert_equal '> ab', lines[-1] }
tmux.send_keys 'c'
Expand All @@ -1189,7 +1189,7 @@ def test_unbind_rebind

def test_scroll_off
tmux.send_keys "seq 1000 | #{FZF} --scroll-off=3 --bind l:last", :Enter
tmux.until { |lines| assert_equal 1000, lines.item_count }
tmux.until { |lines| assert_equal 1000, lines.match_count }
height = tmux.until { |lines| lines }.first.to_i
tmux.send_keys :PgUp
tmux.until do |lines|
Expand All @@ -1208,7 +1208,7 @@ def test_scroll_off

def test_scroll_off_large
tmux.send_keys "seq 1000 | #{FZF} --scroll-off=9999", :Enter
tmux.until { |lines| assert_equal 1000, lines.item_count }
tmux.until { |lines| assert_equal 1000, lines.match_count }
height = tmux.until { |lines| lines }.first.to_i
tmux.send_keys :PgUp
tmux.until { |lines| assert_equal "> #{height}", lines[height / 2].strip }
Expand Down Expand Up @@ -1253,7 +1253,7 @@ def test_focus_event

def test_result_event
tmux.send_keys '(echo 0; seq 10) | fzf --bind "result:pos(2)"', :Enter
tmux.until { |lines| assert_equal 11, lines.item_count }
tmux.until { |lines| assert_equal 11, lines.match_count }
tmux.until { |lines| assert_includes lines, '> 1' }
tmux.send_keys '9'
tmux.until { |lines| assert_includes lines, '> 9' }
Expand Down Expand Up @@ -1397,7 +1397,7 @@ def test_info_command_and_focus

def test_prev_next_selected
tmux.send_keys 'seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected', :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.send_keys :BTab, :BTab, :Up, :BTab
tmux.until { |lines| assert_equal 3, lines.select_count }
tmux.send_keys 'C-n'
Expand Down Expand Up @@ -1530,7 +1530,7 @@ def test_one_and_zero

def test_height_range_with_exit_0
tmux.send_keys "seq 10 | #{FZF} --height ~10% --exit-0", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.send_keys :c
tmux.until { |lines| assert_equal 0, lines.match_count }
end
Expand All @@ -1542,7 +1542,7 @@ def test_delete_with_modifiers
skip('CTRL-DELETE is not properly handled in GitHub Actions environment')
end
tmux.send_keys "seq 100 | #{FZF} --bind 'ctrl-delete:up+up,shift-delete:down,focus:transform-prompt:echo [{}]'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 'C-Delete'
tmux.until { |lines| assert_equal '[3]', lines[-1] }
tmux.send_keys 'S-Delete'
Expand Down
28 changes: 14 additions & 14 deletions test/test_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_execute
opts = %[--bind "alt-a:execute(echo /{}/ >> #{output})+change-header(alt-a),alt-b:execute[echo /{}{}/ >> #{output}]+change-header(alt-b),C:execute(echo /{}{}{}/ >> #{output})+change-header(C)"]
writelines(%w[foo'bar foo"bar foo$bar])
tmux.send_keys "cat #{tempname} | #{FZF} #{opts}", :Enter
tmux.until { |lines| assert_equal 3, lines.item_count }
tmux.until { |lines| assert_equal 3, lines.match_count }

ready = ->(s) { tmux.until { |lines| assert_includes lines[-3], s } }
tmux.send_keys :Escape, :a
Expand Down Expand Up @@ -142,11 +142,11 @@ def test_execute_shell

def test_interrupt_execute
tmux.send_keys "seq 100 | #{FZF} --bind 'ctrl-l:execute:echo executing {}; sleep 100'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 'C-l'
tmux.until { |lines| assert lines.any_include?('executing 1') }
tmux.send_keys 'C-c'
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 99
tmux.until { |lines| assert_equal 1, lines.match_count }
end
Expand All @@ -158,7 +158,7 @@ def test_kill_default_command_on_abort
system("chmod +x #{tempname}")

tmux.send_keys FZF.sub('FZF_DEFAULT_COMMAND=', "FZF_DEFAULT_COMMAND=#{tempname}"), :Enter
tmux.until { |lines| assert_equal 1, lines.item_count }
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'C-c'
tmux.send_keys 'C-l', 'closed'
tmux.until { |lines| assert_includes lines[0], 'closed' }
Expand Down Expand Up @@ -189,9 +189,9 @@ def test_kill_reload_command_on_abort
system("chmod +x #{tempname}")

tmux.send_keys "seq 1 3 | #{FZF} --bind 'ctrl-r:reload(#{tempname})'", :Enter
tmux.until { |lines| assert_equal 3, lines.item_count }
tmux.until { |lines| assert_equal 3, lines.match_count }
tmux.send_keys 'C-r'
tmux.until { |lines| assert_equal 1, lines.item_count }
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'C-c'
tmux.send_keys 'C-l', 'closed'
tmux.until { |lines| assert_includes lines[0], 'closed' }
Expand All @@ -207,9 +207,9 @@ def test_kill_reload_command_on_accept
system("chmod +x #{tempname}")

tmux.send_keys "seq 1 3 | #{fzf("--bind 'ctrl-r:reload(#{tempname})'")}", :Enter
tmux.until { |lines| assert_equal 3, lines.item_count }
tmux.until { |lines| assert_equal 3, lines.match_count }
tmux.send_keys 'C-r'
tmux.until { |lines| assert_equal 1, lines.item_count }
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys :Enter
assert_equal 'Started', fzf_output
wait { refute system("pgrep -f #{tempname}") }
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_reload_even_when_theres_no_match

def test_reload_should_terminate_standard_input_stream
tmux.send_keys %(ruby -e "STDOUT.sync = true; loop { puts 1; sleep 0.1 }" | fzf --bind 'start:reload(seq 100)'), :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
end

def test_clear_list_when_header_lines_changed_due_to_reload
Expand All @@ -264,7 +264,7 @@ def test_item_index_reset_on_reload
tmux.until { |lines| assert_includes lines[1], '[[0]]' }
tmux.send_keys :Space
tmux.until do |lines|
assert_equal 100, lines.item_count
assert_equal 100, lines.match_count
assert_includes lines[1], '[[0]]'
end
tmux.send_keys :Up
Expand All @@ -280,16 +280,16 @@ def test_reload_should_update_preview

def test_reload_and_change_preview_should_update_preview
tmux.send_keys "seq 3 | #{FZF} --bind 'ctrl-t:reload(echo 4)+change-preview(echo {})'", :Enter
tmux.until { |lines| assert_equal 3, lines.item_count }
tmux.until { |lines| assert_equal 3, lines.match_count }
tmux.until { |lines| refute_includes lines[1], '1' }
tmux.send_keys 'C-t'
tmux.until { |lines| assert_equal 1, lines.item_count }
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.until { |lines| assert_includes lines[1], '4' }
end

def test_reload_sync
tmux.send_keys "seq 100 | #{FZF} --bind 'load:reload-sync(sleep 1; seq 1000)+unbind(load)'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys '00'
tmux.until { |lines| assert_equal 1, lines.match_count }
# After 1 second
Expand Down Expand Up @@ -404,7 +404,7 @@ def test_start_on_reload

def test_become
tmux.send_keys "seq 100 | #{FZF} --bind 'enter:become:seq {} | #{FZF}'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 999
tmux.until { |lines| assert_equal 0, lines.match_count }
tmux.send_keys :Enter
Expand Down
22 changes: 11 additions & 11 deletions test/test_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_preview_hidden
def test_preview_size_0
tmux.send_keys %(seq 100 | #{FZF} --reverse --preview 'echo {} >> #{tempname}; echo ' --preview-window 0 --bind space:toggle-preview), :Enter
tmux.until do |lines|
assert_equal 100, lines.item_count
assert_equal 100, lines.match_count
assert_equal ' 100/100', lines[1]
assert_equal '> 1', lines[2]
end
Expand All @@ -147,7 +147,7 @@ def test_preview_size_0

def test_preview_size_0_hidden
tmux.send_keys %(seq 100 | #{FZF} --reverse --preview 'echo {} >> #{tempname}; echo ' --preview-window 0,hidden --bind space:toggle-preview), :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys :Down, :Down
tmux.until { |lines| assert_includes lines, '> 3' }
wait { refute_path_exists tempname }
Expand Down Expand Up @@ -217,7 +217,7 @@ def test_preview_q_no_match_with_initial_query
def test_preview_update_on_select
tmux.send_keys %(seq 10 | fzf -m --preview 'echo {+}' --bind a:toggle-all),
:Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.send_keys 'a'
tmux.until { |lines| assert(lines.any? { |line| line.include?(' 1 2 3 4 5 ') }) }
tmux.send_keys 'a'
Expand All @@ -232,7 +232,7 @@ def test_preview_correct_tab_width_after_ansi_reset_code

def test_preview_bindings_with_default_preview
tmux.send_keys "seq 10 | #{FZF} --preview 'echo [{}]' --bind 'a:preview(echo [{}{}]),b:preview(echo [{}{}{}]),c:refresh-preview'", :Enter
tmux.until { |lines| lines.item_count == 10 }
tmux.until { |lines| lines.match_count == 10 }
tmux.until { |lines| assert_includes lines[1], '[1]' }
tmux.send_keys 'a'
tmux.until { |lines| assert_includes lines[1], '[11]' }
Expand All @@ -246,7 +246,7 @@ def test_preview_bindings_with_default_preview

def test_preview_bindings_without_default_preview
tmux.send_keys "seq 10 | #{FZF} --bind 'a:preview(echo [{}{}]),b:preview(echo [{}{}{}]),c:refresh-preview'", :Enter
tmux.until { |lines| lines.item_count == 10 }
tmux.until { |lines| lines.match_count == 10 }
tmux.until { |lines| refute_includes lines[1], '1' }
tmux.send_keys 'a'
tmux.until { |lines| assert_includes lines[1], '[11]' }
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_preview_scroll_begin_and_offset

def test_preview_clear_screen
tmux.send_keys %{seq 100 | #{FZF} --preview 'for i in $(seq 300); do (( i % 200 == 0 )) && printf "\\033[2J"; echo "[$i]"; sleep 0.001; done'}, :Enter
tmux.until { |lines| lines.item_count == 100 }
tmux.until { |lines| lines.match_count == 100 }
tmux.until { |lines| lines[1]&.include?('[200]') }
end

Expand All @@ -292,7 +292,7 @@ def test_preview_window_follow
file.sync = true

tmux.send_keys %(seq 100 | #{FZF} --preview 'echo start; tail -f "#{file.path}"' --preview-window follow --bind 'up:preview-up,down:preview-down,space:change-preview-window:follow|nofollow' --preview-window '~4'), :Enter
tmux.until { |lines| lines.item_count == 100 }
tmux.until { |lines| lines.match_count == 100 }

# Write to the temporary file, and check if the preview window is showing
# the last line of the file
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_close

def test_preview_header
tmux.send_keys "seq 100 | #{FZF} --bind ctrl-k:preview-up+preview-up,ctrl-j:preview-down+preview-down+preview-down --preview 'seq 1000' --preview-window 'top:+{1}:~3'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 100, lines.match_count }
top5 = ->(lines) { lines.drop(1).take(5).map { |s| s[/[0-9]+/] } }
tmux.until do |lines|
assert_includes lines[1], '4/1000'
Expand Down Expand Up @@ -412,7 +412,7 @@ def test_change_preview_window
'b:change-preview-window(down)+change-preview(echo =={}==)+change-preview-window(up),' \
'c:change-preview(),d:change-preview-window(hidden),' \
"e:preview(printf ::%${FZF_PREVIEW_COLUMNS}s{})+change-preview-window(up),f:change-preview-window(up,wrap)'", :Enter
tmux.until { |lines| assert_equal 1000, lines.item_count }
tmux.until { |lines| assert_equal 1000, lines.match_count }
tmux.until { |lines| assert_includes lines[0], '[[1]]' }

# change-preview action permanently changes the preview command set by --preview
Expand Down Expand Up @@ -515,15 +515,15 @@ def test_change_preview_window_rotate_hidden_down

def test_toggle_alternative_preview_window
tmux.send_keys "seq 10 | #{FZF} --bind space:toggle-preview --preview-window '<100000(hidden,up,border-none)' --preview 'echo /{}/{}/'", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.until { |lines| refute_includes lines, '/1/1/' }
tmux.send_keys :Space
tmux.until { |lines| assert_includes lines, '/1/1/' }
end

def test_alternative_preview_window_opts
tmux.send_keys "seq 10 | #{FZF} --preview-window '~5,2,+0,<100000(~0,+100,wrap,noinfo)' --preview 'seq 1000'", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
tmux.until do |lines|
assert_equal ['╭────╮', '│ 10 │', '│ 0 │', '│ 10 │', '│ 1 │'], lines.take(5).map(&:strip)
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_listen
"--listen --sync --bind 'start:execute-silent:echo $FZF_PORT > /tmp/fzf-port'" =>
-> { URI("http://localhost:#{File.read('/tmp/fzf-port').chomp}") } }.each do |opts, fn|
tmux.send_keys "seq 10 | fzf #{opts}", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
state = JSON.parse(Net::HTTP.get(fn.call), symbolize_names: true)
assert_equal 10, state[:totalCount]
assert_equal 10, state[:matchCount]
Expand All @@ -33,7 +33,7 @@ def test_listen
def test_listen_with_api_key
post_uri = URI('http://localhost:6266')
tmux.send_keys 'seq 10 | FZF_API_KEY=123abc fzf --listen 6266', :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| assert_equal 10, lines.match_count }
# Incorrect API Key
[nil, { 'x-api-key' => '' }, { 'x-api-key' => '124abc' }].each do |headers|
res = Net::HTTP.post(post_uri, 'change-query(yo)+reload(seq 100)+change-prompt:hundred> ', headers)
Expand Down

0 comments on commit 4b74f88

Please sign in to comment.