-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathrepl.jl
1757 lines (1559 loc) · 62.2 KB
/
repl.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Test
using REPL
using Random
import REPL.LineEdit
using Markdown
empty!(Base.Experimental._hint_handlers) # unregister error hints so they can be tested separately
@test isassigned(Base.REPL_MODULE_REF)
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
isdefined(Main, :FakePTYs) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "FakePTYs.jl"))
import .Main.FakePTYs: with_fake_pty
# For curmod_*
include(joinpath(BASE_TEST_PATH, "testenv.jl"))
include("FakeTerminals.jl")
import .FakeTerminals.FakeTerminal
function kill_timer(delay)
# Give ourselves a generous timer here, just to prevent
# this causing e.g. a CI hang when there's something unexpected in the output.
# This is really messy and leaves the process in an undefined state.
# the proper and correct way to do this in real code would be to destroy the
# IO handles: `close(stdout_read); close(stdin_write)`
test_task = current_task()
function kill_test(t)
# **DON'T COPY ME.**
# The correct way to handle timeouts is to close the handle:
# e.g. `close(stdout_read); close(stdin_write)`
test_task.queue === nothing || Base.list_deletefirst!(test_task.queue, test_task)
schedule(test_task, "hard kill repl test"; error=true)
print(stderr, "WARNING: attempting hard kill of repl test after exceeding timeout\n")
end
return Timer(kill_test, delay)
end
## Debugging toys. Usage:
## stdout_read = tee_repr_stdout(stdout_read)
## ccall(:jl_breakpoint, Cvoid, (Any,), stdout_read)
#function tee(f, in::IO)
# copy = Base.BufferStream()
# t = @async try
# while !eof(in)
# l = readavailable(in)
# f(l)
# write(copy, l)
# end
# catch ex
# if !(ex isa Base.IOError && ex.code == Base.UV_EIO)
# rethrow() # ignore EIO on `in` stream
# end
# finally
# # TODO: could we call closewrite to propagate an error, instead of always doing a clean close here?
# closewrite(copy)
# end
# Base.errormonitor(t)
# return copy
#end
#tee(out::IO, in::IO) = tee(l -> write(out, l), in)
#tee_repr_stdout(io) = tee(io) do x
# print(repr(String(copy(x))) * "\n")
#end
# REPL tests
function fake_repl(@nospecialize(f); options::REPL.Options=REPL.Options(confirm_exit=false))
# Use pipes so we can easily do blocking reads
# In the future if we want we can add a test that the right object
# gets displayed by intercepting the display
input = Pipe()
output = Pipe()
err = Pipe()
Base.link_pipe!(input, reader_supports_async=true, writer_supports_async=true)
Base.link_pipe!(output, reader_supports_async=true, writer_supports_async=true)
Base.link_pipe!(err, reader_supports_async=true, writer_supports_async=true)
repl = REPL.LineEditREPL(FakeTerminal(input.out, output.in, err.in, options.hascolor), options.hascolor)
repl.options = options
hard_kill = kill_timer(900) # Your debugging session starts now. You have 15 minutes. Go.
f(input.in, output.out, repl)
t = @async begin
close(input.in)
close(output.in)
close(err.in)
end
@test read(err.out, String) == ""
#display(read(output.out, String))
Base.wait(t)
close(hard_kill)
nothing
end
# Writing ^C to the repl will cause sigint, so let's not die on that
Base.exit_on_sigint(false)
# make sure `run_interface` can normally handle `eof`
# without any special handling by the user
fake_repl() do stdin_write, stdout_read, repl
panel = LineEdit.Prompt("test";
prompt_prefix = "",
prompt_suffix = Base.text_colors[:white],
on_enter = s -> true)
panel.on_done = (s, buf, ok) -> begin
@test !ok
@test bytesavailable(buf) == position(buf) == 0
nothing
end
repltask = @async REPL.run_interface(repl.t, LineEdit.ModalInterface(Any[panel]))
close(stdin_write)
Base.wait(repltask)
end
# These are integration tests. If you want to unit test test e.g. completion, or
# exact LineEdit behavior, put them in the appropriate test files.
# Furthermore since we are emulating an entire terminal, there may be control characters
# in the mix. If verification needs to be done, keep it to the bare minimum. Basically
# this should make sure nothing crashes without depending on how exactly the control
# characters are being used.
fake_repl(options = REPL.Options(confirm_exit=false,hascolor=true)) do stdin_write, stdout_read, repl
repl.specialdisplay = REPL.REPLDisplay(repl)
repl.history_file = false
repltask = @async begin
REPL.run_repl(repl)
end
global inc = false
global b = Base.Event(true)
global c = Base.Event(true)
let cmd = "\"Hello REPL\""
write(stdin_write, "$(curmod_prefix)inc || wait($(curmod_prefix)b); r = $cmd; notify($(curmod_prefix)c); r\r")
end
let t = @async begin
inc = true
notify(b)
wait(c)
end
while (d = readline(stdout_read)) != ""
# first line [optional]: until 80th char of input
# second line: until end of input
# third line: "Hello REPL"
# last line: blank
# last+1 line: next prompt
end
wait(t)
end
# Latex completions
readuntil(stdout_read, "julia> ", keep=true)
write(stdin_write, "\x32\\alpha\t")
readuntil(stdout_read, "α")
# Bracketed paste in search mode
write(stdin_write, "\e[200~paste here ;)\e[201~")
# Abort search (^C)
write(stdin_write, '\x03')
# Test basic completion in main mode
write(stdin_write, "Base.REP\t")
readuntil(stdout_read, "REPL")
write(stdin_write, '\x03')
write(stdin_write, "\\alpha\t")
readuntil(stdout_read,"α")
write(stdin_write, '\x03')
# Test cd feature in shell mode.
origpwd = pwd()
mktempdir() do tmpdir
try
samefile = Base.Filesystem.samefile
tmpdir_pwd = cd(pwd, tmpdir)
homedir_pwd = cd(pwd, homedir())
# Test `cd`'ing to an absolute path
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t)
t = @async write(stdin_write, "cd $(escape_string(tmpdir))\n")
readuntil(stdout_read, "cd $(escape_string(tmpdir))")
readuntil(stdout_read, tmpdir_pwd * "\n\n")
wait(t)
@test samefile(".", tmpdir)
write(stdin_write, "\b")
# Test using `cd` to move to the home directory
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t)
t = @async write(stdin_write, "cd\n")
readuntil(stdout_read, homedir_pwd * "\n\n")
wait(t)
@test samefile(".", homedir_pwd)
t1 = @async write(stdin_write, "\b")
# Test using `-` to jump backward to tmpdir
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t1)
wait(t)
t = @async write(stdin_write, "cd -\n")
readuntil(stdout_read, tmpdir_pwd * "\n\n")
wait(t)
@test samefile(".", tmpdir)
t1 = @async write(stdin_write, "\b")
# Test using `~` (Base.expanduser) in `cd` commands
if !Sys.iswindows()
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t1)
wait(t)
t = @async write(stdin_write, "cd ~\n")
readuntil(stdout_read, homedir_pwd * "\n\n")
wait(t)
@test samefile(".", homedir_pwd)
write(stdin_write, "\b")
end
finally
cd(origpwd)
end
end
# issue #20482
#if !Sys.iswindows()
# write(stdin_write, ";")
# readuntil(stdout_read, "shell> ")
# write(stdin_write, "echo hello >/dev/null\n")
# let s = readuntil(stdout_read, "\n", keep=true)
# @test occursin("shell> ", s) # make sure we echoed the prompt
# @test occursin("echo hello >/dev/null", s) # make sure we echoed the input
# end
# @test readuntil(stdout_read, "\n", keep=true) == "\e[0m\n"
#end
# issue #20771
let s
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t)
t = @async write(stdin_write, "'\n") # invalid input
s = readuntil(stdout_read, "\n")
@test occursin("shell> ", s) # check for the echo of the prompt
@test occursin("'", s) # check for the echo of the input
s = readuntil(stdout_read, "\n\n")
@test startswith(s, "\e[0mERROR: unterminated single quote\nStacktrace:\n [1] ") ||
startswith(s, "\e[0m\e[1m\e[91mERROR: \e[39m\e[22m\e[91munterminated single quote\e[39m\nStacktrace:\n [1] ")
write(stdin_write, "\b")
wait(t)
end
# issue #27293
if Sys.isunix()
let s, old_stdout = stdout
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t)
proc_stdout_read, proc_stdout = redirect_stdout()
get_stdout = @async read(proc_stdout_read, String)
try
t = @async write(stdin_write, "echo ~\n")
readuntil(stdout_read, "~")
readuntil(stdout_read, "\n")
s = readuntil(stdout_read, "\n") # the child has exited
wait(t)
finally
redirect_stdout(old_stdout)
end
@test s == "\e[0m"
close(proc_stdout)
# check for the correct, expanded response
@test occursin(expanduser("~"), fetch(get_stdout))
write(stdin_write, "\b")
end
end
# issues #22176 & #20482
# TODO: figure out how to test this on Windows
#Sys.iswindows() || let tmp = tempname()
# try
# write(stdin_write, ";")
# readuntil(stdout_read, "shell> ")
# write(stdin_write, "echo \$123 >$tmp\n")
# let s = readuntil(stdout_read, "\n")
# @test occursin("shell> ", s) # make sure we echoed the prompt
# @test occursin("echo \$123 >$tmp", s) # make sure we echoed the input
# end
# @test readuntil(stdout_read, "\n", keep=true) == "\e[0m\n"
# @test read(tmp, String) == "123\n"
# finally
# rm(tmp, force=true)
# end
#end
# issue #10120
# ensure that command quoting works correctly
let s, old_stdout = stdout
t = @async write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
wait(t)
t = @async begin
Base.print_shell_escaped(stdin_write, Base.julia_cmd().exec..., special=Base.shell_special)
write(stdin_write, """ -e "println(\\"HI\\")\"""")
end
readuntil(stdout_read, ")\"")
wait(t)
proc_stdout_read, proc_stdout = redirect_stdout()
get_stdout = @async read(proc_stdout_read, String)
try
t = @async write(stdin_write, '\n')
s = readuntil(stdout_read, "\n")
if s == ""
# if shell width is precisely the text width,
# we may print some extra characters to fix the cursor state
s = readuntil(stdout_read, "\n")
@test occursin("shell> ", s)
s = readuntil(stdout_read, "\n")
@test s == "\r\r"
else
@test occursin("shell> ", s)
end
s = readuntil(stdout_read, "\n")
@test s == "\e[0m" # the child printed nothing
wait(t)
finally
redirect_stdout(old_stdout)
end
close(proc_stdout)
@test fetch(get_stdout) == "HI\n"
write(stdin_write, "\b")
end
# Issue #7001
# Test ignoring '\0'
let
write(stdin_write, "\0\n")
s = readuntil(stdout_read, "\n\n")
@test !occursin("invalid character", s)
end
# Test that accepting a REPL result immediately shows up, not
# just on the next keystroke
write(stdin_write, "1+1\n") # populate history with a trivial input
readline(stdout_read)
write(stdin_write, "\e[A\n")
let t = kill_timer(60)
# yield make sure this got processed
readuntil(stdout_read, "1+1")
readuntil(stdout_read, "\n\n")
close(t) # cancel timeout
end
# Issue #10222
# Test ignoring insert key in standard and prefix search modes
write(stdin_write, "\e[2h\e[2h\n") # insert (VT100-style)
@test findfirst("[2h", readline(stdout_read)) === nothing
readline(stdout_read)
write(stdin_write, "\e[2~\e[2~\n") # insert (VT220-style)
@test findfirst("[2~", readline(stdout_read)) === nothing
readline(stdout_read)
write(stdin_write, "1+1\n") # populate history with a trivial input
readline(stdout_read)
write(stdin_write, "\e[A\e[2h\n") # up arrow, insert (VT100-style)
readline(stdout_read)
readline(stdout_read)
write(stdin_write, "\e[A\e[2~\n") # up arrow, insert (VT220-style)
readline(stdout_read)
readline(stdout_read)
# Test down arrow to go back to history
# populate history with a trivial input
s1 = "12345678"; s2 = "23456789"
write(stdin_write, s1, '\n')
readuntil(stdout_read, s1)
write(stdin_write, s2, '\n')
readuntil(stdout_read, s2)
# Two up arrow, enter, should get back to 1
write(stdin_write, "\e[A\e[A\n")
readuntil(stdout_read, s1)
# Now, down arrow, enter, should get us back to 2
write(stdin_write, "\e[B\n")
readuntil(stdout_read, s2)
# test that prefix history search "passes through" key bindings to parent mode
write(stdin_write, "0x321\n")
readuntil(stdout_read, "0x321")
write(stdin_write, "\e[A\e[1;3C|||") # uparrow (go up history) and then Meta-rightarrow (indent right)
s2 = readuntil(stdout_read, "|||", keep=true)
@test endswith(s2, " 0x321\r\e[13C|||") # should have a space (from Meta-rightarrow) and not
# have a spurious C before ||| (the one here is not spurious!)
# "pass through" for ^x^x
write(stdin_write, "\x030x4321\n") # \x03 == ^c
readuntil(stdout_read, "0x4321")
write(stdin_write, "\e[A\x18\x18||\x18\x18||||") # uparrow, ^x^x||^x^x||||
s3 = readuntil(stdout_read, "||||", keep=true)
@test endswith(s3, "||0x4321\r\e[15C||||")
# Delete line (^U) and close REPL (^D)
write(stdin_write, "\x15\x04")
Base.wait(repltask)
nothing
end
function buffercontents(buf::IOBuffer)
p = position(buf)
seek(buf,0)
c = read(buf, String)
seek(buf,p)
c
end
function AddCustomMode(repl, prompt)
# Custom REPL mode tests
foobar_mode = LineEdit.Prompt(prompt;
prompt_prefix="\e[38;5;166m",
prompt_suffix=Base.text_colors[:white],
on_enter = s->true,
on_done = line->true)
main_mode = repl.interface.modes[1]
push!(repl.interface.modes,foobar_mode)
hp = main_mode.hist
hp.mode_mapping[:foobar] = foobar_mode
foobar_mode.hist = hp
foobar_keymap = Dict{Any,Any}(
'<' => function (s,args...)
if isempty(s)
if !haskey(s.mode_state,foobar_mode)
s.mode_state[foobar_mode] = LineEdit.init_state(repl.t,foobar_mode)
end
LineEdit.transition(s,foobar_mode)
else
LineEdit.edit_insert(s,'<')
end
end
)
search_prompt, skeymap = LineEdit.setup_search_keymap(hp)
mk = REPL.mode_keymap(main_mode)
b = Dict{Any,Any}[skeymap, mk, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults]
foobar_mode.keymap_dict = LineEdit.keymap(b)
main_mode.keymap_dict = LineEdit.keymap_merge(main_mode.keymap_dict, foobar_keymap)
foobar_mode, search_prompt
end
# Note: since the \t character matters for the REPL file history,
# it is important not to have the """ code reindent this line,
# possibly converting \t to spaces.
fakehistory = """
# time: 2014-06-29 20:44:29 EDT
# mode: julia
\té
# time: 2014-06-29 21:44:29 EDT
# mode: julia
\téé
# time: 2014-06-30 17:32:49 EDT
# mode: julia
\tshell
# time: 2014-06-30 17:32:59 EDT
# mode: shell
\tll
# time: 2014-06-30 99:99:99 EDT
# mode: julia
\tx ΔxΔ
# time: 2014-06-30 17:32:49 EDT
# mode: julia
\t1 + 1
# time: 2014-06-30 17:35:39 EDT
# mode: foobar
\tbarfoo
# time: 2014-06-30 18:44:29 EDT
# mode: shell
\tls
# time: 2014-06-30 19:44:29 EDT
# mode: foobar
\tls
# time: 2014-06-30 20:44:29 EDT
# mode: julia
\t2 + 2
"""
# Test various history related issues
for prompt = ["TestΠ", () -> randstring(rand(1:10))]
fake_repl() do stdin_write, stdout_read, repl
# In the future if we want we can add a test that the right object
# gets displayed by intercepting the display
repl.specialdisplay = REPL.REPLDisplay(repl)
errormonitor(@async write(devnull, stdout_read)) # redirect stdout to devnull so we drain the output pipe
repl.interface = REPL.setup_interface(repl)
repl_mode = repl.interface.modes[1]
shell_mode = repl.interface.modes[2]
help_mode = repl.interface.modes[3]
histp = repl.interface.modes[4]
prefix_mode = repl.interface.modes[5]
hp = REPL.REPLHistoryProvider(Dict{Symbol,Any}(:julia => repl_mode,
:shell => shell_mode,
:help => help_mode))
hist_path = tempname()
write(hist_path, fakehistory)
REPL.hist_from_file(hp, hist_path)
f = open(hist_path, read=true, write=true, create=true)
hp.history_file = f
seekend(f)
REPL.history_reset_state(hp)
histp.hp = repl_mode.hist = shell_mode.hist = help_mode.hist = hp
# Some manual setup
s = LineEdit.init_state(repl.t, repl.interface)
repl.mistate = s
LineEdit.edit_insert(s, "wip")
# LineEdit functions related to history
LineEdit.edit_insert_last_word(s)
@test buffercontents(LineEdit.buffer(s)) == "wip2"
LineEdit.edit_backspace(s) # remove the "2"
# Test that navigating history skips invalid modes
# (in both directions)
LineEdit.history_prev(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "2 + 2"
LineEdit.history_prev(s, hp)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_prev(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "1 + 1"
LineEdit.history_next(s, hp)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_next(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "2 + 2"
LineEdit.history_next(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "wip"
@test position(LineEdit.buffer(s)) == 3
LineEdit.history_next(s, hp)
@test buffercontents(LineEdit.buffer(s)) == "wip"
LineEdit.history_prev(s, hp, 2)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_prev(s, hp, -2) # equivalent to history_next(s, hp, 2)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "2 + 2"
LineEdit.history_next(s, hp, -2) # equivalent to history_prev(s, hp, 2)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_first(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "é"
LineEdit.history_next(s, hp, 6)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_last(s, hp)
@test buffercontents(LineEdit.buffer(s)) == "wip"
@test position(LineEdit.buffer(s)) == 3
# test that history_first jumps to beginning of current session's history
hp.start_idx -= 5 # temporarily alter history
LineEdit.history_first(s, hp)
@test hp.cur_idx == 6
# we are at the beginning of current session's history, so history_first
# must now jump to the beginning of all history
LineEdit.history_first(s, hp)
@test hp.cur_idx == 1
LineEdit.history_last(s, hp)
@test hp.cur_idx-1 == length(hp.history)
hp.start_idx += 5
LineEdit.move_line_start(s)
@test position(LineEdit.buffer(s)) == 0
# Test that the same holds for prefix search
ps = LineEdit.state(s, prefix_mode)::LineEdit.PrefixSearchState
@test LineEdit.input_string(ps) == ""
LineEdit.enter_prefix_search(s, prefix_mode, true)
LineEdit.history_prev_prefix(ps, hp, "")
@test ps.prefix == ""
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "2 + 2"
@test position(LineEdit.buffer(s)) == 5
LineEdit.history_prev_prefix(ps, hp, "")
@test ps.parent == shell_mode
@test LineEdit.input_string(ps) == "ls"
@test position(LineEdit.buffer(s)) == 2
LineEdit.history_prev_prefix(ps, hp, "sh")
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "shell"
@test position(LineEdit.buffer(s)) == 2
LineEdit.history_next_prefix(ps, hp, "sh")
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "wip"
@test position(LineEdit.buffer(s)) == 0
LineEdit.move_input_end(s)
LineEdit.history_prev_prefix(ps, hp, "é")
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "éé"
@test position(LineEdit.buffer(s)) == sizeof("é") > 1
LineEdit.history_prev_prefix(ps, hp, "é")
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "é"
@test position(LineEdit.buffer(s)) == sizeof("é")
LineEdit.history_next_prefix(ps, hp, "zzz")
@test ps.parent == repl_mode
@test LineEdit.input_string(ps) == "wip"
@test position(LineEdit.buffer(s)) == 3
LineEdit.accept_result(s, prefix_mode)
# Test that searching backwards puts you into the correct mode and
# skips invalid modes.
LineEdit.enter_search(s, histp, true)
ss = LineEdit.state(s, histp)
write(ss.query_buffer, "l")
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
@test position(LineEdit.buffer(s)) == 0
# Test that searching for `ll` actually matches `ll` after
# both letters are types rather than jumping to `shell`
LineEdit.history_prev(s, hp)
LineEdit.enter_search(s, histp, true)
write(ss.query_buffer, "l")
LineEdit.update_display_buffer(ss, ss)
@test buffercontents(ss.response_buffer) == "ll"
@test position(ss.response_buffer) == 1
write(ss.query_buffer, "l")
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ll"
@test position(LineEdit.buffer(s)) == 0
# Test that searching backwards with a one-letter query doesn't
# return indefinitely the same match (#9352)
LineEdit.enter_search(s, histp, true)
write(ss.query_buffer, "l")
LineEdit.update_display_buffer(ss, ss)
LineEdit.history_next_result(s, ss)
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "shell"
@test position(LineEdit.buffer(s)) == 4
# Test that searching backwards doesn't skip matches (#9352)
# (for a search with multiple one-byte characters, or UTF-8 characters)
LineEdit.enter_search(s, histp, true)
write(ss.query_buffer, "é") # matches right-most "é" in "éé"
LineEdit.update_display_buffer(ss, ss)
@test position(ss.query_buffer) == sizeof("é")
LineEdit.history_next_result(s, ss) # matches left-most "é" in "éé"
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test buffercontents(LineEdit.buffer(s)) == "éé"
@test position(LineEdit.buffer(s)) == 0
# Issue #7551
# Enter search mode and try accepting an empty result
REPL.history_reset_state(hp)
LineEdit.edit_clear(s)
cur_mode = LineEdit.mode(s)
LineEdit.enter_search(s, histp, true)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == cur_mode
@test buffercontents(LineEdit.buffer(s)) == ""
@test position(LineEdit.buffer(s)) == 0
# Test that new modes can be dynamically added to the REPL and will
# integrate nicely
foobar_mode, custom_histp = AddCustomMode(repl, prompt)
# ^R l, should now find `ls` in foobar mode
LineEdit.enter_search(s, histp, true)
ss = LineEdit.state(s, histp)
write(ss.query_buffer, "l")
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == foobar_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
@test position(LineEdit.buffer(s)) == 0
# Try the same for prefix search
LineEdit.history_next(s, hp)
LineEdit.history_prev_prefix(ps, hp, "l")
@test ps.parent == foobar_mode
@test LineEdit.input_string(ps) == "ls"
@test position(LineEdit.buffer(s)) == 1
# Some Unicode handling testing
LineEdit.history_prev(s, hp)
LineEdit.enter_search(s, histp, true)
write(ss.query_buffer, "x")
LineEdit.update_display_buffer(ss, ss)
@test buffercontents(ss.response_buffer) == "x ΔxΔ"
@test position(ss.response_buffer) == 4
write(ss.query_buffer, " ")
LineEdit.update_display_buffer(ss, ss)
LineEdit.accept_result(s, histp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "x ΔxΔ"
@test position(LineEdit.buffer(s)) == 0
LineEdit.edit_clear(s)
LineEdit.enter_search(s, histp, true)
ss = LineEdit.state(s, histp)
write(ss.query_buffer, "Å") # should not be in history
LineEdit.update_display_buffer(ss, ss)
@test buffercontents(ss.response_buffer) == ""
@test position(ss.response_buffer) == 0
LineEdit.history_next_result(s, ss) # should not throw BoundsError
LineEdit.accept_result(s, histp)
# Try entering search mode while in custom repl mode
LineEdit.enter_search(s, custom_histp, true)
end
end
# Test removal of prompt in bracket pasting
fake_repl() do stdin_write, stdout_read, repl
repl.interface = REPL.setup_interface(repl)
repl_mode = repl.interface.modes[1]
shell_mode = repl.interface.modes[2]
help_mode = repl.interface.modes[3]
repltask = @async begin
REPL.run_repl(repl)
end
global c = Base.Event(true)
function sendrepl2(cmd)
t = @async readuntil(stdout_read, "\"done\"\n\n")
write(stdin_write, "$cmd\n notify($(curmod_prefix)c); \"done\"\n")
wait(c)
fetch(t)
end
# Test removal of prefix in single statement paste
sendrepl2("\e[200~julia> A = 2\e[201~\n")
@test Main.A == 2
# Test removal of prefix in single statement paste
sendrepl2("\e[200~In [12]: A = 2.2\e[201~\n")
@test Main.A == 2.2
# Test removal of prefix in multiple statement paste
sendrepl2("""\e[200~
julia> mutable struct T17599; a::Int; end
julia> function foo(julia)
julia> 3
end
julia> A = 3\e[201~
""")
@test Main.A == 3
@test Base.invokelatest(Main.foo, 4)
@test Base.invokelatest(Main.T17599, 3).a == 3
@test !Base.invokelatest(Main.foo, 2)
sendrepl2("""\e[200~
julia> goo(x) = x + 1
error()
julia> A = 4
4\e[201~
""")
@test Main.A == 4
@test Base.invokelatest(Main.goo, 4) == 5
# Test prefix removal only active in bracket paste mode
sendrepl2("julia = 4\n julia> 3 && (A = 1)\n")
@test Main.A == 1
# Test that indentation corresponding to the prompt is removed
s = sendrepl2("""\e[200~julia> begin\n α=1\n β=2\n end\n\e[201~""")
s2 = split(rsplit(s, "begin", limit=2)[end], "end", limit=2)[1]
@test s2 == "\n\r\e[7C α=1\n\r\e[7C β=2\n\r\e[7C"
# for incomplete input (`end` below is added after the end of bracket paste)
s = sendrepl2("""\e[200~julia> begin\n α=1\n β=2\n\e[201~end""")
s2 = split(rsplit(s, "begin", limit=2)[end], "end", limit=2)[1]
@test s2 == "\n\r\e[7C α=1\n\r\e[7C β=2\n\r\e[7C"
# Test switching repl modes
redirect_stdout(devnull) do # to suppress "foo" echoes
sendrepl2("""\e[200~
julia> A = 1
1
shell> echo foo
foo
shell> echo foo
foo
foo foo
help?> Int
Dummy docstring
Some text
julia> error("If this error throws, the paste handler has failed to ignore this docstring example")
julia> B = 2
2\e[201~
""")
@test Main.A == 1
@test Main.B == 2
end # redirect_stdout
# Close repl
write(stdin_write, '\x04')
Base.wait(repltask)
end
# Simple non-standard REPL tests
fake_repl() do stdin_write, stdout_read, repl
panel = LineEdit.Prompt("testπ";
prompt_prefix="\e[38;5;166m",
prompt_suffix=Base.text_colors[:white],
on_enter = s->true)
hp = REPL.REPLHistoryProvider(Dict{Symbol,Any}(:parse => panel))
search_prompt, skeymap = LineEdit.setup_prefix_keymap(hp, panel)
REPL.history_reset_state(hp)
panel.hist = hp
panel.keymap_dict = LineEdit.keymap(Dict{Any,Any}[skeymap,
LineEdit.default_keymap, LineEdit.escape_defaults])
c = Condition()
panel.on_done = (s, buf, ok) -> begin
if !ok
LineEdit.transition(s, :abort)
end
line = strip(String(take!(buf)))
LineEdit.reset_state(s)
notify(c, line)
nothing
end
repltask = @async REPL.run_interface(repl.t, LineEdit.ModalInterface(Any[panel, search_prompt]))
write(stdin_write, "a\n")
@test wait(c) == "a"
# Up arrow enter should recall history even at the start
write(stdin_write, "\e[A\n")
@test wait(c) == "a"
# And again
write(stdin_write, "\e[A\n")
@test wait(c) == "a"
# Close REPL ^D
write(stdin_write, '\x04')
Base.wait(repltask)
end
Base.exit_on_sigint(true)
let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
# Test REPL in dumb mode
with_fake_pty() do pts, ptm
nENV = copy(ENV)
nENV["TERM"] = "dumb"
p = run(detach(setenv(`$exename -q`, nENV)), pts, pts, pts, wait=false)
Base.close_stdio(pts)
output = readuntil(ptm, "julia> ", keep=true)
if ccall(:jl_running_on_valgrind, Cint,()) == 0
# If --trace-children=yes is passed to valgrind, we will get a
# valgrind banner here, not just the prompt.
@test output == "julia> "
end
write(ptm, "1\nexit()\n")
output = readuntil(ptm, ' ', keep=true)
if Sys.iswindows()
# Our fake pty is actually a pipe, and thus lacks the input echo feature of posix
@test output == "1\n\njulia> "
else
@test output == "1\r\nexit()\r\n1\r\n\r\njulia> "
end
@test bytesavailable(ptm) == 0
@test if Sys.iswindows() || Sys.isbsd()
eof(ptm)
else
# Some platforms (such as linux) report EIO instead of EOF
# possibly consume child-exited notification
# for example, see discussion in https://bugs.python.org/issue5380
try
eof(ptm) && !Sys.islinux()
catch ex
(ex isa Base.IOError && ex.code == Base.UV_EIO) || rethrow()
@test_throws ex eof(ptm) # make sure the error is sticky
ptm.readerror = nothing
eof(ptm)
end
end
@test read(ptm, String) == ""
wait(p)
end
# Test stream mode
p = open(`$exename -q`, "r+")
write(p, "1\nexit()\n")
@test read(p, String) == "1\n"
end # let exename
# issue #19864
mutable struct Error19864 <: Exception; end
function test19864()
@eval Base.showerror(io::IO, e::Error19864) = print(io, "correct19864")
buf = IOBuffer()
fake_response = (Base.ExceptionStack([(exception=Error19864(),backtrace=Ptr{Cvoid}[])]),true)
REPL.print_response(buf, fake_response, false, false, nothing)
return String(take!(buf))
end
@test occursin("correct19864", test19864())
# Test containers in error messages are limited #18726
let io = IOBuffer()
Base.display_error(io, Base.ExceptionStack(Any[(exception =
(try
[][trues(6000)]
@assert false
catch e
e
end), backtrace = [])]))
@test length(String(take!(io))) < 1500
end
fake_repl() do stdin_write, stdout_read, repl
# Relies on implementation detail to make sure we only have the single
# replinit callback we want to test.
saved_replinit = copy(Base.repl_hooks)
slot = Ref(false)
# Create a closure from a newer world to check if `_atreplinit`
# can run it correctly
atreplinit(@eval(repl::REPL.LineEditREPL -> ($slot[] = true)))
Base._atreplinit(repl)
@test slot[]
@test_throws MethodError Base.repl_hooks[1](repl)
copyto!(Base.repl_hooks, saved_replinit)
nothing
end
let ends_with_semicolon = REPL.ends_with_semicolon
@test !ends_with_semicolon("")
@test ends_with_semicolon(";")
@test !ends_with_semicolon("ä")
@test !ends_with_semicolon("ä # äsdf ;")
@test ends_with_semicolon("""a * "#ä" ;""")
@test ends_with_semicolon("a; #=#=# =# =#\n")
@test ends_with_semicolon("1;")
@test ends_with_semicolon("1;\n")
@test ends_with_semicolon("1;\r")
@test ends_with_semicolon("1;\r\n \t\f")
@test ends_with_semicolon("1;#äsdf\n")
@test ends_with_semicolon("""1;\n#äsdf\n""")
@test !ends_with_semicolon("\"\\\";\"#\"")
@test ends_with_semicolon("\"\\\\\";#\"")
@test !ends_with_semicolon("begin\na;\nb;\nend")
@test !ends_with_semicolon("begin\na; #=#=#\n=#b=#\nend")
@test ends_with_semicolon("\na; #=#=#\n=#b=#\n# test\n#=\nfoobar\n=##bazbax\n")
@test ends_with_semicolon("f()= 1; # é ; 2")
@test ends_with_semicolon("f()= 1; # é")
@test !ends_with_semicolon("f()= 1; \"é\"")
@test !ends_with_semicolon("""("f()= 1; # é")""")
@test !ends_with_semicolon(""" "f()= 1; # é" """)
@test ends_with_semicolon("f()= 1;")
# the next result does not matter because this is not legal syntax
@test_nowarn ends_with_semicolon("1; #=# 2")
end
# PR #20794, TTYTerminal with other kinds of streams
let term = REPL.Terminals.TTYTerminal("dumb",IOBuffer("1+2\n"),IOContext(IOBuffer(),:foo=>true),IOBuffer())
r = REPL.BasicREPL(term)
REPL.run_repl(r)
@test String(take!(term.out_stream.io)) == "julia> 3\n\njulia> \n"
@test haskey(term, :foo) == true
@test haskey(term, :bar) == false
@test (:foo=>true) in term
@test (:foo=>false) ∉ term
@test term[:foo] == get(term, :foo, nothing) == true
@test get(term, :bar, nothing) === nothing
@test_throws KeyError term[:bar]
end
# Ensure even the dumb REPL elides content