Repository: junegunn/fzf
File: test/test_go.rb (Ruby)

Code before:
  def test_ctrl_r
    tmux.prepare
    tmux.send_keys 'echo 1st', :Enter; tmux.prepare
    tmux.send_keys 'echo 2nd', :Enter; tmux.prepare
    tmux.send_keys 'echo 3d',  :Enter; tmux.prepare
    tmux.send_keys 'echo 3rd', :Enter; tmux.prepare
    tmux.send_keys 'echo 4th', :Enter
    retries do
      tmux.prepare
      tmux.send_keys 'C-r'
      tmux.until { |lines| lines.match_count.positive? }
    end
    tmux.send_keys 'C-r'
    tmux.send_keys '3d'
    tmux.until { |lines| lines[-3].end_with? 'echo 3rd' }
    tmux.send_keys :Enter
    tmux.until { |lines| lines[-1] == 'echo 3rd' }
    tmux.send_keys :Enter
    tmux.until { |lines| lines[-1] == '3rd' }
  end

  def test_ctrl_r_abort
    skip "doesn't restore the original line when search is aborted pre Bash 4" if self.is_a?(TestBash) && /(?<= version )\d+/.match(`#{Shell.bash} --version`).to_s.to_i < 4
    # Why is this necessary? To wait until the shell is ready?
    tmux.send_keys 'echo hello', :Enter
    tmux.until { |lines| lines[-1] == 'hello' }

    tmux.send_keys 'foo'
    tmux.until { |lines| lines[-1].start_with? 'foo' }
    tmux.send_keys 'C-r'
    tmux.until { |lines| lines[-1].start_with? '>' }
    tmux.send_keys 'C-g'
    tmux.until { |lines| lines[-1].start_with? 'foo' }
  end

  def retries(times = 3)
    (times - 1).times do
      begin
        return yield
      rescue RuntimeError
      end
    end
    yield
  end
end

module CompletionTest
  def test_file_completion
    FileUtils.mkdir_p '/tmp/fzf-test'
    FileUtils.mkdir_p '/tmp/fzf test'
    (1..100).each { |i| FileUtils.touch "/tmp/fzf-test/#{i}" }

Review comment (suggestion):
Yeah, I guess. You probably should use `tmux.prepare` instead, which also retries for a few times. It's used all over the test cases.

Code after:
    expected = lines.reverse.select { |l| l.start_with? '>' }.first[2..-1]
    tmux.send_keys :Enter
    tmux.prepare
    tmux.send_keys :pwd, :Enter
    tmux.until { |lines| lines[-1].end_with?(expected) }
  end

  def test_alt_c_command
    set_var 'FZF_ALT_C_COMMAND', 'echo /tmp'

    tmux.prepare
    tmux.send_keys 'cd /', :Enter

    retries do
      tmux.prepare
      tmux.send_keys :Escape, :c
      tmux.until { |lines| lines.item_count == 1 }
    end
    tmux.send_keys :Enter

    tmux.prepare
    tmux.send_keys :pwd, :Enter
    tmux.until { |lines| lines[-1].end_with? '/tmp' }
  end

  def test_ctrl_r
    tmux.prepare
    tmux.send_keys 'echo 1st', :Enter; tmux.prepare
    tmux.send_keys 'echo 2nd', :Enter; tmux.prepare
    tmux.send_keys 'echo 3d',  :Enter; tmux.prepare
    tmux.send_keys 'echo 3rd', :Enter; tmux.prepare
    tmux.send_keys 'echo 4th', :Enter
    retries do
      tmux.prepare
      tmux.send_keys 'C-r'
      tmux.until { |lines| lines.match_count.positive? }
    end
    tmux.send_keys 'C-r'
    tmux.send_keys '3d'
    tmux.until { |lines| lines[-3].end_with? 'echo 3rd' }
    tmux.send_keys :Enter
    tmux.until { |lines| lines[-1] == 'echo 3rd' }
    tmux.send_keys :Enter
    tmux.until { |lines| lines[-1] == '3rd' }
  end

  def test_ctrl_r_abort
    skip "doesn't restore the original line when search is aborted pre Bash 4" if shell == :bash && /(?<= version )\d+/.match(`#{Shell.bash} --version`).to_s.to_i < 4
    tmux.send_keys 'foo'
    tmux.until { |lines| lines[-1].start_with? 'foo' }
    tmux.send_keys 'C-r'