Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "TestEnv"
uuid = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
version = "1.103.7"
version = "1.104.0"

[deps]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ TestEnv.activate("Example") do
end
```

## Preferences

On Julia 1.8 and later, preferences are copied into the test environment the same way `Pkg.test` does it: they are taken from `test/LocalPreferences.toml` (or the `[preferences]` section of `test/Project.toml`) if the package has its own test project, and from the package's own project otherwise, with the surrounding `LOAD_PATH` merged in behind them.

On Julia 1.7 and earlier this is not supported -- `Base` there only exposes preferences per-package-UUID, so there is no way to collect the whole set to copy across.

### See also:
- [Discourse Release Announcement](https://discourse.julialang.org/t/ann-testenv-jl-activate-your-test-enviroment-so-you-can-use-your-test-dependencies/65739)
6 changes: 5 additions & 1 deletion src/julia-1.11/activate_do.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function activate(f, pkg::AbstractString=current_pkg_name(); allow_reresolve=tru

test_project_override = maybe_gen_project_override!(ctx, pkgspec)
path = pkgspec.path::String
return sandbox(ctx, pkgspec, joinpath(path, "test"), test_project_override; allow_reresolve) do
preferences = sandbox_preferences(path)
return sandbox(
ctx, pkgspec, joinpath(path, "test"), test_project_override;
preferences, allow_reresolve,
) do
flush(stdout)
f()
end
Expand Down
10 changes: 10 additions & 0 deletions src/julia-1.11/activate_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

sandbox_path = joinpath(pkgspec.path::String, "test")
sandbox_project = projectfile_path(sandbox_path)
preferences = sandbox_preferences(pkgspec.path::String)

tmp = mktempdir()
tmp_project = projectfile_path(tmp)
tmp_manifest = manifestfile_path(tmp)
tmp_preferences = joinpath(tmp, first(Base.preferences_names))

# Copy env info over to temp env
if sandbox_project_override !== nothing
Expand Down Expand Up @@ -51,6 +53,14 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

Types.write_manifest(working_manifest, tmp_manifest)

# Copy the preferences over too, as `Pkg.test` does. We reach for `Pkg.TOML` rather
# than the `TOML` stdlib because TestEnv supports Julia 1.0, which predates it.
if !isempty(preferences)
open(tmp_preferences, "w") do io
Pkg.TOML.print(io, preferences)
end
end

Base.ACTIVE_PROJECT[] = tmp_project

temp_ctx = Context()
Expand Down
31 changes: 31 additions & 0 deletions src/julia-1.11/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ function maybe_gen_project_override!(ctx, pkgspec)
nothing
end
end


"""
sandbox_preferences(source_path::AbstractString)

The preferences that apply inside the test environment of the package rooted at
`source_path`, computed the same way `Pkg.test` does it (see `Pkg.Operations.test`):
they come from the `test/` environment when it has its own project file, and from the
package's own project otherwise, with the rest of the current `LOAD_PATH` merged in
behind them.
"""
function sandbox_preferences(source_path::AbstractString)
test_path = joinpath(source_path, "test")
env = isfile(projectfile_path(test_path)) ? test_path :
something(projectfile_path(source_path))
preferences = nothing
with_load_path([env, Base.LOAD_PATH...]) do
preferences = Base.get_preferences()
end
return preferences::Dict{String,Any}
end

function with_load_path(f::Function, new_load_path::Vector{String})
old_load_path = copy(Base.LOAD_PATH)
copy!(Base.LOAD_PATH, new_load_path)
try
f()
finally
copy!(Base.LOAD_PATH, old_load_path)
end
end
6 changes: 5 additions & 1 deletion src/julia-1.12/activate_do.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function activate(f, pkg::AbstractString=current_pkg_name(); allow_reresolve=tru

test_project_override = maybe_gen_project_override!(ctx, pkgspec)
path = pkgspec.path::String
return sandbox(ctx, pkgspec, joinpath(path, "test"), test_project_override; allow_reresolve) do
preferences = sandbox_preferences(path)
return sandbox(
ctx, pkgspec, joinpath(path, "test"), test_project_override;
preferences, allow_reresolve,
) do
flush(stdout)
f()
end
Expand Down
10 changes: 10 additions & 0 deletions src/julia-1.12/activate_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

sandbox_path = joinpath(pkgspec.path::String, "test")
sandbox_project = projectfile_path(sandbox_path)
preferences = sandbox_preferences(pkgspec.path::String)

tmp = mktempdir()
tmp_project = projectfile_path(tmp)
tmp_manifest = manifestfile_path(tmp)
tmp_preferences = joinpath(tmp, first(Base.preferences_names))

# Copy env info over to temp env
if sandbox_project_override !== nothing
Expand Down Expand Up @@ -51,6 +53,14 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

Types.write_manifest(working_manifest, tmp_manifest)

# Copy the preferences over too, as `Pkg.test` does. We reach for `Pkg.TOML` rather
# than the `TOML` stdlib because TestEnv supports Julia 1.0, which predates it.
if !isempty(preferences)
open(tmp_preferences, "w") do io
Pkg.TOML.print(io, preferences)
end
end

Base.ACTIVE_PROJECT[] = tmp_project

temp_ctx = Context()
Expand Down
31 changes: 31 additions & 0 deletions src/julia-1.12/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ function maybe_gen_project_override!(ctx, pkgspec)
nothing
end
end


"""
sandbox_preferences(source_path::AbstractString)

The preferences that apply inside the test environment of the package rooted at
`source_path`, computed the same way `Pkg.test` does it (see `Pkg.Operations.test`):
they come from the `test/` environment when it has its own project file, and from the
package's own project otherwise, with the rest of the current `LOAD_PATH` merged in
behind them.
"""
function sandbox_preferences(source_path::AbstractString)
test_path = joinpath(source_path, "test")
env = isfile(projectfile_path(test_path)) ? test_path :
something(projectfile_path(source_path))
preferences = nothing
with_load_path([env, Base.LOAD_PATH...]) do
preferences = Base.get_preferences()
end
return preferences::Dict{String,Any}
end

function with_load_path(f::Function, new_load_path::Vector{String})
old_load_path = copy(Base.LOAD_PATH)
copy!(Base.LOAD_PATH, new_load_path)
try
f()
finally
copy!(Base.LOAD_PATH, old_load_path)
end
end
6 changes: 5 additions & 1 deletion src/julia-1.13/activate_do.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function activate(f, pkg::AbstractString=current_pkg_name(); allow_reresolve=tru

test_project_override = maybe_gen_project_override!(ctx, pkgspec)
path = pkgspec.path::String
return sandbox(ctx, pkgspec, joinpath(path, "test"), test_project_override; allow_reresolve) do
preferences = sandbox_preferences(path)
return sandbox(
ctx, pkgspec, joinpath(path, "test"), test_project_override;
preferences, allow_reresolve,
) do
flush(stdout)
f()
end
Expand Down
10 changes: 10 additions & 0 deletions src/julia-1.13/activate_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

sandbox_path = joinpath(pkgspec.path::String, "test")
sandbox_project = projectfile_path(sandbox_path)
preferences = sandbox_preferences(pkgspec.path::String)

tmp = mktempdir()
tmp_project = projectfile_path(tmp)
tmp_manifest = manifestfile_path(tmp)
tmp_preferences = joinpath(tmp, first(Base.preferences_names))

# Copy env info over to temp env
if sandbox_project_override !== nothing
Expand Down Expand Up @@ -51,6 +53,14 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

Types.write_manifest(working_manifest, tmp_manifest)

# Copy the preferences over too, as `Pkg.test` does. We reach for `Pkg.TOML` rather
# than the `TOML` stdlib because TestEnv supports Julia 1.0, which predates it.
if !isempty(preferences)
open(tmp_preferences, "w") do io
Pkg.TOML.print(io, preferences)
end
end

Base.ACTIVE_PROJECT[] = tmp_project

temp_ctx = Context()
Expand Down
31 changes: 31 additions & 0 deletions src/julia-1.13/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ function maybe_gen_project_override!(ctx, pkgspec)
nothing
end
end


"""
sandbox_preferences(source_path::AbstractString)

The preferences that apply inside the test environment of the package rooted at
`source_path`, computed the same way `Pkg.test` does it (see `Pkg.Operations.test`):
they come from the `test/` environment when it has its own project file, and from the
package's own project otherwise, with the rest of the current `LOAD_PATH` merged in
behind them.
"""
function sandbox_preferences(source_path::AbstractString)
test_path = joinpath(source_path, "test")
env = isfile(projectfile_path(test_path)) ? test_path :
something(projectfile_path(source_path))
preferences = nothing
with_load_path([env, Base.LOAD_PATH...]) do
preferences = Base.get_preferences()
end
return preferences::Dict{String,Any}
end

function with_load_path(f::Function, new_load_path::Vector{String})
old_load_path = copy(Base.LOAD_PATH)
copy!(Base.LOAD_PATH, new_load_path)
try
f()
finally
copy!(Base.LOAD_PATH, old_load_path)
end
end
6 changes: 5 additions & 1 deletion src/julia-1.8/activate_do.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function activate(f, pkg::AbstractString=current_pkg_name(); allow_reresolve=tru

test_project_override = maybe_gen_project_override!(ctx, pkgspec)
path = pkgspec.path::String
return sandbox(ctx, pkgspec, path, joinpath(path, "test"), test_project_override; allow_reresolve) do
preferences = sandbox_preferences(path)
return sandbox(
ctx, pkgspec, path, joinpath(path, "test"), test_project_override;
preferences, allow_reresolve,
) do
flush(stdout)
f()
end
Expand Down
10 changes: 10 additions & 0 deletions src/julia-1.8/activate_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

sandbox_path = joinpath(pkgspec.path::String, "test")
sandbox_project = projectfile_path(sandbox_path)
preferences = sandbox_preferences(pkgspec.path::String)

tmp = mktempdir()
tmp_project = projectfile_path(tmp)
tmp_manifest = manifestfile_path(tmp)
tmp_preferences = joinpath(tmp, first(Base.preferences_names))

# Copy env info over to temp env
if sandbox_project_override !== nothing
Expand Down Expand Up @@ -50,6 +52,14 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

Types.write_manifest(working_manifest, tmp_manifest)

# Copy the preferences over too, as `Pkg.test` does. We reach for `Pkg.TOML` rather
# than the `TOML` stdlib because TestEnv supports Julia 1.0, which predates it.
if !isempty(preferences)
open(tmp_preferences, "w") do io
Pkg.TOML.print(io, preferences)
end
end

Base.ACTIVE_PROJECT[] = tmp_project

temp_ctx = Context()
Expand Down
31 changes: 31 additions & 0 deletions src/julia-1.8/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ function maybe_gen_project_override!(ctx, pkgspec)
nothing
end
end


"""
sandbox_preferences(source_path::AbstractString)

The preferences that apply inside the test environment of the package rooted at
`source_path`, computed the same way `Pkg.test` does it (see `Pkg.Operations.test`):
they come from the `test/` environment when it has its own project file, and from the
package's own project otherwise, with the rest of the current `LOAD_PATH` merged in
behind them.
"""
function sandbox_preferences(source_path::AbstractString)
test_path = joinpath(source_path, "test")
env = isfile(projectfile_path(test_path)) ? test_path :
something(projectfile_path(source_path))
preferences = nothing
with_load_path([env, Base.LOAD_PATH...]) do
preferences = Base.get_preferences()
end
return preferences::Dict{String,Any}
end

function with_load_path(f::Function, new_load_path::Vector{String})
old_load_path = copy(Base.LOAD_PATH)
copy!(Base.LOAD_PATH, new_load_path)
try
f()
finally
copy!(Base.LOAD_PATH, old_load_path)
end
end
6 changes: 5 additions & 1 deletion src/julia-1.9/activate_do.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function activate(f, pkg::AbstractString=current_pkg_name(); allow_reresolve=tru

test_project_override = maybe_gen_project_override!(ctx, pkgspec)
path = pkgspec.path::String
return sandbox(ctx, pkgspec, path, joinpath(path, "test"), test_project_override; allow_reresolve) do
preferences = sandbox_preferences(path)
return sandbox(
ctx, pkgspec, path, joinpath(path, "test"), test_project_override;
preferences, allow_reresolve,
) do
flush(stdout)
f()
end
Expand Down
10 changes: 10 additions & 0 deletions src/julia-1.9/activate_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

sandbox_path = joinpath(pkgspec.path::String, "test")
sandbox_project = projectfile_path(sandbox_path)
preferences = sandbox_preferences(pkgspec.path::String)

tmp = mktempdir()
tmp_project = projectfile_path(tmp)
tmp_manifest = manifestfile_path(tmp)
tmp_preferences = joinpath(tmp, first(Base.preferences_names))

# Copy env info over to temp env
if sandbox_project_override !== nothing
Expand Down Expand Up @@ -50,6 +52,14 @@ function activate(pkg::AbstractString=current_pkg_name(); allow_reresolve=true)

Types.write_manifest(working_manifest, tmp_manifest)

# Copy the preferences over too, as `Pkg.test` does. We reach for `Pkg.TOML` rather
# than the `TOML` stdlib because TestEnv supports Julia 1.0, which predates it.
if !isempty(preferences)
open(tmp_preferences, "w") do io
Pkg.TOML.print(io, preferences)
end
end

Base.ACTIVE_PROJECT[] = tmp_project

temp_ctx = Context()
Expand Down
31 changes: 31 additions & 0 deletions src/julia-1.9/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,34 @@ function maybe_gen_project_override!(ctx, pkgspec)
nothing
end
end


"""
sandbox_preferences(source_path::AbstractString)

The preferences that apply inside the test environment of the package rooted at
`source_path`, computed the same way `Pkg.test` does it (see `Pkg.Operations.test`):
they come from the `test/` environment when it has its own project file, and from the
package's own project otherwise, with the rest of the current `LOAD_PATH` merged in
behind them.
"""
function sandbox_preferences(source_path::AbstractString)
test_path = joinpath(source_path, "test")
env = isfile(projectfile_path(test_path)) ? test_path :
something(projectfile_path(source_path))
preferences = nothing
with_load_path([env, Base.LOAD_PATH...]) do
preferences = Base.get_preferences()
end
return preferences::Dict{String,Any}
end

function with_load_path(f::Function, new_load_path::Vector{String})
old_load_path = copy(Base.LOAD_PATH)
copy!(Base.LOAD_PATH, new_load_path)
try
f()
finally
copy!(Base.LOAD_PATH, old_load_path)
end
end
Loading
Loading