Skip to content

Add ability to activate test/build target envs #4218

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 14 additions & 3 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,11 @@ function status(ctx::Context, pkgs::Vector{PackageSpec}; diff::Bool=false, mode=
end


function activate(;temp=false, shared=false, prev=false, io::IO=stderr_f())
function activate(;temp=false, shared=false, prev=false, target::Union{Symbol,Nothing}=nothing, io::IO=stderr_f())
shared && pkgerror("Must give a name for a shared environment")
if target !== nothing
return activate(Base.active_project(); target, io)
end
temp && return activate(mktempdir(); io=io)
if prev
if isempty(PREV_ENV_PATH[])
Expand Down Expand Up @@ -1333,12 +1336,12 @@ function _activate_dep(dep_name::AbstractString)
uuid = get(ctx.env.project.deps, dep_name, nothing)
if uuid !== nothing
entry = manifest_info(ctx.env.manifest, uuid)
if entry.path !== nothing
if entry !== nothing && entry.path !== nothing
return joinpath(dirname(ctx.env.manifest_file), entry.path::String)
end
end
end
function activate(path::AbstractString; shared::Bool=false, temp::Bool=false, io::IO=stderr_f())
function activate(path::AbstractString; shared::Bool=false, temp::Bool=false, target::Union{Symbol,Nothing}=nothing, io::IO=stderr_f())
temp && pkgerror("Can not give `path` argument when creating a temporary environment")
if !shared
# `pkg> activate path`/`Pkg.activate(path)` does the following
Expand Down Expand Up @@ -1370,6 +1373,14 @@ function activate(path::AbstractString; shared::Bool=false, temp::Bool=false, io
fullpath = joinpath(Pkg.envdir(Pkg.depots1()), path)
end
end
if target !== nothing
printpkgstyle(io, :Creating, "temporary project based on the target `$target` in $(pathrepr(fullpath))", color = Base.info_color())
target_project = Operations.gen_target_project(Context(), nothing, fullpath, "test")
temp_project_dir = mktempdir()
temp_projectfile = joinpath(temp_project_dir, "Project.toml")
Types.write_project(target_project, temp_projectfile)
fullpath = temp_projectfile
end
if !isnothing(Base.active_project())
PREV_ENV_PATH[] = Base.active_project()
end
Expand Down
6 changes: 5 additions & 1 deletion src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2219,11 +2219,15 @@ function parse_REQUIRE(require_path::String)
end

# "targets" based test deps -> "test/Project.toml" based deps
function gen_target_project(ctx::Context, pkg::PackageSpec, source_path::String, target::String)
function gen_target_project(ctx::Context, pkg::Union{PackageSpec,Nothing}, source_path::String, target::String)
env = ctx.env
registries = ctx.registries
test_project = Types.Project()
if projectfile_path(source_path; strict=true) === nothing
if pkg === nothing
# don't support REQUIRE if pkg isn't provided
pkgerror("No project file found in $(source_path)")
end
# no project file, assuming this is an old REQUIRE package
test_project.deps = copy(env.manifest[pkg.uuid].deps)
if target == "test"
Expand Down
1 change: 1 addition & 0 deletions src/REPLMode/argument_parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ end
#
function parse_activate(args::Vector{QString}, options)
isempty(args) && return [] # nothing to do
args = filter(a -> !in(a.raw, ("--test", "--build")), args)
if length(args) == 1
x = first(args)
if x.isquoted
Expand Down
6 changes: 5 additions & 1 deletion src/REPLMode/command_declarations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ packages have changed causing the current Manifest to be out of sync.
],
PSA[:name => "activate",
:api => API.activate,
:arg_count => 0 => 1,
:arg_count => 0 => 2,
:arg_parser => parse_activate,
:option_spec => [
PSA[:name => "shared", :api => :shared => true],
PSA[:name => "temp", :api => :temp => true],
PSA[:name => "test", :api => :target => :test],
PSA[:name => "build", :api => :target => :build],
],
:completions => :complete_activate,
:description => "set the primary environment the package manager manipulates",
Expand All @@ -321,6 +323,8 @@ PSA[:name => "activate",
activate [--shared] path
activate --temp
activate - (activates the previously active environment)
activate --test (activates the test target within the current environment)
activate --build (activates the build target within the current environment)

Activate the environment at the given `path`, or use the first project found in
`LOAD_PATH` (ignoring `"@"`) if no `path` is specified.
Expand Down
1 change: 1 addition & 0 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ end

function projectfile_path(env_path::String; strict=false)
for name in Base.project_names
basename(env_path) == name && isfile(env_path) && return env_path
maybe_file = joinpath(env_path, name)
isfile(maybe_file) && return maybe_file
end
Expand Down
Loading