Skip to content

Commit b48104d

Browse files
authored
update loading test to best-practices (#48429)
Many of these test were hiding output or using semi-deprecated functions or failing to cleanup output or setting the wrong variables.
1 parent 94c4fb5 commit b48104d

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

test/loading.jl

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,10 @@ end
673673
cd("foo")
674674
@test Base.active_project() == old
675675
"""
676-
@test success(`$(Base.julia_cmd()) --startup-file=no --project=foo -e $(script)`)
677-
withenv("JULIA_PROJECT" => "foo") do
678-
@test success(`$(Base.julia_cmd()) --startup-file=no -e $(script)`)
679-
end
676+
cmd = `$(Base.julia_cmd()) --startup-file=no -e $(script)`
677+
cmd = addenv(cmd, "JULIA_PROJECT" => "foo")
678+
cmd = pipeline(cmd; stdout, stderr)
679+
@test success(cmd)
680680
end; end
681681
end
682682

@@ -689,15 +689,16 @@ mktempdir() do dir
689689
vdir = vdir[2:end] # remove @
690690
vpath = joinpath(dir, "environments", vdir)
691691
mkpath(vpath)
692-
withenv("JULIA_DEPOT_PATH" => dir) do
693-
script = "@assert startswith(Base.active_project(), $(repr(vpath)))"
694-
@test success(`$(Base.julia_cmd()) --startup-file=no -e $(script)`)
695-
end
692+
script = "@assert startswith(Base.active_project(), $(repr(vpath)))"
693+
cmd = `$(Base.julia_cmd()) --startup-file=no -e $(script)`
694+
cmd = addenv(cmd, "JULIA_DEPOT_PATH" => dir)
695+
cmd = pipeline(cmd; stdout, stderr)
696+
@test success(cmd)
696697
end
697698

698699
@testset "expansion of JULIA_LOAD_PATH" begin
699700
s = Sys.iswindows() ? ';' : ':'
700-
tmp = "/foo/bar"
701+
tmp = "/this/does/not/exist"
701702
cases = Dict{Any,Vector{String}}(
702703
nothing => Base.DEFAULT_LOAD_PATH,
703704
"" => [],
@@ -706,16 +707,17 @@ end
706707
"$s$tmp" => [Base.DEFAULT_LOAD_PATH; tmp],
707708
)
708709
for (env, result) in pairs(cases)
709-
withenv("JULIA_LOAD_PATH" => env) do
710-
script = "LOAD_PATH == $(repr(result)) || error()"
711-
@test success(`$(Base.julia_cmd()) --startup-file=no -e $script`)
712-
end
710+
script = "LOAD_PATH == $(repr(result)) || error()"
711+
cmd = `$(Base.julia_cmd()) --startup-file=no -e $script`
712+
cmd = addenv(cmd, "JULIA_LOAD_PATH" => env)
713+
cmd = pipeline(cmd; stdout, stderr)
714+
@test success(cmd)
713715
end
714716
end
715717

716718
@testset "expansion of JULIA_DEPOT_PATH" begin
717719
s = Sys.iswindows() ? ';' : ':'
718-
tmp = "/foo/bar"
720+
tmp = "/this/does/not/exist"
719721
DEFAULT = Base.append_default_depot_path!(String[])
720722
cases = Dict{Any,Vector{String}}(
721723
nothing => DEFAULT,
@@ -725,10 +727,11 @@ end
725727
"$s$tmp" => [DEFAULT; tmp],
726728
)
727729
for (env, result) in pairs(cases)
728-
withenv("JULIA_DEPOT_PATH" => env) do
729-
script = "DEPOT_PATH == $(repr(result)) || error()"
730-
@test success(`$(Base.julia_cmd()) --startup-file=no -e $script`)
731-
end
730+
script = "DEPOT_PATH == $(repr(result)) || error()"
731+
cmd = `$(Base.julia_cmd()) --startup-file=no -e $script`
732+
cmd = addenv(cmd, "JULIA_DEPOT_PATH" => env)
733+
cmd = pipeline(cmd; stdout, stderr)
734+
@test success(cmd)
732735
end
733736
end
734737

@@ -996,15 +999,14 @@ end
996999
end
9971000

9981001
@testset "Extensions" begin
999-
old_depot_path = copy(DEPOT_PATH)
1002+
depot_path = mktempdir()
10001003
try
1001-
tmp = mktempdir()
1002-
push!(empty!(DEPOT_PATH), joinpath(tmp, "depot"))
10031004
proj = joinpath(@__DIR__, "project", "Extensions", "HasDepWithExtensions.jl")
10041005

10051006
function gen_extension_cmd(compile)
10061007
```$(Base.julia_cmd()) $compile --startup-file=no -e '
10071008
begin
1009+
push!(empty!(DEPOT_PATH), '$(repr(depot_path))')
10081010
using HasExtensions
10091011
# Base.get_extension(HasExtensions, :Extension) === nothing || error("unexpectedly got an extension")
10101012
HasExtensions.ext_loaded && error("ext_loaded set")
@@ -1020,21 +1022,25 @@ end
10201022
```
10211023
end
10221024

1023-
for compile in (`--compiled-modules=no`, ``, ``) # Once when requiring precomilation, once where it is already precompiled
1025+
for compile in (`--compiled-modules=no`, ``, ``) # Once when requiring precompilation, once where it is already precompiled
10241026
cmd = gen_extension_cmd(compile)
1025-
withenv("JULIA_LOAD_PATH" => proj) do
1026-
@test success(cmd)
1027-
end
1027+
cmd = addenv(cmd, "JULIA_LOAD_PATH" => proj)
1028+
cmd = pipeline(cmd; stdout, stderr)
1029+
@test success(cmd)
10281030
end
10291031

10301032
# 48351
10311033
sep = Sys.iswindows() ? ';' : ':'
1032-
withenv("JULIA_LOAD_PATH" => join([mktempdir(), proj], sep)) do
1033-
cmd = gen_extension_cmd(``)
1034-
@test success(cmd)
1035-
end
1034+
cmd = gen_extension_cmd(``)
1035+
cmd = addenv(cmd, "JULIA_LOAD_PATH" => join([mktempdir(), proj], sep))
1036+
cmd = pipeline(cmd; stdout, stderr)
1037+
@test success(cmd)
10361038
finally
1037-
copy!(DEPOT_PATH, old_depot_path)
1039+
try
1040+
rm(depot_path, force=true, recursive=true)
1041+
catch err
1042+
@show err
1043+
end
10381044
end
10391045
end
10401046

@@ -1075,7 +1081,7 @@ end
10751081
end
10761082
"""
10771083
cmd = `$julia $(pkgimage(P)) $(opt_level(O)) $(debug_level(D)) $(check_bounds(C)) $(inline(I)) -e $script`
1078-
@test success(pipeline(cmd; stderr))
1084+
@test success(pipeline(cmd; stdout, stderr))
10791085
end
10801086
end
10811087

0 commit comments

Comments
 (0)