Skip to content

Commit 8d8c90c

Browse files
authored
Merge pull request #8 from JuliaTesting/ox/initial-1.7
Make a 1.7 only branch/release
2 parents f3c8c7d + 291c4b4 commit 8d8c90c

15 files changed

+258
-410
lines changed

.github/workflows/CI.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
version:
13-
- '1.4'
14-
- '1.5'
15-
- '1.6'
16-
# - '1.7-nightly' works last i checked but not final yet
13+
- '1.7-nightly'
1714
os:
1815
- ubuntu-latest
1916
arch:

Project.toml

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
name = "TestEnv"
22
uuid = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
3-
version = "1.4.0"
3+
version = "1.7.0"
44

55
[deps]
66
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
77

88
[compat]
99
ChainRulesCore = "=1.0.2"
10-
julia = "~1.4, ~1.5, ~1.6"
10+
MCMCDiagnosticTools = "=0.1.0"
11+
YAXArrays = "0.1.3"
12+
julia = "~1.7"
1113

1214
[extras]
1315
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
16+
MCMCDiagnosticTools = "be115224-59cd-429b-ad48-344e309966f0"
1417
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18+
YAXArrays = "c21b50f5-aa40-41ea-b809-c0f5e47bfa5c"
1519

1620
[targets]
17-
test = ["ChainRulesCore", "Test"]
21+
test = ["ChainRulesCore", "MCMCDiagnosticTools", "Test", "YAXArrays"]

src/TestEnv.jl

+7-36
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,16 @@
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!
8+
using Pkg.Operations: sandbox, source_path, sandbox_preserve, abspath!
9+
using Pkg.Operations: gen_target_project
710

8-
using Pkg.Types: Types, projectfile_path, manifestfile_path
9-
10-
# Version specific imports
11-
@static if VERSION >= v"1.4.0"
12-
using Pkg.Operations: gen_target_project
13-
else
14-
using Pkg.Operations: with_dependencies_loadable_at_toplevel
15-
end
16-
@static if isdefined(Pkg.Operations, :update_package_test!)
17-
using Pkg.Operations: update_package_test!
18-
else
19-
function update_package_test!(pkg, entry)
20-
is_stdlib(pkg.uuid) && return
21-
pkg.version = entry.version
22-
pkg.tree_hash = entry.tree_hash
23-
pkg.repo = entry.repo
24-
pkg.path = entry.path
25-
pkg.pinned = entry.pinned
26-
end
27-
end
28-
29-
@static if VERSION >= v"1.2.0"
30-
using Pkg.Types: is_stdlib
31-
using Pkg.Operations: sandbox, source_path, sandbox_preserve, abspath!
32-
else
33-
using Pkg.Operations: find_installed
34-
using Pkg.Types: SHA1
35-
end
36-
37-
38-
include("exceptions.jl")
3911

40-
include("activate.jl")
41-
include("make_test_env.jl")
42-
include("sandbox.jl")
43-
include("test_dir.jl")
12+
include("common.jl")
13+
include("activate_do.jl")
14+
include("activate_set.jl")
4415

4516
end

src/activate.jl

-69
This file was deleted.

src/activate_do.jl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
15+
test_project_override = maybe_gen_project_override!(ctx, pkgspec)
16+
return sandbox(ctx, pkgspec, pkgspec.path, joinpath(pkgspec.path, "test"), test_project_override) do
17+
flush(stdout)
18+
f()
19+
end
20+
end

src/activate_set.jl

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
# Originally from Pkg.Operations.sandbox
3+
4+
"""
5+
TestEnv.activate([pkg])
6+
7+
Activate the test enviroment of `pkg` (defaults to current enviroment).
8+
"""
9+
function activate(pkg::AbstractString=current_pkg_name())
10+
ctx, pkgspec = ctx_and_pkgspec(pkg)
11+
# This needs to be first as `gen_target_project` fixes `pkgspec.path` if it is nothing
12+
sandbox_project_override = maybe_gen_project_override!(ctx, pkgspec)
13+
14+
sandbox_path = joinpath(pkgspec.path, "test")
15+
sandbox_project = projectfile_path(sandbox_path)
16+
17+
tmp = mktempdir()
18+
tmp_project = projectfile_path(tmp)
19+
tmp_manifest = manifestfile_path(tmp)
20+
21+
# Copy env info over to temp env
22+
if sandbox_project_override !== nothing
23+
Types.write_project(sandbox_project_override, tmp_project)
24+
elseif isfile(sandbox_project)
25+
cp(sandbox_project, tmp_project)
26+
chmod(tmp_project, 0o600)
27+
end
28+
# create merged manifest
29+
# - copy over active subgraph
30+
# - abspath! to maintain location of all deved nodes
31+
working_manifest = abspath!(ctx.env, sandbox_preserve(ctx.env, pkgspec, tmp_project))
32+
33+
# - copy over fixed subgraphs from test subgraph
34+
# really only need to copy over "special" nodes
35+
sandbox_env = Types.EnvCache(projectfile_path(sandbox_path))
36+
sandbox_manifest = abspath!(sandbox_env, sandbox_env.manifest)
37+
for (name, uuid) in sandbox_env.project.deps
38+
entry = get(sandbox_manifest, uuid, nothing)
39+
if entry !== nothing && isfixed(entry)
40+
subgraph = prune_manifest(sandbox_manifest, [uuid])
41+
for (uuid, entry) in subgraph
42+
if haskey(working_manifest, uuid)
43+
pkgerror("can not merge projects")
44+
end
45+
working_manifest[uuid] = entry
46+
end
47+
end
48+
end
49+
50+
Types.write_manifest(working_manifest, tmp_manifest)
51+
52+
# sandbox
53+
push!(empty!(LOAD_PATH), "@", tmp)
54+
Base.ACTIVE_PROJECT[] = nothing
55+
56+
temp_ctx = Context()
57+
temp_ctx.env.project.deps[pkgspec.name] = pkgspec.uuid
58+
59+
try
60+
Pkg.resolve(temp_ctx; io=devnull)
61+
@debug "Using _parent_ dep graph"
62+
catch err# TODO
63+
@debug err
64+
@warn "Could not use exact versions of packages in manifest, re-resolving"
65+
temp_ctx.env.manifest.deps = Dict(
66+
uuid => entry for
67+
(uuid, entry) in temp_ctx.env.manifest.deps if isfixed(entry)
68+
)
69+
Pkg.resolve(temp_ctx; io=devnull)
70+
@debug "Using _clean_ dep graph"
71+
end
72+
73+
# Absolutify stdlibs paths
74+
for (uuid, entry) in temp_ctx.env.manifest
75+
if is_stdlib(uuid)
76+
entry.path = Types.stdlib_path(entry.name)
77+
end
78+
end
79+
write_env(temp_ctx.env; update_undo=false)
80+
81+
# update enviroment variables
82+
path_sep = Sys.iswindows() ? ';' : ':'
83+
ENV["JULIA_LOAD_PATH"] = "@$(path_sep)$(tmp)"
84+
delete!(ENV, "JULIA_PROJECT")
85+
86+
return Base.active_project()
87+
end

src/common.jl

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
28+
29+
Checks if the package is installed by using `ensure_resolved` from `Pkg/src/Types.jl`.
30+
This function fails if the package is not installed, but here we wrap it in a
31+
try-catch as we may want to test another package after the one that isn't installed.
32+
"""
33+
function isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
34+
project_resolve!(ctx.env, [pkgspec])
35+
project_deps_resolve!(ctx.env, [pkgspec])
36+
manifest_resolve!(ctx.env.manifest, [pkgspec])
37+
38+
try
39+
ensure_resolved(ctx.env.manifest, [pkgspec])
40+
catch err
41+
err isa MethodError && rethrow()
42+
return false
43+
end
44+
return true
45+
end
46+
47+
48+
function test_dir_has_project_file(ctx, pkgspec)
49+
return isfile(joinpath(get_test_dir(ctx, pkgspec), "Project.toml"))
50+
end
51+
52+
"""
53+
get_test_dir(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
54+
55+
Gets the testfile path of the package. Code for each Julia version mirrors that found
56+
in `Pkg/src/Operations.jl`.
57+
"""
58+
function get_test_dir(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
59+
if is_project_uuid(ctx.env, pkgspec.uuid)
60+
pkgspec.path = dirname(ctx.env.project_file)
61+
pkgspec.version = ctx.env.pkg.version
62+
else
63+
is_stdlib(pkgspec.uuid) && return
64+
entry = manifest_info(ctx.env.manifest, pkgspec.uuid)
65+
pkgspec.version = entry.version
66+
pkgspec.tree_hash = entry.tree_hash
67+
pkgspec.repo = entry.repo
68+
pkgspec.path = entry.path
69+
pkgspec.pinned = entry.pinned
70+
pkgspec.path = project_rel_path(ctx.env, source_path(ctx.env.project_file, pkgspec))
71+
end
72+
pkgfilepath = source_path(ctx.env.project_file, pkgspec)
73+
return joinpath(pkgfilepath, "test")
74+
end
75+
76+
77+
function maybe_gen_project_override!(ctx, pkgspec)
78+
if !test_dir_has_project_file(ctx, pkgspec)
79+
gen_target_project(ctx.env, ctx.registries, pkgspec, pkgspec.path, "test")
80+
else
81+
nothing
82+
end
83+
end

src/exceptions.jl

-7
This file was deleted.

0 commit comments

Comments
 (0)