Skip to content

Commit 3cfe013

Browse files
Fix missed rename of Operations._compat. Add tests for interactive compat menu (#4344)
1 parent 8cee071 commit 3cfe013

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

ext/REPLExt/compat.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# TODO: Overload
2-
function _compat(ctx::Context; io = nothing)
2+
function _compat(ctx::Context; io = nothing, input_io = stdin)
33
io = something(io, ctx.io)
4-
can_fancyprint(io) || pkgerror("Pkg.compat cannot be run interactively in this terminal")
4+
if input_io isa Base.TTY # testing uses IOBuffer
5+
can_fancyprint(io) || pkgerror("Pkg.compat cannot be run interactively in this terminal")
6+
end
57
printpkgstyle(io, :Compat, pathrepr(ctx.env.project_file))
68
longest_dep_len = max(5, length.(collect(keys(ctx.env.project.deps)))...)
79
opt_strs = String[]
@@ -16,7 +18,7 @@ function _compat(ctx::Context; io = nothing)
1618
end
1719
menu = TerminalMenus.RadioMenu(opt_strs, pagesize = length(opt_strs))
1820
choice = try
19-
TerminalMenus.request(" Select an entry to edit:", menu)
21+
TerminalMenus.request(TerminalMenus.default_terminal(in = input_io, out = io), " Select an entry to edit:", menu)
2022
catch err
2123
if err isa InterruptException # if ^C is entered
2224
println(io)
@@ -35,10 +37,12 @@ function _compat(ctx::Context; io = nothing)
3537
start_pos = length(prompt) + 2
3638
move_start = "\e[$(start_pos)G"
3739
clear_to_end = "\e[0J"
38-
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), stdin.handle, true)
40+
if input_io isa Base.TTY
41+
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), input_io.handle, true)
42+
end
3943
while true
4044
print(io, move_start, clear_to_end, buffer, "\e[$(start_pos + cursor)G")
41-
inp = TerminalMenus._readkey(stdin)
45+
inp = TerminalMenus._readkey(input_io)
4246
if inp == '\r' # Carriage return
4347
println(io)
4448
break
@@ -85,9 +89,11 @@ function _compat(ctx::Context; io = nothing)
8589
end
8690
buffer
8791
finally
88-
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), stdin.handle, false)
92+
if input_io isa Base.TTY
93+
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid}, Int32), input_io.handle, false)
94+
end
8995
end
9096
new_entry = strip(resp)
91-
compat(ctx, dep, string(new_entry))
97+
API._compat(ctx, dep, string(new_entry))
9298
return
9399
end

test/repl.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,4 +805,36 @@ end
805805
@test Pkg.in_repl_mode() == false
806806
end
807807

808+
@testset "compat REPL mode" begin
809+
temp_pkg_dir() do project_path
810+
with_pkg_env(project_path; change_dir = true) do
811+
812+
pkg"add Example JSON"
813+
814+
test_ctx = Pkg.Types.Context()
815+
test_ctx.io = IOBuffer()
816+
817+
@test Pkg.Operations.get_compat_str(test_ctx.env.project, "Example") === nothing
818+
@test Pkg.Operations.get_compat_str(test_ctx.env.project, "JSON") === nothing
819+
820+
input_io = Base.BufferStream()
821+
# Send input to stdin before starting the _compat function
822+
# This simulates the user typing in the REPL
823+
write(input_io, "\e[B") # Down arrow once to select Example
824+
write(input_io, "\r") # Enter to confirm selection
825+
# now editing Example compat
826+
write(input_io, "0.4") # Set compat to 0.4
827+
write(input_io, "\r") # Enter to confirm input
828+
close(input_io)
829+
830+
Pkg.API._compat(test_ctx; input_io)
831+
832+
str = String(take!(test_ctx.io))
833+
@test occursin("Example = \"0.4\"", str)
834+
@test occursin("checking for compliance with the new compat rules..", str)
835+
@test occursin("Error empty intersection between", str) # Latest Example is at least 0.5.5
836+
end
837+
end
838+
end
839+
808840
end # module

0 commit comments

Comments
 (0)