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
0 commit comments