Skip to content

Commit

Permalink
REPL: fix projname when project_file is missing (#54795)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Jun 14, 2024
1 parent d9a0c25 commit f4b81af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
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

0 comments on commit f4b81af

Please sign in to comment.