Skip to content

Commit e2940cb

Browse files
authored
Delete keys with value nothing in addenv. (#43271)
1 parent cbc2ce8 commit e2940cb

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ New library features
6969
* `@testset foo()` can now be used to create a test set from a given function. The name of the test set
7070
is the name of the called function. The called function can contain `@test` and other `@testset`
7171
definitions, including to other function calls, while recording all intermediate test results. ([#42518])
72+
* Keys with value `nothing` are now removed from the environment in `addenv` ([#43271]).
7273

7374
Standard library changes
7475
------------------------

base/cmd.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ setenv(cmd::Cmd; dir="") = Cmd(cmd; dir=dir)
256256
Merge new environment mappings into the given [`Cmd`](@ref) object, returning a new `Cmd` object.
257257
Duplicate keys are replaced. If `command` does not contain any environment values set already,
258258
it inherits the current environment at time of `addenv()` call if `inherit` is `true`.
259+
Keys with value `nothing` are deleted from the env.
259260
260261
See also [`Cmd`](@ref), [`setenv`](@ref), [`ENV`](@ref).
261262
@@ -274,7 +275,11 @@ function addenv(cmd::Cmd, env::Dict; inherit::Bool = true)
274275
end
275276
end
276277
for (k, v) in env
277-
new_env[string(k)::String] = string(v)::String
278+
if v === nothing
279+
delete!(new_env, string(k)::String)
280+
else
281+
new_env[string(k)::String] = string(v)::String
282+
end
278283
end
279284
return setenv(cmd, new_env)
280285
end

test/spawn.jl

+4
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,10 @@ end
817817
cmd2 = addenv(cmd, "FOO" => "foo2", "BAR" => "bar"; inherit=true)
818818
@test strip(String(read(cmd2))) == "foo2 bar"
819819
end
820+
# Keys with value === nothing are deleted
821+
cmd = Cmd(`$shcmd -c "echo \$FOO \$BAR"`, env=Dict("FOO" => "foo", "BAR" => "bar"))
822+
cmd2 = addenv(cmd, "FOO" => nothing)
823+
@test strip(String(read(cmd2))) == "bar"
820824
end
821825

822826

0 commit comments

Comments
 (0)