Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #9103, implementing cd - in REPL shell mode #9192

Merged
merged 4 commits into from
Dec 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
# and pass into Base.repl_cmd for processing (handles `ls` and `cd`
# special)
on_done = respond(repl, julia_prompt) do line
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, symbol("@cmd"),line)))
Expr(:call, :(Base.repl_cmd), macroexpand(Expr(:macrocall, symbol("@cmd"),line)), outstream(repl))
end)

################################# Stage II #############################
Expand Down
15 changes: 12 additions & 3 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exit(n) = ccall(:jl_exit, Void, (Int32,), n)
exit() = exit(0)
quit() = exit()

function repl_cmd(cmd)
function repl_cmd(cmd, out)
shell = shell_split(get(ENV,"JULIA_SHELL",get(ENV,"SHELL","/bin/sh")))
# Note that we can't support the fish shell due to its lack of subshells
# See this for details: https://github.com/JuliaLang/julia/issues/4918
Expand All @@ -50,15 +50,24 @@ function repl_cmd(cmd)
if isempty(cmd.exec)
error("no cmd to execute")
elseif cmd.exec[1] == "cd"
new_oldpwd = pwd()
if length(cmd.exec) > 2
error("cd method only takes one argument")
elseif length(cmd.exec) == 2
dir = cmd.exec[2]
cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
if dir == "-"
if !haskey(ENV, "OLDPWD")
error("cd: OLDPWD not set")
end
cd(ENV["OLDPWD"])
else
cd(@windows? dir : readchomp(`$shell -c "echo $(shell_escape(dir))"`))
end
else
cd()
end
println(pwd())
ENV["OLDPWD"] = new_oldpwd
println(out, pwd())
else
run(ignorestatus(@windows? cmd : (isa(STDIN, TTY) ? `$shell -i -c "($(shell_escape(cmd))) && true"` : `$shell -c "($(shell_escape(cmd))) && true"`)))
end
Expand Down
29 changes: 29 additions & 0 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@ write(stdin_write, '\x03')
write(stdin_write, "\\alpha\t")
readuntil(stdout_read,"α")
write(stdin_write, '\x03')
# Test cd feature in shell mode. We limit to 40 characters when
# calling readuntil() to suppress the warning it (currently) gives for
# long strings.
origpwd = pwd()
tmpdir = mktempdir()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be changed to tmpdir = escape_string(mktempdir()) due to no escaption takes place when you write to stdin_write. This effects windows path due to it uses backslashes, but it also effects linux if there is spaces in the path.

write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd $(escape_string(tmpdir))\n")
readuntil(stdout_read, "cd $(escape_string(tmpdir))"[max(1,end-39):end])
readuntil(stdout_read, realpath(tmpdir)[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == realpath(tmpdir)
write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd -\n")
readuntil(stdout_read, origpwd[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == origpwd
write(stdin_write, ";")
readuntil(stdout_read, "shell> ")
write(stdin_write, "cd\n")
readuntil(stdout_read, homedir()[max(1,end-39):end])
readuntil(stdout_read, "\n")
readuntil(stdout_read, "\n")
@test pwd() == homedir()
rm(tmpdir)
cd(origpwd)
# Close REPL ^D
write(stdin_write, '\x04')
wait(repltask)
Expand Down