Skip to content

Commit a135ef3

Browse files
authored
Backport addenv (#721)
1 parent 7481eb8 commit a135ef3

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.15.0"
3+
version = "3.16.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ changes in `julia`.
5555

5656
## Supported features
5757

58+
* New function `addenv` for adding environment mappings into a `Cmd` object, returning the new `Cmd` object ([#37244]). (since Compat 3.16)
59+
5860
* `contains(haystack, needle)` and its one argument partially applied form `contains(haystack)` have been added, it acts like `occursin(needle, haystack)` ([#35132]). (since Compat 3.15)
5961

6062
* `startswith(x)` and `endswith(x)`, returning partially-applied versions of the functions, similar to existing methods like `isequal(x)` ([#35052]). (since Compat 3.15)
@@ -197,3 +199,4 @@ Note that you should specify the correct minimum version for `Compat` in the
197199
[#34352]: https://github.com/JuliaLang/julia/pull/34352
198200
[#35132]: https://github.com/JuliaLang/julia/pull/35132
199201
[#35052]: https://github.com/JuliaLang/julia/pull/35052
202+
[#37244]: https://github.com/JuliaLang/julia/pull/37244

src/Compat.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,31 @@ if VERSION < v"1.5.0-DEV.438" # 0a43c0f1d21ce9c647c49111d93927369cd20f85
607607
Base.startswith(s) = Base.Fix2(startswith, s)
608608
end
609609

610+
# https://github.com/JuliaLang/julia/pull/37244
611+
if VERSION < v"1.6.0-DEV.873" # 18198b1bf85125de6cec266eac404d31ccc2e65c
612+
export addenv
613+
function addenv(cmd::Cmd, env::Dict)
614+
new_env = Dict{String,String}()
615+
if cmd.env !== nothing
616+
for (k, v) in split.(cmd.env, "=")
617+
new_env[string(k)::String] = string(v)::String
618+
end
619+
end
620+
for (k, v) in env
621+
new_env[string(k)::String] = string(v)::String
622+
end
623+
return setenv(cmd, new_env)
624+
end
625+
626+
function addenv(cmd::Cmd, pairs::Pair{<:AbstractString}...)
627+
return addenv(cmd, Dict(k => v for (k, v) in pairs))
628+
end
629+
630+
function addenv(cmd::Cmd, env::Vector{<:AbstractString})
631+
return addenv(cmd, Dict(k => v for (k, v) in split.(env, "=")))
632+
end
633+
end
634+
610635
include("iterators.jl")
611636
include("deprecated.jl")
612637

test/runtests.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,34 @@ end
556556
@test endswith("d")("abcd")
557557
end
558558

559+
# From spawn.jl
560+
shcmd = `sh`
561+
havebb = false
562+
if Sys.iswindows()
563+
busybox = download("https://cache.julialang.org/https://frippery.org/files/busybox/busybox.exe", joinpath(tempdir(), "busybox.exe"))
564+
havebb = try # use busybox-w32 on windows, if available
565+
success(`$busybox`)
566+
true
567+
catch
568+
false
569+
end
570+
if havebb
571+
shcmd = `$busybox sh`
572+
end
573+
end
574+
575+
# https://github.com/JuliaLang/julia/pull/37244
576+
@testset "addenv()" begin
577+
cmd = Cmd(`$shcmd -c "echo \$FOO \$BAR"`, env=Dict("FOO" => "foo"))
578+
@test strip(String(read(cmd))) == "foo"
579+
cmd = addenv(cmd, "BAR" => "bar")
580+
@test strip(String(read(cmd))) == "foo bar"
581+
cmd = addenv(cmd, Dict("FOO" => "bar"))
582+
@test strip(String(read(cmd))) == "bar bar"
583+
cmd = addenv(cmd, ["FOO=baz"])
584+
@test strip(String(read(cmd))) == "baz bar"
585+
end
586+
559587
include("iterators.jl")
560588

561589
nothing

0 commit comments

Comments
 (0)