Skip to content

Commit

Permalink
Make Pkg.activate(path) behave like pkg> activate path. (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored and KristofferC committed Aug 4, 2018
1 parent 3e88964 commit e9e320b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
21 changes: 21 additions & 0 deletions stdlib/Pkg/src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,28 @@ function status(ctx::Context, mode=PKGMODE_PROJECT)
end

function activate(path::Union{String,Nothing}=nothing)
if path !== nothing
devpath = nothing
env = Base.active_project() === nothing ? nothing : EnvCache()
if env !== nothing && haskey(env.project["deps"], path)
uuid = UUID(env.project["deps"][path])
info = manifest_info(env, uuid)
devpath = haskey(info, "path") ? joinpath(dirname(env.project_file), info["path"]) : nothing
end
# `pkg> activate path`/`Pkg.activate(path)` does the following
# 1. if path exists, activate that
# 2. if path exists in deps, and the dep is deved, activate that path (`devpath` above)
# 3. activate the non-existing directory (e.g. as in `pkg> activate .` for initing a new env)
if Types.isdir_windows_workaround(path)
path = abspath(path)
elseif devpath !== nothing
path = abspath(devpath)
else
path = abspath(path)
end
end
Base.ACTIVE_PROJECT[] = Base.load_path_expand(path)
return
end

"""
Expand Down
23 changes: 3 additions & 20 deletions stdlib/Pkg/src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -612,28 +612,11 @@ do_up!(ctx::Context, args::PkgArguments, api_opts::Vector{APIOption}) =
API.up(ctx, args; api_opts...)

function do_activate!(ctx::Context, args::PkgArguments, api_opts::Vector{APIOption})
# TODO: Remove the ctx argument to this function.
if isempty(args)
return API.activate()
end

path = args[1]
env = Base.active_project() === nothing ? nothing : ctx.env
devpath = nothing
if env !== nothing && haskey(env.project["deps"], path)
uuid = UUID(env.project["deps"][path])
info = manifest_info(env, uuid)
devpath = haskey(info, "path") ? joinpath(dirname(env.project_file), info["path"]) : nothing
end
# `pkg> activate path` does the following
# 1. if path exists, activate that
# 2. if path exists in deps, and the dep is deved, activate that path (`devpath`) above
# 3. activate the non-existing directory (e.g. as in `pkg> activate . for initing a new dev`)
if Types.isdir_windows_workaround(path)
API.activate(abspath(path))
elseif devpath !== nothing
API.activate(abspath(devpath))
return API.activate(nothing)
else
API.activate(abspath(path))
return API.activate(args[1])
end
end

Expand Down
39 changes: 39 additions & 0 deletions stdlib/Pkg/test/api.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module APITests

using Pkg, Test

@testset "Pkg.activate" begin
cd(mktempdir()) do
path = pwd()
Pkg.activate(".")
mkdir("Foo")
cd(mkdir("modules")) do
Pkg.generate("Foo")
end
Pkg.develop(Pkg.Types.PackageSpec(url="modules/Foo")) # to avoid issue #542
Pkg.activate("Foo") # activate path Foo over deps Foo
@test Base.active_project() == joinpath(path, "Foo", "Project.toml")
Pkg.activate(".")
rm("Foo"; force=true, recursive=true)
Pkg.activate("Foo") # activate path from developed Foo
@test Base.active_project() == joinpath(path, "modules", "Foo", "Project.toml")
Pkg.activate(".")
Pkg.activate("./Foo") # activate empty directory Foo (sidestep the developed Foo)
@test Base.active_project() == joinpath(path, "Foo", "Project.toml")
Pkg.activate(".")
Pkg.activate("Bar") # activate empty directory Bar
@test Base.active_project() == joinpath(path, "Bar", "Project.toml")
Pkg.activate(".")
Pkg.add("Example") # non-deved deps should not be activated
Pkg.activate("Example")
@test Base.active_project() == joinpath(path, "Example", "Project.toml")
Pkg.activate(".")
cd(mkdir("tests"))
Pkg.activate("Foo") # activate developed Foo from another directory
@test Base.active_project() == joinpath(path, "modules", "Foo", "Project.toml")
Pkg.activate() # activate home project
@test Base.ACTIVE_PROJECT[] === nothing
end
end

end # module APITests
1 change: 1 addition & 0 deletions stdlib/Pkg/test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,6 @@ temp_pkg_dir() do project_path
end

include("repl.jl")
include("api.jl")

end # module
2 changes: 2 additions & 0 deletions stdlib/Pkg/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ temp_pkg_dir() do project_path
cd(mkdir("tests"))
pkg"activate Foo" # activate developed Foo from another directory
@test Base.active_project() == joinpath(path, "modules", "Foo", "Project.toml")
pkg"activate" # activate home project
@test Base.ACTIVE_PROJECT[] === nothing
end
end

Expand Down

0 comments on commit e9e320b

Please sign in to comment.