Skip to content

Commit

Permalink
Fix (add|set)env to keep currently set dir for the command, fixes #42131
Browse files Browse the repository at this point in the history
. (#43276)
  • Loading branch information
fredrikekre authored Dec 1, 2021
1 parent e2940cb commit f53de73
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
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

2 comments on commit f53de73

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

Please sign in to comment.