Debian Codegraph @

test/test_shell_integration.rb

raw
1 # frozen_string_literal: true
2
3 require_relative 'lib/common'
4
5 # Testing shell integration
6 module TestShell
7 attr_reader :tmux
8
9 def setup
10 @tmux = Tmux.new(shell)
11 tmux.prepare
12 end
13
14 def teardown
15 @tmux.kill
16 end
17
18 def set_var(name, val)
19 tmux.prepare
20 tmux.send_keys "export #{name}='#{val}'", :Enter
21 tmux.prepare
22 end
23
24 def unset_var(name)
25 tmux.prepare
26 tmux.send_keys "unset #{name}", :Enter
27 tmux.prepare
28 end
29
30 def trigger
31 '**'
32 end
33
34 def test_ctrl_t
35 set_var('FZF_CTRL_T_COMMAND', 'seq 100')
36
37 tmux.prepare
38 tmux.send_keys 'C-t'
39 tmux.until { |lines| assert_equal 100, lines.match_count }
40 tmux.send_keys :Tab, :Tab, :Tab
41 tmux.until { |lines| assert lines.any_include?(' (3)') }
42 tmux.send_keys :Enter
43 tmux.until { |lines| assert lines.any_include?('1 2 3') }
44 tmux.send_keys 'C-c'
45 end
46
47 def test_ctrl_t_unicode
48 writelines(['fzf-unicode 테스트1', 'fzf-unicode 테스트2'])
49 set_var('FZF_CTRL_T_COMMAND', "cat #{tempname}")
50
51 tmux.prepare
52 tmux.send_keys 'echo ', 'C-t'
53 tmux.until { |lines| assert_equal 2, lines.match_count }
54 tmux.send_keys 'fzf-unicode'
55 tmux.until { |lines| assert_equal 2, lines.match_count }
56
57 tmux.send_keys '1'
58 tmux.until { |lines| assert_equal 1, lines.match_count }
59 tmux.send_keys :Tab
60 tmux.until { |lines| assert_equal 1, lines.select_count }
61
62 tmux.send_keys :BSpace
63 tmux.until { |lines| assert_equal 2, lines.match_count }
64
65 tmux.send_keys '2'
66 tmux.until { |lines| assert_equal 1, lines.match_count }
67 tmux.send_keys :Tab
68 tmux.until { |lines| assert_equal 2, lines.select_count }
69
70 tmux.send_keys :Enter
71 tmux.until { |lines| assert_match(/echo .*fzf-unicode.*1.* .*fzf-unicode.*2/, lines.join) }
72 tmux.send_keys :Enter
73 tmux.until { |lines| assert_equal 'fzf-unicode 테스트1 fzf-unicode 테스트2', lines[-1] }
74 end
75
76 def test_alt_c
77 tmux.prepare
78 tmux.send_keys :Escape, :c
79 lines = tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
80 expected = lines.reverse.find { |l| l.start_with?('> ') }[2..].chomp('/')
81 tmux.send_keys :Enter
82 tmux.prepare
83 tmux.send_keys :pwd, :Enter
84 tmux.until { |lines| assert lines[-1]&.end_with?(expected) }
85 end
86
87 def test_alt_c_command
88 set_var('FZF_ALT_C_COMMAND', 'echo /tmp')
89
90 tmux.prepare
91 tmux.send_keys 'cd /', :Enter
92
93 tmux.prepare
94 tmux.send_keys :Escape, :c
95 tmux.until { |lines| assert_equal 1, lines.match_count }
96 tmux.send_keys :Enter
97
98 tmux.prepare
99 tmux.send_keys :pwd, :Enter
100 tmux.until { |lines| assert_equal '/tmp', lines[-1] }
101 end
102
103 def test_alt_c_symlink
104 base = '/tmp/fzf-test-alt-c-symlink'
105 FileUtils.rm_rf(base)
106 FileUtils.mkdir_p("#{base}/real/subdir")
107 FileUtils.ln_s("#{base}/real", "#{base}/link")
108
109 tmux.prepare
110 tmux.send_keys "cd #{base}/link", :Enter
111 tmux.prepare
112 tmux.send_keys :Escape, :c
113 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
114 tmux.send_keys 'subdir'
115 tmux.until { |lines| assert_equal 1, lines.match_count }
116 tmux.send_keys :Enter
117 tmux.prepare
118 tmux.send_keys :pwd, :Enter
119 tmux.until { |lines| assert_equal "#{base}/link/subdir", lines[-1] }
120 ensure
121 FileUtils.rm_rf(base)
122 end
123
124 def test_alt_c_absolute_cmd
125 base = '/tmp/fzf-test-alt-c-absolute'
126 FileUtils.rm_rf(base)
127 FileUtils.mkdir_p(base)
128
129 set_var('FZF_ALT_C_COMMAND', "echo #{base}")
130
131 tmux.prepare
132 tmux.send_keys 'cd /tmp', :Enter
133 tmux.prepare
134 tmux.send_keys :Escape, :c
135 tmux.until { |lines| assert_equal 1, lines.match_count }
136 tmux.send_keys :Enter
137 tmux.prepare
138 tmux.send_keys :pwd, :Enter
139 tmux.until { |lines| assert_equal base, lines[-1] }
140 ensure
141 FileUtils.rm_rf(base)
142 end
143
144 def test_ctrl_r
145 tmux.prepare
146 tmux.send_keys 'echo 1st', :Enter
147 tmux.prepare
148 tmux.send_keys 'echo 2nd', :Enter
149 tmux.prepare
150 tmux.send_keys 'echo 3d', :Enter
151 tmux.prepare
152 3.times do
153 tmux.send_keys 'echo 3rd', :Enter
154 tmux.prepare
155 end
156 tmux.send_keys 'echo 4th', :Enter
157 tmux.prepare
158 tmux.send_keys 'C-r'
159 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
160 tmux.send_keys 'e3d'
161 # Duplicates removed: 3d (1) + 3rd (1) => 2 matches
162 tmux.until { |lines| assert_equal 2, lines.match_count }
163 tmux.until { |lines| assert lines[-3]&.end_with?(' echo 3d') }
164 tmux.send_keys 'C-r'
165 tmux.until { |lines| assert lines[-3]&.end_with?(' echo 3rd') }
166 tmux.send_keys :Enter
167 tmux.until { |lines| assert_equal 'echo 3rd', lines[-1] }
168 tmux.send_keys :Enter
169 tmux.until { |lines| assert_equal '3rd', lines[-1] }
170 end
171
172 def test_ctrl_r_multiline
173 # NOTE: Current bash implementation shows an extra new line if there's
174 # only entry in the history
175 tmux.send_keys ':', :Enter
176 tmux.send_keys 'echo "foo', :Enter, 'bar"', :Enter
177 tmux.until { |lines| assert_equal %w[foo bar], lines[-2..] }
178 tmux.prepare
179 tmux.send_keys 'C-r'
180 tmux.until { |lines| assert_equal '>', lines[-1] }
181 tmux.send_keys 'foo bar'
182 tmux.until { |lines| assert_includes lines[-4], '"foo' } if shell == :bash
183 tmux.until { |lines| assert lines[-3]&.match?(/bar"␊?/) }
184 tmux.send_keys :Enter
185 tmux.until { |lines| assert lines[-1]&.match?(/bar"␊?/) }
186 tmux.send_keys :Enter
187 tmux.until { |lines| assert_equal %w[foo bar], lines[-2..] }
188 end
189
190 def test_ctrl_r_abort
191 skip("doesn't restore the original line when search is aborted pre Bash 4") if shell == :bash && `#{Shell.bash} --version`[/(?<= version )\d+/].to_i < 4
192 %w[foo ' "].each do |query|
193 tmux.prepare
194 tmux.send_keys :Enter, query
195 tmux.until { |lines| assert lines[-1]&.start_with?(query) }
196 tmux.send_keys 'C-r'
197 tmux.until { |lines| assert_equal "> #{query}", lines[-1] }
198 tmux.send_keys 'C-g'
199 tmux.until { |lines| assert lines[-1]&.start_with?(query) }
200 end
201 end
202 end
203
204 module CompletionTest
205 def test_file_completion
206 FileUtils.mkdir_p('/tmp/fzf-test')
207 FileUtils.mkdir_p('/tmp/fzf test')
208 (1..100).each { |i| FileUtils.touch("/tmp/fzf-test/#{i}") }
209 ['no~such~user', '/tmp/fzf test/foobar'].each do |f|
210 FileUtils.touch(File.expand_path(f))
211 end
212 tmux.prepare
213 if shell == :fish
214 tmux.send_keys 'cat /tmp/fzf-test/10', 'C-t'
215 else
216 tmux.send_keys "cat /tmp/fzf-test/10#{trigger}", :Tab
217 end
218 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
219 tmux.send_keys ' !d'
220 tmux.until { |lines| assert_equal 2, lines.match_count }
221 tmux.send_keys :Tab, :Tab
222 tmux.until { |lines| assert_equal 2, lines.select_count }
223 tmux.send_keys :Enter
224 tmux.until(true) do |lines|
225 assert_equal 'cat /tmp/fzf-test/10 /tmp/fzf-test/100', lines[-1]
226 end
227
228 # ~USERNAME**<TAB>
229 user = `whoami`.chomp
230 tmux.send_keys 'C-u'
231 if shell == :fish
232 tmux.send_keys "cat ~#{user}", 'C-t'
233 else
234 tmux.send_keys "cat ~#{user}#{trigger}", :Tab
235 end
236 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
237 tmux.send_keys "/#{user}"
238 tmux.until { |lines| assert(lines.any? { |l| l.end_with?("/#{user}") }) }
239 tmux.send_keys :Enter
240 tmux.until(true) do |lines|
241 assert_match %r{cat .*/#{user}}, lines[-1]
242 end
243
244 # ~INVALID_USERNAME**<TAB>
245 tmux.send_keys 'C-u'
246 if shell == :fish
247 tmux.send_keys 'cat ~such', 'C-t'
248 else
249 tmux.send_keys "cat ~such#{trigger}", :Tab
250 end
251 tmux.until(true) { |lines| assert lines.any_include?('no~such~user') }
252 tmux.send_keys :Enter
253 tmux.until(true) do |lines|
254 if shell == :fish
255 # Fish's string escape quotes filenames with ~ to prevent tilde expansion
256 assert_equal 'cat no\\~such\\~user', lines[-1]
257 else
258 assert_equal 'cat no~such~user', lines[-1]
259 end
260 end
261
262 # /tmp/fzf\ test**<TAB>
263 tmux.send_keys 'C-u'
264 if shell == :fish
265 tmux.send_keys 'cat /tmp/fzf\\ test/', 'C-t'
266 else
267 tmux.send_keys "cat /tmp/fzf\\ test/#{trigger}", :Tab
268 end
269 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
270 tmux.send_keys 'foobar$'
271 tmux.until do |lines|
272 assert_equal 1, lines.match_count
273 assert lines.any_include?('> /tmp/fzf test/foobar')
274 end
275 tmux.send_keys :Enter
276 tmux.until(true) { |lines| assert_equal 'cat /tmp/fzf\ test/foobar', lines[-1] }
277
278 # Should include hidden files
279 (1..100).each { |i| FileUtils.touch("/tmp/fzf-test/.hidden-#{i}") }
280 tmux.send_keys 'C-u'
281 if shell == :fish
282 tmux.send_keys 'cat /tmp/fzf-test/hidden', 'C-t'
283 else
284 tmux.send_keys "cat /tmp/fzf-test/hidden#{trigger}", :Tab
285 end
286 tmux.until(true) do |lines|
287 assert_equal 100, lines.match_count
288 assert lines.any_include?('/tmp/fzf-test/.hidden-')
289 end
290 tmux.send_keys :Enter
291 ensure
292 ['/tmp/fzf-test', '/tmp/fzf test', '~/.fzf-home', 'no~such~user'].each do |f|
293 FileUtils.rm_rf(File.expand_path(f))
294 end
295 end
296
297 def test_file_completion_root
298 if shell == :fish
299 tmux.send_keys 'ls /', 'C-t'
300 else
301 tmux.send_keys "ls /#{trigger}", :Tab
302 end
303 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
304 tmux.send_keys :Enter
305 end
306
307 def test_dir_completion
308 FileUtils.mkdir_p('/tmp/fzf-test-dir')
309 (1..100).each do |idx|
310 FileUtils.mkdir_p("/tmp/fzf-test-dir/d#{idx}")
311 end
312 FileUtils.touch('/tmp/fzf-test-dir/d55/xxx')
313 tmux.prepare
314 if shell == :fish
315 tmux.send_keys 'cd /tmp/fzf-test-dir/', 'C-t'
316 else
317 tmux.send_keys "cd /tmp/fzf-test-dir/#{trigger}", :Tab
318 end
319 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
320 # Tab selects items in C-t's --multi mode, so skip for fish
321 tmux.send_keys :Tab, :Tab unless shell == :fish # Tab does not work here
322 tmux.send_keys '55/$'
323 tmux.until do |lines|
324 assert_equal 1, lines.match_count
325 assert_includes lines, '> 55/$'
326 assert_includes lines, '> /tmp/fzf-test-dir/d55/'
327 end
328 tmux.send_keys :Enter
329 tmux.until(true) { |lines| assert_equal 'cd /tmp/fzf-test-dir/d55/', lines[-1] }
330 # C-t appends a trailing space after the result
331 tmux.send_keys :BSpace if shell == :fish
332 tmux.send_keys :xx
333 tmux.until { |lines| assert_equal 'cd /tmp/fzf-test-dir/d55/xx', lines[-1] }
334
335 # Should not match regular files (bash-only)
336 if instance_of?(TestBash)
337 tmux.send_keys :Tab
338 tmux.until { |lines| assert_equal 'cd /tmp/fzf-test-dir/d55/xx', lines[-1] }
339 end
340
341 # Fail back to plusdirs
342 tmux.send_keys :BSpace, :BSpace, :BSpace
343 tmux.until { |lines| assert_equal 'cd /tmp/fzf-test-dir/d55', lines[-1] }
344 tmux.send_keys :Tab
345 tmux.until { |lines| assert_equal 'cd /tmp/fzf-test-dir/d55/', lines[-1] }
346 ensure
347 FileUtils.rm_rf('/tmp/fzf-test-dir')
348 end
349
350 def test_process_completion
351 skip('fish background job format differs') if shell == :fish
352
353 begin
354 tmux.send_keys 'sleep 12345 &', :Enter
355 lines = tmux.until { |lines| assert lines[-1]&.start_with?('[1] ') }
356 pid = lines[-1]&.split&.last
357 tmux.prepare
358 tmux.send_keys 'C-L'
359 tmux.send_keys "kill #{trigger}", :Tab
360 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
361 tmux.send_keys 'sleep12345'
362 tmux.until { |lines| assert lines.any_include?('sleep 12345') }
363 tmux.send_keys :Enter
364 tmux.until(true) { |lines| assert_equal "kill #{pid}", lines[-1] }
365 ensure
366 if pid
367 begin
368 Process.kill('KILL', pid.to_i)
369 rescue StandardError
370 nil
371 end
372 end
373 end
374 end
375
376 def test_custom_completion
377 skip('fish does not use _fzf_compgen_path; path completion is via ctrl-t') if shell == :fish
378 tmux.send_keys '_fzf_compgen_path() { echo "$1"; seq 10; }', :Enter
379 tmux.prepare
380 tmux.send_keys "ls /tmp/#{trigger}", :Tab
381 tmux.until { |lines| assert_equal 11, lines.match_count }
382 tmux.send_keys :Tab, :Tab, :Tab
383 tmux.until { |lines| assert_equal 3, lines.select_count }
384 tmux.send_keys :Enter
385 tmux.until(true) { |lines| assert_equal 'ls /tmp 1 2', lines[-1] }
386 end
387
388 def test_unset_completion
389 skip('fish has native completion for set and unset variables') if shell == :fish
390 tmux.send_keys 'export FZFFOOBAR=BAZ', :Enter
391 tmux.prepare
392
393 # Using tmux
394 tmux.send_keys "unset FZFFOOBR#{trigger}", :Tab
395 tmux.until { |lines| assert_equal 1, lines.match_count }
396 tmux.send_keys :Enter
397 tmux.until { |lines| assert_equal 'unset FZFFOOBAR', lines[-1] }
398 tmux.send_keys 'C-c'
399
400 # FZF_TMUX=1
401 new_shell
402 tmux.focus
403 tmux.send_keys "unset FZFFOOBR#{trigger}", :Tab
404 tmux.until { |lines| assert_equal 1, lines.match_count }
405 tmux.send_keys :Enter
406 tmux.until { |lines| assert_equal 'unset FZFFOOBAR', lines[-1] }
407 end
408
409 def test_completion_in_command_sequence
410 if shell == :fish
411 FileUtils.mkdir_p('/tmp/fzf-test-seq')
412 FileUtils.touch('/tmp/fzf-test-seq/fzffoobar')
413 tmux.prepare
414 # Fish uses Shift-Tab for fzf completion (no trigger system)
415 command = 'echo foo; QUX=THUD ls /tmp/fzf-test-seq/fzffoobr'
416 expected = 'echo foo; QUX=THUD ls /tmp/fzf-test-seq/fzffoobar'
417 tmux.send_keys command, :BTab
418 tmux.until { |lines| assert_equal 1, lines.match_count }
419 tmux.send_keys :Enter
420 tmux.until { |lines| assert_equal expected, lines[-1] }
421 else
422 tmux.send_keys 'export FZFFOOBAR=BAZ', :Enter
423 tmux.prepare
424
425 triggers = ['**', '~~', '++', 'ff', '/']
426 triggers.push('&', '[', ';', '`') if instance_of?(TestZsh)
427
428 triggers.each do |trigger|
429 set_var('FZF_COMPLETION_TRIGGER', trigger)
430 command = "echo foo; QUX=THUD unset FZFFOOBR#{trigger}"
431 expected = 'echo foo; QUX=THUD unset FZFFOOBAR'
432 tmux.send_keys command.sub(/(;|`)$/, '\\\\\1'), :Tab
433 tmux.until { |lines| assert_equal 1, lines.match_count }
434 tmux.send_keys :Enter
435 tmux.until { |lines| assert_equal expected, lines[-1] }
436 end
437 end
438 ensure
439 FileUtils.rm_rf('/tmp/fzf-test-seq') if shell == :fish
440 end
441
442 def test_file_completion_unicode
443 FileUtils.mkdir_p('/tmp/fzf-test')
444 # Shell-agnostic file creation
445 File.write('/tmp/fzf-test/fzf-unicode 테스트1', "test3\n")
446 File.write('/tmp/fzf-test/fzf-unicode 테스트2', "test4\n")
447 tmux.send_keys 'cd /tmp/fzf-test', :Enter
448 tmux.prepare
449 if shell == :fish
450 tmux.send_keys 'cat fzf-unicode', 'C-t'
451 else
452 tmux.send_keys "cat fzf-unicode#{trigger}", :Tab
453 end
454 tmux.until { |lines| assert_equal 2, lines.match_count }
455
456 tmux.send_keys '1'
457 tmux.until { |lines| assert_equal 1, lines.match_count }
458 tmux.send_keys :Tab
459 tmux.until { |lines| assert_equal 1, lines.select_count }
460
461 tmux.send_keys :BSpace
462 tmux.until { |lines| assert_equal 2, lines.match_count }
463
464 tmux.send_keys '2'
465 tmux.until { |lines| assert_equal 1, lines.select_count }
466 tmux.send_keys :Tab
467 tmux.until { |lines| assert_equal 2, lines.select_count }
468
469 tmux.send_keys :Enter
470 tmux.until(true) { |lines| assert_match(/cat .*fzf-unicode.*1.* .*fzf-unicode.*2/, lines[-1]) }
471 tmux.send_keys :Enter
472 tmux.until { |lines| assert_equal %w[test3 test4], lines[-2..] }
473 end
474
475 def test_custom_completion_api
476 skip('bash-specific _comprun/declare syntax') if shell == :fish
477
478 begin
479 tmux.send_keys 'eval "_fzf$(declare -f _comprun)"', :Enter
480 %w[f g].each do |command|
481 tmux.prepare
482 tmux.send_keys "#{command} b#{trigger}", :Tab
483 tmux.until do |lines|
484 assert_equal 2, lines.item_count
485 assert_equal 1, lines.match_count
486 assert lines.any_include?("prompt-#{command}")
487 assert lines.any_include?("preview-#{command}-bar")
488 end
489 tmux.send_keys :Enter
490 tmux.until { |lines| assert_equal "#{command} #{command}barbar", lines[-1] }
491 tmux.send_keys 'C-u'
492 end
493 ensure
494 tmux.prepare
495 tmux.send_keys 'unset -f _fzf_comprun', :Enter
496 end
497 end
498
499 def test_ssh_completion
500 skip('fish uses native ssh completion') if shell == :fish
501 (1..5).each { |i| FileUtils.touch("/tmp/fzf-test-ssh-#{i}") }
502
503 tmux.send_keys "ssh jg@localhost#{trigger}", :Tab
504 tmux.until do |lines|
505 assert_operator lines.match_count, :>=, 1
506 end
507
508 tmux.send_keys :Enter
509 tmux.until { |lines| assert lines.any_include?('ssh jg@localhost') }
510 tmux.send_keys " -i /tmp/fzf-test-ssh#{trigger}", :Tab
511 tmux.until do |lines|
512 assert_operator lines.match_count, :>=, 5
513 assert_equal 0, lines.select_count
514 end
515 tmux.send_keys :Tab, :Tab, :Tab
516 tmux.until do |lines|
517 assert_equal 3, lines.select_count
518 end
519 tmux.send_keys :Enter
520 tmux.until { |lines| assert lines.any_include?('ssh jg@localhost -i /tmp/fzf-test-ssh-') }
521
522 tmux.send_keys "localhost#{trigger}", :Tab
523 tmux.until do |lines|
524 assert_operator lines.match_count, :>=, 1
525 end
526 end
527
528 def test_option_equals_long_option
529 FileUtils.mkdir_p('/tmp/fzf-test-opt-eq-long')
530 FileUtils.touch('/tmp/fzf-test-opt-eq-long/SECURITY.md')
531 tmux.prepare
532 tmux.send_keys 'cd /tmp/fzf-test-opt-eq-long', :Enter
533 tmux.prepare
534 if shell == :fish
535 tmux.send_keys 'some-command --opt=SECURI', 'C-t'
536 else
537 tmux.send_keys "some-command --opt=SECURI#{trigger}", :Tab
538 end
539
540 case shell
541 when :bash
542 tmux.until do |lines|
543 assert_equal 1, lines.match_count
544 assert_includes lines, '> SECURI'
545 end
546 tmux.send_keys :Enter
547 tmux.until(true) { |lines| assert_equal 'some-command --opt=SECURITY.md', lines[-1] }
548 when :fish
549 tmux.until do |lines|
550 assert_equal 1, lines.match_count
551 assert lines.any_include?('SECURITY.md')
552 end
553 tmux.send_keys :Enter
554 tmux.until(true) { |lines| assert_equal 'some-command --opt=SECURITY.md', lines[-1] }
555 when :zsh
556 tmux.until do |lines|
557 assert_equal 0, lines.match_count
558 assert_includes lines, '> --opt=SECURI'
559 end
560 end
561 ensure
562 FileUtils.rm_rf('/tmp/fzf-test-opt-eq-long')
563 end
564
565 def test_option_equals_long_option_after_double_dash
566 FileUtils.mkdir_p('/tmp/fzf-test-opt-eq-long-ddash')
567 FileUtils.touch('/tmp/fzf-test-opt-eq-long-ddash/SECURITY.md')
568 tmux.prepare
569 tmux.send_keys 'cd /tmp/fzf-test-opt-eq-long-ddash', :Enter
570 tmux.prepare
571 if shell == :fish
572 tmux.send_keys 'some-command -- --opt=SECURI', 'C-t'
573 else
574 tmux.send_keys "some-command -- --opt=SECURI#{trigger}", :Tab
575 end
576
577 case shell
578 when :bash
579 tmux.until do |lines|
580 assert_equal 1, lines.match_count
581 assert_includes lines, '> SECURI'
582 end
583 tmux.send_keys :Enter
584 tmux.until(true) { |lines| assert_equal 'some-command -- --opt=SECURITY.md', lines[-1] }
585 when :fish, :zsh
586 tmux.until do |lines|
587 assert_equal 0, lines.match_count
588 assert_includes lines, '> --opt=SECURI'
589 end
590 end
591 ensure
592 FileUtils.rm_rf('/tmp/fzf-test-opt-eq-long-ddash')
593 end
594
595 def test_option_equals_short_option
596 FileUtils.mkdir_p('/tmp/fzf-test-opt-eq-short')
597 FileUtils.touch('/tmp/fzf-test-opt-eq-short/SECURITY.md')
598 tmux.prepare
599 tmux.send_keys 'cd /tmp/fzf-test-opt-eq-short', :Enter
600 tmux.prepare
601 if shell == :fish
602 tmux.send_keys 'some-command -o=SECURI', 'C-t'
603 else
604 tmux.send_keys "some-command -o=SECURI#{trigger}", :Tab
605 end
606
607 case shell
608 when :bash, :fish
609 tmux.until do |lines|
610 assert_equal 1, lines.match_count
611 assert lines.any_include?('> SECURITY.md')
612 end
613 tmux.send_keys :Enter
614 tmux.until(true) { |lines| assert_equal 'some-command -o=SECURITY.md', lines[-1] }
615 when :zsh
616 tmux.until do |lines|
617 assert_equal 0, lines.match_count
618 assert_includes lines, '> -o=SECURI'
619 end
620 end
621 ensure
622 FileUtils.rm_rf('/tmp/fzf-test-opt-eq-short')
623 end
624
625 def test_option_equals_short_option_after_double_dash
626 FileUtils.mkdir_p('/tmp/fzf-test-opt-eq-short-ddash')
627 FileUtils.touch('/tmp/fzf-test-opt-eq-short-ddash/SECURITY.md')
628 tmux.prepare
629 tmux.send_keys 'cd /tmp/fzf-test-opt-eq-short-ddash', :Enter
630 tmux.prepare
631 if shell == :fish
632 tmux.send_keys 'some-command -- -o=SECURI', 'C-t'
633 else
634 tmux.send_keys "some-command -- -o=SECURI#{trigger}", :Tab
635 end
636
637 case shell
638 when :bash
639 tmux.until do |lines|
640 assert_equal 1, lines.match_count
641 assert_includes lines, '> SECURITY.md'
642 end
643 tmux.send_keys :Enter
644 tmux.until(true) { |lines| assert_equal 'some-command -- -o=SECURITY.md', lines[-1] }
645 when :fish, :zsh
646 tmux.until do |lines|
647 assert_equal 0, lines.match_count
648 assert_includes lines, '> -o=SECURI'
649 end
650 end
651 ensure
652 FileUtils.rm_rf('/tmp/fzf-test-opt-eq-short-ddash')
653 end
654
655 def test_option_no_equals_long_option
656 FileUtils.mkdir_p('/tmp/fzf-test-opt-no-eq-long')
657 FileUtils.touch('/tmp/fzf-test-opt-no-eq-long/SECURITY.md')
658 tmux.prepare
659 tmux.send_keys 'cd /tmp/fzf-test-opt-no-eq-long', :Enter
660 tmux.prepare
661 if shell == :fish
662 tmux.send_keys 'some-command --optSECURI', 'C-t'
663 else
664 tmux.send_keys "some-command --optSECURI#{trigger}", :Tab
665 end
666
667 tmux.until do |lines|
668 assert_equal 0, lines.match_count
669 assert_includes lines, '> --optSECURI'
670 end
671 ensure
672 FileUtils.rm_rf('/tmp/fzf-test-opt-no-eq-long')
673 end
674
675 def test_option_no_equals_long_option_after_double_dash
676 FileUtils.mkdir_p('/tmp/fzf-test-opt-no-eq-long-ddash')
677 FileUtils.touch('/tmp/fzf-test-opt-no-eq-long-ddash/SECURITY.md')
678 tmux.prepare
679 tmux.send_keys 'cd /tmp/fzf-test-opt-no-eq-long-ddash', :Enter
680 tmux.prepare
681 if shell == :fish
682 tmux.send_keys 'some-command -- --optSECURI', 'C-t'
683 else
684 tmux.send_keys "some-command -- --optSECURI#{trigger}", :Tab
685 end
686
687 tmux.until do |lines|
688 assert_equal 0, lines.match_count
689 assert_includes lines, '> --optSECURI'
690 end
691 ensure
692 FileUtils.rm_rf('/tmp/fzf-test-opt-no-eq-long-ddash')
693 end
694
695 def test_option_no_equals_short_option
696 FileUtils.mkdir_p('/tmp/fzf-test-opt-no-eq-short')
697 FileUtils.touch('/tmp/fzf-test-opt-no-eq-short/SECURITY.md')
698 tmux.prepare
699 tmux.send_keys 'cd /tmp/fzf-test-opt-no-eq-short', :Enter
700 tmux.prepare
701 if shell == :fish
702 tmux.send_keys 'some-command -oSECURI', 'C-t'
703 else
704 tmux.send_keys "some-command -oSECURI#{trigger}", :Tab
705 end
706
707 case shell
708 when :bash, :zsh
709 tmux.until do |lines|
710 assert_equal 0, lines.match_count
711 assert_includes lines, '> -oSECURI'
712 end
713 when :fish
714 tmux.until do |lines|
715 assert_equal 1, lines.match_count
716 assert lines.any_include?('> SECURITY.md')
717 end
718 tmux.send_keys :Enter
719 tmux.until(true) { |lines| assert_equal 'some-command -oSECURITY.md', lines[-1] }
720 end
721 ensure
722 FileUtils.rm_rf('/tmp/fzf-test-opt-no-eq-short')
723 end
724
725 def test_option_no_equals_short_option_after_double_dash
726 FileUtils.mkdir_p('/tmp/fzf-test-opt-no-eq-short-ddash')
727 FileUtils.touch('/tmp/fzf-test-opt-no-eq-short-ddash/SECURITY.md')
728 tmux.prepare
729 tmux.send_keys 'cd /tmp/fzf-test-opt-no-eq-short-ddash', :Enter
730 tmux.prepare
731 if shell == :fish
732 tmux.send_keys 'some-command -- -oSECURI', 'C-t'
733 else
734 tmux.send_keys "some-command -- -oSECURI#{trigger}", :Tab
735 end
736
737 tmux.until do |lines|
738 assert_equal 0, lines.match_count
739 assert_includes lines, '> -oSECURI'
740 end
741 ensure
742 FileUtils.rm_rf('/tmp/fzf-test-opt-no-eq-short-ddash')
743 end
744
745 def test_filename_with_newline
746 FileUtils.mkdir_p('/tmp/fzf-test-newline')
747 FileUtils.touch("/tmp/fzf-test-newline/xyz\nwith\nnewlines")
748 tmux.prepare
749 tmux.send_keys 'cd /tmp/fzf-test-newline', :Enter
750 tmux.prepare
751 if shell == :fish
752 tmux.send_keys 'cat xyz', 'C-t'
753 else
754 tmux.send_keys "cat xyz#{trigger}", :Tab
755 end
756
757 case shell
758 when :fish
759 tmux.until do |lines|
760 assert_equal 1, lines.match_count
761 assert_includes lines, '> xyz'
762 end
763 tmux.send_keys :Enter
764 # fish escapes newlines in filenames
765 tmux.until(true) { |lines| assert_equal 'cat xyz\\nwith\\nnewlines', lines[-1] }
766 when :bash, :zsh
767 tmux.until do |lines|
768 assert_equal 1, lines.match_count
769 assert_includes lines, '> xyz'
770 end
771 tmux.send_keys :Enter
772 # bash and zsh replace newlines with spaces in filenames
773 tmux.until(true) { |lines| assert_equal 'cat xyz with newlines', lines[-1] }
774 end
775 ensure
776 FileUtils.rm_rf('/tmp/fzf-test-newline')
777 end
778
779 def test_path_with_special_chars
780 FileUtils.mkdir_p('/tmp/fzf-test-[special]')
781 FileUtils.touch('/tmp/fzf-test-[special]/xyz123')
782 tmux.prepare
783 if shell == :fish
784 tmux.send_keys 'ls /tmp/fzf-test-\[special\]/xyz', 'C-t'
785 else
786 tmux.send_keys "ls /tmp/fzf-test-\\[special\\]/xyz#{trigger}", :Tab
787 end
788 tmux.until { |lines| assert_equal 1, lines.match_count }
789 tmux.send_keys :Enter
790 tmux.until(true) { |lines| assert_equal 'ls /tmp/fzf-test-\\[special\\]/xyz123', lines[-1] }
791 ensure
792 FileUtils.rm_rf('/tmp/fzf-test-[special]')
793 end
794
795 def test_query_with_dollar_anchor
796 FileUtils.mkdir_p('/tmp/fzf-test-dollar-anchor')
797 FileUtils.touch('/tmp/fzf-test-dollar-anchor/file.txt')
798 FileUtils.touch('/tmp/fzf-test-dollar-anchor/filetxt.md')
799 tmux.prepare
800 tmux.send_keys 'cd /tmp/fzf-test-dollar-anchor', :Enter
801 tmux.prepare
802 if shell == :fish
803 tmux.send_keys 'ls txt$', 'C-t'
804 else
805 tmux.send_keys "ls txt$#{trigger}", :Tab
806 end
807 tmux.until do |lines|
808 assert_equal 1, lines.match_count
809 assert_includes lines, '> txt$'
810 end
811 tmux.send_keys :Enter
812 tmux.until(true) { |lines| assert_equal 'ls file.txt', lines[-1] }
813 ensure
814 FileUtils.rm_rf('/tmp/fzf-test-dollar-anchor')
815 end
816
817 def test_single_flag_completion
818 FileUtils.mkdir_p('/tmp/fzf-test-single-flag')
819 FileUtils.touch('/tmp/fzf-test-single-flag/-testfile.txt')
820 tmux.prepare
821 tmux.send_keys 'cd /tmp/fzf-test-single-flag', :Enter
822 tmux.prepare
823 if shell == :fish
824 tmux.send_keys 'ls -', 'C-t'
825 else
826 tmux.send_keys "ls -#{trigger}", :Tab
827 end
828
829 tmux.until do |lines|
830 assert_equal 1, lines.match_count
831 assert_includes lines, '> -'
832 end
833 tmux.send_keys :Enter
834 tmux.until(true) { |lines| assert_equal 'ls -testfile.txt', lines[-1] }
835 ensure
836 FileUtils.rm_rf('/tmp/fzf-test-single-flag')
837 end
838
839 def test_double_flag_completion
840 FileUtils.mkdir_p('/tmp/fzf-test-double-flag')
841 FileUtils.touch('/tmp/fzf-test-double-flag/--testfile.txt')
842 tmux.prepare
843 tmux.send_keys 'cd /tmp/fzf-test-double-flag', :Enter
844 tmux.prepare
845 if shell == :fish
846 tmux.send_keys 'ls --', 'C-t'
847 else
848 tmux.send_keys "ls --#{trigger}", :Tab
849 end
850
851 tmux.until do |lines|
852 assert_equal 1, lines.match_count
853 assert_includes lines, '> --'
854 end
855 tmux.send_keys :Enter
856 tmux.until(true) { |lines| assert_equal 'ls --testfile.txt', lines[-1] }
857 ensure
858 FileUtils.rm_rf('/tmp/fzf-test-double-flag')
859 end
860 end
861
862 class TestBash < TestBase
863 include TestShell
864 include CompletionTest
865
866 def shell
867 :bash
868 end
869
870 def new_shell
871 tmux.prepare
872 tmux.send_keys "FZF_TMUX=1 #{Shell.bash}", :Enter
873 tmux.prepare
874 end
875
876 def test_ctrl_r_delete
877 tmux.prepare
878 tmux.send_keys 'echo to-keep', :Enter
879 tmux.prepare
880 tmux.send_keys 'echo to-delete-1', :Enter
881 tmux.prepare
882 tmux.send_keys 'echo to-delete-2', :Enter
883 tmux.prepare
884 tmux.send_keys 'echo to-delete-3', :Enter
885 tmux.prepare
886 tmux.send_keys 'echo another-keeper', :Enter
887 tmux.prepare
888
889 # Open Ctrl-R and delete one entry
890 tmux.send_keys 'C-r'
891 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
892 tmux.send_keys 'to-delete'
893 tmux.until { |lines| assert_equal 3, lines.match_count }
894 tmux.send_keys 'S-Delete'
895 tmux.until { |lines| assert_equal 2, lines.match_count }
896
897 # Multi-select remaining two and delete them at once
898 tmux.send_keys :BTab, :BTab
899 tmux.until { |lines| assert_includes lines[-2], '(2)' }
900 tmux.send_keys 'S-Delete'
901 tmux.until { |lines| assert_equal 0, lines.match_count }
902
903 # Exit without selecting
904 tmux.send_keys :Escape
905 tmux.prepare
906
907 # Verify deleted entries are gone from history
908 tmux.send_keys 'C-r'
909 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
910 tmux.send_keys 'to-delete'
911 tmux.until { |lines| assert_equal 0, lines.match_count }
912 tmux.send_keys :Escape
913 tmux.prepare
914
915 # Verify kept entries are still there
916 tmux.send_keys 'C-r'
917 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
918 tmux.send_keys 'to-keep'
919 tmux.until { |lines| assert_equal 1, lines.match_count }
920 tmux.send_keys :Enter
921 tmux.until { |lines| assert_equal 'echo to-keep', lines[-1] }
922 tmux.send_keys 'C-c'
923 end
924
925 def test_dynamic_completion_loader
926 tmux.paste 'touch /tmp/foo; _fzf_completion_loader=1'
927 tmux.paste '_completion_loader() { complete -o default fake; }'
928 tmux.paste 'complete -F _fzf_path_completion -o default -o bashdefault fake'
929 tmux.send_keys "fake /tmp/foo#{trigger}", :Tab
930 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
931 tmux.send_keys 'C-c'
932
933 tmux.prepare
934 tmux.send_keys 'fake /tmp/foo'
935 tmux.send_keys :Tab, 'C-u'
936
937 tmux.prepare
938 tmux.send_keys "fake /tmp/foo#{trigger}", :Tab
939 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
940 end
941 end
942
943 class TestZsh < TestBase
944 include TestShell
945 include CompletionTest
946
947 def shell
948 :zsh
949 end
950
951 def new_shell
952 tmux.send_keys "FZF_TMUX=1 #{Shell.zsh}", :Enter
953 tmux.prepare
954 end
955
956 def test_complete_quoted_command
957 tmux.send_keys 'export FZFFOOBAR=BAZ', :Enter
958 ['unset', '\unset', "'unset'"].each do |command|
959 tmux.prepare
960 tmux.send_keys "#{command} FZFFOOBR#{trigger}", :Tab
961 tmux.until { |lines| assert_equal 1, lines.match_count }
962 tmux.send_keys :Enter
963 tmux.until { |lines| assert_equal "#{command} FZFFOOBAR", lines[-1] }
964 tmux.send_keys 'C-c'
965 end
966 end
967
968 # Duplicate 'chpwd' calls overcount visits => skews rank tracking tools (e.g. 'zoxide')
969 def test_alt_c_chpwd_hook_once
970 tmux.send_keys "chpwd() { echo 'chpwd hook fired' >&2 }", :Enter
971 tmux.prepare
972 tmux.send_keys :Escape, :c
973 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
974 tmux.send_keys :Enter
975 tmux.until do |lines|
976 assert_equal(1, lines.count { |l| l.include?('chpwd hook fired') })
977 end
978 end
979
980 # Helper function to run test with Perl and again with Awk
981 def self.test_perl_and_awk(name, &block)
982 define_method(:"test_#{name}") do
983 instance_eval(&block)
984 end
985
986 define_method(:"test_#{name}_awk") do
987 tmux.send_keys "unset 'commands[perl]'", :Enter
988 tmux.prepare
989 # Verify perl is actually unset (0 = not found)
990 tmux.send_keys 'echo ${+commands[perl]}', :Enter
991 tmux.until { |lines| assert_equal '0', lines[-1] }
992 tmux.prepare
993 instance_eval(&block)
994 end
995 end
996
997 def prepare_ctrl_r_test
998 tmux.send_keys ':', :Enter
999 tmux.send_keys 'echo match-collision', :Enter
1000 tmux.prepare
1001 tmux.send_keys 'echo "line 1', :Enter, '2 line 2"', :Enter
1002 tmux.prepare
1003 tmux.send_keys 'echo "foo', :Enter, 'bar"', :Enter
1004 tmux.prepare
1005 tmux.send_keys 'echo "bar', :Enter, 'foo"', :Enter
1006 tmux.prepare
1007 tmux.send_keys 'echo "trailing_space "', :Enter
1008 tmux.prepare
1009 tmux.send_keys 'cat <<EOF | wc -c', :Enter, 'qux thud', :Enter, 'EOF', :Enter
1010 tmux.prepare
1011 tmux.send_keys 'C-l', 'C-r'
1012 end
1013
1014 test_perl_and_awk 'ctrl_r_exit_code' do
1015 exit_file = "#{tempname}-exit"
1016 # Wrapper captures status, as widgets return values don't propagate to "$?"
1017 # A non-zero status causes the shell to beep when the widget exits (man zshzle)
1018 tmux.send_keys %(f() { zle fzf-history-widget; echo $? > #{exit_file}; zle reset-prompt } && zle -N f && bindkey "^R" f), :Enter
1019 prepare_ctrl_r_test
1020 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
1021 tmux.send_keys 'C-g' # abort
1022 tmux.send_keys "cat #{exit_file}", :Enter
1023 tmux.until { |lines| assert_equal '130', lines[-1] }
1024 ensure
1025 FileUtils.rm_f(exit_file)
1026 end
1027
1028 test_perl_and_awk 'ctrl_r_accept_or_print_query' do
1029 set_var('FZF_CTRL_R_OPTS', '--bind enter:accept-or-print-query')
1030 prepare_ctrl_r_test
1031 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
1032 tmux.send_keys '1 foobar'
1033 tmux.until { |lines| assert_equal 0, lines.match_count }
1034 tmux.send_keys :Enter
1035 tmux.until { |lines| assert_equal '1 foobar', lines[-1] }
1036 end
1037
1038 test_perl_and_awk 'ctrl_r_multiline_index_collision' do
1039 tmux.send_keys 'setopt sh_glob', :Enter
1040 # Leading number in multi-line history content is not confused with index
1041 prepare_ctrl_r_test
1042 tmux.send_keys "'line 1"
1043 tmux.until { |lines| assert_equal 1, lines.match_count }
1044 tmux.send_keys :Enter
1045 tmux.until do |lines|
1046 assert_equal ['echo "line 1', '2 line 2"'], lines[-2..]
1047 end
1048 end
1049
1050 test_perl_and_awk 'ctrl_r_multi_selection' do
1051 prepare_ctrl_r_test
1052 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
1053 tmux.send_keys :BTab, :BTab, :BTab
1054 tmux.until { |lines| assert_includes lines[-2], '(3)' }
1055 tmux.send_keys :Enter
1056 tmux.until do |lines|
1057 assert_equal ['cat <<EOF | wc -c', 'qux thud', 'EOF', 'echo "trailing_space "', 'echo "bar', 'foo"'], lines[-6..]
1058 end
1059 end
1060
1061 test_perl_and_awk 'ctrl_r_no_multi_selection' do
1062 set_var('FZF_CTRL_R_OPTS', '--no-multi')
1063 prepare_ctrl_r_test
1064 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
1065 tmux.send_keys :BTab, :BTab, :BTab
1066 tmux.until { |lines| refute_includes lines[-2], '(3)' }
1067 tmux.send_keys :Enter
1068 tmux.until do |lines|
1069 assert_equal ['cat <<EOF | wc -c', 'qux thud', 'EOF'], lines[-3..]
1070 end
1071 end
1072
1073 # NOTE: 'Perl/$history' won't see foreign cmds immediately, unlike 'awk/fc'.
1074 # Perl passes only because another cmd runs between mocking and triggering C-r
1075 # https://github.com/junegunn/fzf/issues/4061
1076 # https://zsh.org/mla/users/2024/msg00692.html
1077 test_perl_and_awk 'ctrl_r_foreign_commands' do
1078 histfile = "#{tempname}-foreign-hist"
1079 tmux.send_keys "HISTFILE=#{histfile}", :Enter
1080 tmux.prepare
1081 # SHARE_HISTORY picks up foreign commands; marked with * in fc
1082 tmux.send_keys 'setopt SHARE_HISTORY', :Enter
1083 tmux.prepare
1084 tmux.send_keys 'fzf_cmd_local', :Enter
1085 tmux.prepare
1086 # Mock foreign command (for testing only; don't edit your HISTFILE this way)
1087 tmux.send_keys "echo ': 0:0;fzf_cmd_foreign' >> $HISTFILE", :Enter
1088 tmux.prepare
1089 # Verify fc shows foreign command with asterisk
1090 tmux.send_keys 'fc -rl -1', :Enter
1091 tmux.until { |lines| assert(lines.any? { |l| l.match?(/^\s*\d+\* fzf_cmd_foreign/) }) }
1092 tmux.prepare
1093 # Test ctrl-r correctly extracts the foreign command
1094 tmux.send_keys 'C-r'
1095 tmux.until { |lines| assert_operator lines.match_count, :>, 0 }
1096 tmux.send_keys '^fzf_cmd_'
1097 tmux.until { |lines| assert_equal 2, lines.match_count }
1098 tmux.send_keys :BTab, :BTab
1099 tmux.until { |lines| assert_includes lines[-2], '(2)' }
1100 tmux.send_keys :Enter
1101 tmux.until do |lines|
1102 assert_equal %w[fzf_cmd_foreign fzf_cmd_local], lines[-2..]
1103 end
1104 ensure
1105 FileUtils.rm_f(histfile)
1106 end
1107 end
1108
1109 class TestFish < TestBase
1110 include TestShell
1111 include CompletionTest
1112
1113 def shell
1114 :fish
1115 end
1116
1117 def trigger
1118 '++'
1119 end
1120
1121 def new_shell
1122 tmux.send_keys 'env FZF_TMUX=1 XDG_CONFIG_HOME=/tmp/fzf-fish fish', :Enter
1123 tmux.send_keys 'function fish_prompt; end; clear', :Enter
1124 tmux.until { |lines| assert_empty lines }
1125 end
1126
1127 def set_var(name, val)
1128 tmux.prepare
1129 tmux.send_keys "set -g #{name} '#{val}'", :Enter
1130 tmux.prepare
1131 end
1132
1133 def test_ctrl_r_multi
1134 tmux.send_keys ':', :Enter
1135 tmux.send_keys 'echo "foo', :Enter, 'bar"', :Enter
1136 tmux.prepare
1137 tmux.send_keys 'echo "bar', :Enter, 'foo"', :Enter
1138 tmux.prepare
1139 tmux.send_keys 'C-l', 'C-r'
1140 offset = -6
1141 block = <<~BLOCK
1142 echo "foo
1143 bar"
1144 echo "bar
1145 foo"
1146 BLOCK
1147 if shell == :fish
1148 offset = -4
1149 block = <<~FISH
1150 echo "foo␊bar"
1151 echo "bar␊foo"
1152 FISH
1153 end
1154 tmux.until do |lines|
1155 block.lines.each_with_index do |line, idx|
1156 assert_includes lines[idx + offset], line.chomp
1157 end
1158 end
1159 tmux.send_keys :BTab, :BTab
1160 tmux.until { |lines| assert_includes lines[-2], '(2)' }
1161 tmux.send_keys :Enter
1162 block = <<~BLOCK
1163 echo "bar
1164 foo"
1165 echo "foo
1166 bar"
1167 BLOCK
1168 tmux.until do |lines|
1169 assert_equal block.lines.map(&:chomp), lines
1170 end
1171 end
1172 end
1173
1174 class TestNushell < TestBase
1175 include TestShell
1176
1177 def teardown
1178 @tmux&.kill
1179 end
1180
1181 def shell
1182 :nushell
1183 end
1184
1185 def set_var(name, val)
1186 tmux.prepare
1187 tmux.send_keys "$env.#{name} = '#{val}'", :Enter
1188 tmux.prepare
1189 end
1190
1191 def unset_var(name)
1192 tmux.prepare
1193 tmux.send_keys "hide-env -i #{name}", :Enter
1194 tmux.prepare
1195 end
1196
1197 def new_shell
1198 tmux.send_keys 'FZF_TMUX=1 nu', :Enter
1199 tmux.prepare
1200 end
1201
1202 # Override: Nushell's builtin `echo` outputs structured data, so we need
1203 # `^echo` (external echo) for plain text output on the command line.
1204 def test_ctrl_t_unicode
1205 writelines(['fzf-unicode 테스트1', 'fzf-unicode 테스트2'])
1206 set_var('FZF_CTRL_T_COMMAND', "cat #{tempname}")
1207
1208 tmux.prepare
1209 tmux.send_keys '^echo ', 'C-t'
1210 tmux.until { |lines| assert_equal 2, lines.match_count }
1211 tmux.send_keys 'fzf-unicode'
1212 tmux.until { |lines| assert_equal 2, lines.match_count }
1213
1214 tmux.send_keys '1'
1215 tmux.until { |lines| assert_equal 1, lines.match_count }
1216 tmux.send_keys :Tab
1217 tmux.until { |lines| assert_equal 1, lines.select_count }
1218
1219 tmux.send_keys :BSpace
1220 tmux.until { |lines| assert_equal 2, lines.match_count }
1221
1222 tmux.send_keys '2'
1223 tmux.until { |lines| assert_equal 1, lines.match_count }
1224 tmux.send_keys :Tab
1225 tmux.until { |lines| assert_equal 2, lines.select_count }
1226
1227 tmux.send_keys :Enter
1228 tmux.until { |lines| assert_match(/\^echo .*fzf-unicode.*1.* .*fzf-unicode.*2/, lines.join) }
1229 tmux.send_keys :Enter
1230 tmux.until { |lines| assert_equal 'fzf-unicode 테스트1 fzf-unicode 테스트2', lines[-1] }
1231 end
1232
1233 # Override: Nushell's external completer replaces the entire token,
1234 # so we use assert_includes instead of assert_equal for the result.
1235 # ~USERNAME expansion and backslash-escaped spaces are not applicable.
1236 def test_file_completion
1237 FileUtils.mkdir_p('/tmp/fzf-test')
1238 (1..100).each { |i| FileUtils.touch("/tmp/fzf-test/#{i}") }
1239 tmux.prepare
1240
1241 # Multi-selection
1242 tmux.send_keys "cat /tmp/fzf-test/10#{trigger}", :Tab
1243 tmux.until { |lines| assert_equal 2, lines.match_count }
1244 tmux.send_keys :Tab, :Tab
1245 tmux.until { |lines| assert_equal 2, lines.select_count }
1246 tmux.send_keys :Enter
1247 tmux.until(true) do |lines|
1248 assert_includes lines[-1].to_s, '/tmp/fzf-test/10'
1249 assert_includes lines[-1].to_s, '/tmp/fzf-test/100'
1250 end
1251
1252 # Single selection
1253 tmux.prepare
1254 tmux.send_keys "cat /tmp/fzf-test/10#{trigger}", :Tab
1255 tmux.until { |lines| assert_equal 2, lines.match_count }
1256 tmux.send_keys '0'
1257 tmux.until { |lines| assert_equal 1, lines.match_count }
1258 tmux.send_keys :Enter
1259 tmux.until(true) do |lines|
1260 assert_includes lines[-1].to_s, '/tmp/fzf-test/100'
1261 end
1262
1263 # Should include hidden files
1264 (1..100).each { |i| FileUtils.touch("/tmp/fzf-test/.hidden-#{i}") }
1265 tmux.prepare
1266 tmux.send_keys "cat /tmp/fzf-test/hidden#{trigger}", :Tab
1267 tmux.until(true) do |lines|
1268 assert_equal 100, lines.match_count
1269 assert lines.any_include?('/tmp/fzf-test/.hidden-')
1270 end
1271 tmux.send_keys :Enter
1272 ensure
1273 FileUtils.rm_rf('/tmp/fzf-test')
1274 end
1275
1276 # Override: paths are inserted as Nushell string literals, so the
1277 # selections appear quoted on the command line.
1278 def test_ctrl_t
1279 set_var('FZF_CTRL_T_COMMAND', 'seq 100')
1280
1281 tmux.prepare
1282 tmux.send_keys 'C-t'
1283 tmux.until { |lines| assert_equal 100, lines.match_count }
1284 tmux.send_keys :Tab, :Tab, :Tab
1285 tmux.until { |lines| assert lines.any_include?(' (3)') }
1286 tmux.send_keys :Enter
1287 tmux.until { |lines| assert lines.any_include?('"1" "2" "3"') }
1288 tmux.send_keys 'C-c'
1289 end
1290
1291 # A path is inserted as a Nushell string literal, so that syntax in a file
1292 # name is not evaluated and each path stays a single argument.
1293 def test_ctrl_t_quoting
1294 marker = "#{tempname}-marker"
1295 FileUtils.rm_f(marker)
1296 writelines(["fzf-inject$(touch #{marker}).txt", 'fzf-inject space.txt'])
1297 set_var('FZF_CTRL_T_COMMAND', "cat #{tempname}")
1298
1299 tmux.prepare
1300 tmux.send_keys '^printf "%s\n" ', 'C-t'
1301 tmux.until { |lines| assert_equal 2, lines.match_count }
1302 tmux.send_keys :Tab, :Tab
1303 tmux.until { |lines| assert_equal 2, lines.select_count }
1304 tmux.send_keys :Enter
1305 tmux.until { |lines| assert_includes lines[-1].to_s, '"fzf-inject$(touch' }
1306 tmux.send_keys :Enter
1307 tmux.until do |lines|
1308 assert_equal ["fzf-inject$(touch #{marker}).txt", 'fzf-inject space.txt'], lines[-2..]
1309 end
1310 refute_path_exists marker
1311 end
1312
1313 # Nushell does not support multiline command recall the same way
1314 # as bash/zsh/fish, so test_ctrl_r_multiline is omitted.
1315
1316 # Override: only test with 'foo' -- single and double quotes cause
1317 # issues in Nushell's line editor.
1318 def test_ctrl_r_abort
1319 %w[foo].each do |query|
1320 tmux.prepare
1321 tmux.send_keys :Enter, query
1322 tmux.until { |lines| assert lines[-1]&.start_with?(query) }
1323 tmux.send_keys 'C-r'
1324 tmux.until { |lines| assert_equal "> #{query}", lines[-1] }
1325 tmux.send_keys 'C-g'
1326 tmux.until { |lines| assert lines[-1]&.start_with?(query) }
1327 end
1328 end
1329 end