Skip to content

Commit 236d37a

Browse files
committed
Fully align code structure with other branchs
1 parent be7df16 commit 236d37a

8 files changed

+117
-121
lines changed

src/TestEnv.jl

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
module TestEnv
22
using Pkg
33
using Pkg: PackageSpec
4-
using Pkg.Types: Context, ensure_resolved, is_project_uuid, write_env
4+
using Pkg.Types: Context, ensure_resolved, is_project_uuid, write_env, is_stdlib
5+
using Pkg.Types: Types, projectfile_path, manifestfile_path
56
using Pkg.Operations: manifest_info, manifest_resolve!, project_deps_resolve!
67
using Pkg.Operations: project_rel_path, project_resolve!
7-
8-
using Pkg.Types: Types, projectfile_path, manifestfile_path
9-
using Pkg.Operations: gen_target_project
10-
using Pkg.Operations: update_package_test!
11-
using Pkg.Types: is_stdlib
128
using Pkg.Operations: sandbox, source_path, sandbox_preserve, abspath!
9+
using Pkg.Operations: gen_target_project, update_package_test!
1310

14-
15-
include("exceptions.jl")
16-
17-
include("activate.jl")
18-
include("make_test_env.jl")
19-
include("sandbox.jl")
20-
include("test_dir.jl")
11+
include("common.jl")
12+
include("activate_do.jl")
13+
include("activate_set.jl")
2114

2215
end

src/activate.jl

-58
This file was deleted.

src/activate_do.jl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TestEnv.activate(f, [pkg])
3+
4+
Activate the test enviroment of `pkg` (defaults to current enviroment), and run `f()`,
5+
then deactivate the enviroment.
6+
This is not useful for many people: Julia is not really designed to have the enviroment
7+
being changed while you are executing code.
8+
However, this *is* useful for anyone doing something like making a alternative to
9+
`Pkg.test()`.
10+
Indeed this is basically extracted from what `Pkg.test()` does.
11+
"""
12+
function activate(f, pkg::AbstractString=current_pkg_name())
13+
ctx, pkgspec = ctx_and_pkgspec(pkg)
14+
test_project_override = maybe_gen_project_override!(ctx, pkgspec)
15+
return sandbox(ctx, pkgspec, pkgspec.path, joinpath(pkgspec.path, "test"), test_project_override) do
16+
flush(stdout)
17+
f()
18+
end
19+
end

src/make_test_env.jl src/activate_set.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
"""
2+
TestEnv.activate([pkg])
13
2-
# Originally from Pkg.Operations.sandbox
3-
function make_test_env(ctx::Context, target)
4-
# This needs to be first as `gen_target_project` fixes `target.path` if it is nothing
5-
sandbox_project_override = if !test_dir_has_project_file(ctx, target)
6-
sandbox_project_override = gen_target_project(ctx, target, target.path, "test")
7-
else
8-
nothing
9-
end
4+
Activate the test enviroment of `pkg` (defaults to current enviroment).
5+
"""
6+
function activate(pkg::AbstractString=current_pkg_name())
7+
ctx, pkgspec = ctx_and_pkgspec(pkg)
8+
# This needs to be first as `gen_target_project` fixes `pkgspec.path` if it is nothing
9+
sandbox_project_override = maybe_gen_project_override!(ctx, pkgspec)
1010

11-
sandbox_path = joinpath(target.path, "test")
11+
sandbox_path = joinpath(pkgspec.path, "test")
1212
sandbox_project = projectfile_path(sandbox_path)
1313

1414
tmp = mktempdir()
@@ -25,7 +25,7 @@ function make_test_env(ctx::Context, target)
2525
# create merged manifest
2626
# - copy over active subgraph
2727
# - abspath! to maintain location of all deved nodes
28-
working_manifest = abspath!(ctx, sandbox_preserve(ctx, target, tmp_project))
28+
working_manifest = abspath!(ctx, sandbox_preserve(ctx, pkgspec, tmp_project))
2929

3030
# - copy over fixed subgraphs from test subgraph
3131
# really only need to copy over "special" nodes
@@ -52,7 +52,7 @@ function make_test_env(ctx::Context, target)
5252
Base.ACTIVE_PROJECT[] = nothing
5353

5454
temp_ctx = Context()
55-
temp_ctx.env.project.deps[target.name] = target.uuid
55+
temp_ctx.env.project.deps[pkgspec.name] = pkgspec.uuid
5656

5757
try
5858
Pkg.resolve(temp_ctx; io=devnull)

src/common.jl

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
struct TestEnvError <: Exception
2+
msg::AbstractString
3+
end
4+
5+
function Base.showerror(io::IO, ex::TestEnvError, bt; backtrace=true)
6+
printstyled(io, ex.msg, color=Base.error_color())
7+
end
8+
9+
10+
current_pkg_name() = Context().env.pkg.name
11+
12+
"""
13+
ctx, pkgspec = ctx_and_pkgspec(pkg::AbstractString)
14+
15+
For a given package name `pkg`, instantiate a `Context` for it, and return that `Context`,
16+
and it's `PackageSpec`.
17+
"""
18+
function ctx_and_pkgspec(pkg::AbstractString)
19+
pkgspec = deepcopy(PackageSpec(pkg))
20+
ctx = Context()
21+
isinstalled!(ctx, pkgspec) || throw(TestEnvError("$pkg not installed 👻"))
22+
Pkg.instantiate(ctx)
23+
return ctx, pkgspec
24+
end
25+
26+
27+
"""
28+
isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
29+
30+
Checks if the package is installed by using `ensure_resolved` from `Pkg/src/Types.jl`.
31+
This function fails if the package is not installed, but here we wrap it in a
32+
try-catch as we may want to test another package after the one that isn't installed.
33+
34+
For Julia versions V1.4 and later, the first arguments of the Pkg functions used
35+
is of type `Pkg.Types.Context`. For earlier versions, they are of type
36+
`Pkg.Types.EnvCache`.
37+
"""
38+
function isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
39+
project_resolve!(ctx, [pkgspec])
40+
project_deps_resolve!(ctx, [pkgspec])
41+
manifest_resolve!(ctx, [pkgspec])
42+
43+
try
44+
ensure_resolved(ctx, [pkgspec])
45+
catch err
46+
err isa MethodError && rethrow()
47+
return false
48+
end
49+
return true
50+
end
51+
52+
function test_dir_has_project_file(ctx, pkgspec)
53+
return isfile(joinpath(get_test_dir(ctx, pkgspec), "Project.toml"))
54+
end
55+
56+
"""
57+
get_test_dir(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
58+
59+
Gets the testfile path of the package. Code for each Julia version mirrors that found
60+
in `Pkg/src/Operations.jl`.
61+
"""
62+
function get_test_dir(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
63+
if is_project_uuid(ctx, pkgspec.uuid)
64+
pkgspec.path = dirname(ctx.env.project_file)
65+
pkgspec.version = ctx.env.pkg.version
66+
else
67+
update_package_test!(pkgspec, manifest_info(ctx, pkgspec.uuid))
68+
pkgspec.path = project_rel_path(ctx, source_path(ctx, pkgspec))
69+
end
70+
pkgfilepath = source_path(ctx, pkgspec)
71+
return joinpath(pkgfilepath, "test")
72+
end
73+
74+
75+
function maybe_gen_project_override!(ctx, pkgspec)
76+
if !test_dir_has_project_file(ctx, pkgspec)
77+
sandbox_project_override = gen_target_project(ctx, pkgspec, pkgspec.path, "test")
78+
else
79+
nothing
80+
end
81+
end

src/exceptions.jl

-7
This file was deleted.

src/sandbox.jl

-11
This file was deleted.

src/test_dir.jl

-21
This file was deleted.

0 commit comments

Comments
 (0)