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