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

REPL: fix projname when project_file is missing #54795

Merged
merged 3 commits into from
Jun 14, 2024
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
16 changes: 12 additions & 4 deletions stdlib/REPL/src/Pkg_beforeload.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,18 @@ function relative_project_path(project_file::String, path::String)
end

function projname(project_file::String)
p = Base.TOML.Parser()
Base.TOML.reinit!(p, read(project_file, String); filepath=project_file)
proj = Base.TOML.parse(p)
name = get(proj, "name", nothing)
if isfile(project_file)
name = try
p = Base.TOML.Parser()
Base.TOML.reinit!(p, read(project_file, String); filepath=project_file)
proj = Base.TOML.parse(p)
get(proj, "name", nothing)
catch
nothing
end
else
name = nothing
end
if name === nothing
name = basename(dirname(project_file))
end
Expand Down
22 changes: 22 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1836,3 +1836,25 @@ end
@test_broken isempty(undoc)
@test undoc == [:AbstractREPL, :BasicREPL, :LineEditREPL, :StreamREPL]
end

@testset "Dummy Pkg prompt" begin
# do this in an empty depot to test default for new users
withenv("JULIA_DEPOT_PATH" => mktempdir(), "JULIA_LOAD_PATH" => nothing) do
prompt = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no -e "using REPL; print(REPL.Pkg_promptf())"`)
@test prompt == "(@v$(VERSION.major).$(VERSION.minor)) pkg> "
end

get_prompt(proj::String) = readchomp(`$(Base.julia_cmd()[1]) --startup-file=no $(proj) -e "using REPL; print(REPL.Pkg_promptf())"`)

@test get_prompt("--project=$(pkgdir(REPL))") == "(REPL) pkg> "

tdir = mkpath(joinpath(mktempdir(), "foo"))
@test get_prompt("--project=$tdir") == "(foo) pkg> "

proj_file = joinpath(tdir, "Project.toml")
touch(proj_file) # make a bad Project.toml
@test get_prompt("--project=$proj_file") == "(foo) pkg> "

write(proj_file, "name = \"Bar\"\n")
@test get_prompt("--project=$proj_file") == "(Bar) pkg> "
end