Skip to content

Commit

Permalink
fix bug in addenv for environment entries with embedded = (JuliaL…
Browse files Browse the repository at this point in the history
…ang#44212)

Co-authored-by: Jameson Nash <[email protected]>
Co-authored-by: Fredrik Ekre <[email protected]>
  • Loading branch information
3 people committed Feb 18, 2022
1 parent 15dcd5b commit f5d9b86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions base/cmd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ setenv(cmd::Cmd, env::Pair{<:AbstractString}...; dir=cmd.dir) =
setenv(cmd, env; dir=dir)
setenv(cmd::Cmd; dir=cmd.dir) = Cmd(cmd; dir=dir)

# split environment entry string into before and after first `=` (key and value)
function splitenv(e::String)
i = findnext('=', e, 2)
if i === nothing
throw(ArgumentError("malformed environment entry"))
end
e[1:prevind(e, i)], e[nextind(e, i):end]
end

"""
addenv(command::Cmd, env...; inherit::Bool = true)
Expand All @@ -282,7 +291,7 @@ function addenv(cmd::Cmd, env::Dict; inherit::Bool = true)
merge!(new_env, ENV)
end
else
for (k, v) in eachsplit.(cmd.env, "=")
for (k, v) in splitenv.(cmd.env)
new_env[string(k)::String] = string(v)::String
end
end
Expand All @@ -301,7 +310,7 @@ function addenv(cmd::Cmd, pairs::Pair{<:AbstractString}...; inherit::Bool = true
end

function addenv(cmd::Cmd, env::Vector{<:AbstractString}; inherit::Bool = true)
return addenv(cmd, Dict(k => v for (k, v) in eachsplit.(env, "=")); inherit)
return addenv(cmd, Dict(k => v for (k, v) in splitenv.(env)); inherit)
end

"""
Expand Down
6 changes: 6 additions & 0 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,12 @@ end
dir = joinpath(pwd(), "dir")
cmd = addenv(setenv(`julia`; dir=dir), Dict())
@test cmd.dir == dir

@test addenv(``, ["a=b=c"], inherit=false).env == ["a=b=c"]
cmd = addenv(``, "a"=>"b=c", inherit=false)
@test cmd.env == ["a=b=c"]
cmd = addenv(cmd, "b"=>"b")
@test issetequal(cmd.env, ["b=b", "a=b=c"])
end

@testset "setenv with dir (with tests for #42131)" begin
Expand Down

0 comments on commit f5d9b86

Please sign in to comment.