Skip to content

Fix (add|set)env to keep currently set dir for the command #43276

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

Merged
merged 1 commit into from
Dec 1, 2021
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
10 changes: 6 additions & 4 deletions base/cmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ byteenv(env::Union{AbstractVector{Pair{T,V}}, Tuple{Vararg{Pair{T,V}}}}) where {
String[cstr(k*"="*string(v)) for (k,v) in env]

"""
setenv(command::Cmd, env; dir="")
setenv(command::Cmd, env; dir)

Set environment variables to use when running the given `command`. `env` is either a
dictionary mapping strings to strings, an array of strings of the form `"var=val"`, or
Expand All @@ -242,13 +242,15 @@ existing environment, create `env` through `copy(ENV)` and then setting `env["va
as desired, or use [`addenv`](@ref).

The `dir` keyword argument can be used to specify a working directory for the command.
`dir` defaults to the currently set `dir` for `command` (which is the current working
directory if not specified already).

See also [`Cmd`](@ref), [`addenv`](@ref), [`ENV`](@ref), [`pwd`](@ref).
"""
setenv(cmd::Cmd, env; dir="") = Cmd(cmd; env=byteenv(env), dir=dir)
setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir="") =
setenv(cmd::Cmd, env; dir=cmd.dir) = Cmd(cmd; env=byteenv(env), dir=dir)
setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir=cmd.dir) =
setenv(cmd, env; dir=dir)
setenv(cmd::Cmd; dir="") = Cmd(cmd; dir=dir)
setenv(cmd::Cmd; dir=cmd.dir) = Cmd(cmd; dir=dir)

"""
addenv(command::Cmd, env...; inherit::Bool = true)
Expand Down
23 changes: 23 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,29 @@ end
cmd = Cmd(`$shcmd -c "echo \$FOO \$BAR"`, env=Dict("FOO" => "foo", "BAR" => "bar"))
cmd2 = addenv(cmd, "FOO" => nothing)
@test strip(String(read(cmd2))) == "bar"
# addenv keeps the cmd's dir (#42131)
dir = joinpath(pwd(), "dir")
cmd = addenv(setenv(`julia`; dir=dir), Dict())
@test cmd.dir == dir
end

@testset "setenv with dir (with tests for #42131)" begin
dir1 = joinpath(pwd(), "dir1")
dir2 = joinpath(pwd(), "dir2")
cmd = Cmd(`julia`; dir=dir1)
@test cmd.dir == dir1
@test Cmd(cmd).dir == dir1
@test Cmd(cmd; dir=dir2).dir == dir2
@test Cmd(cmd; dir="").dir == ""
@test setenv(cmd).dir == dir1
@test setenv(cmd; dir=dir2).dir == dir2
@test setenv(cmd; dir="").dir == ""
@test setenv(cmd, "FOO"=>"foo").dir == dir1
@test setenv(cmd, "FOO"=>"foo"; dir=dir2).dir == dir2
@test setenv(cmd, "FOO"=>"foo"; dir="").dir == ""
@test setenv(cmd, Dict("FOO"=>"foo")).dir == dir1
@test setenv(cmd, Dict("FOO"=>"foo"); dir=dir2).dir == dir2
@test setenv(cmd, Dict("FOO"=>"foo"); dir="").dir == ""
end


Expand Down