@@ -191,126 +191,6 @@ function gen_command(runner_code, julia_args, coverage)
191
191
return cmd
192
192
end
193
193
194
- """
195
- isinstalled!(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
196
-
197
- Checks if the package is installed by using `ensure_resolved` from `Pkg/src/Types.jl`.
198
- This function fails if the package is not installed, but here we wrap it in a
199
- try-catch as we may want to test another package after the one that isn't installed.
200
-
201
- For Julia versions V1.4 and later, the first arguments of the Pkg functions used
202
- is of type `Pkg.Types.Context`. For earlier versions, they are of type
203
- `Pkg.Types.EnvCache`.
204
- """
205
- function isinstalled! (ctx:: Context , pkgspec:: Pkg.Types.PackageSpec )
206
- @static if v " 1.4.0" <= VERSION < v " 1.7.0"
207
- var = ctx
208
- else
209
- var = ctx. env
210
- end
211
- @static if VERSION >= v " 1.7.0"
212
- manifest_var = ctx. env. manifest
213
- else
214
- manifest_var = var
215
- end
216
- project_resolve! (var, [pkgspec])
217
- project_deps_resolve! (var, [pkgspec])
218
- manifest_resolve! (manifest_var, [pkgspec])
219
- try
220
- @static if VERSION >= v " 1.8.0"
221
- ensure_resolved (ctx, manifest_var, [pkgspec])
222
- else
223
- ensure_resolved (manifest_var, [pkgspec])
224
- end
225
- catch err
226
- err isa MethodError && rethrow ()
227
- return false
228
- end
229
- return true
230
- end
231
-
232
- """
233
- gettestfilepath(ctx::Context, pkgspec::Pkg.Types.PackageSpec)
234
-
235
- Gets the testfile path of the package. Code for each Julia version mirrors that found
236
- in `Pkg/src/Operations.jl`.
237
- """
238
- function gettestfilepath (ctx:: Context , pkgspec:: Pkg.Types.PackageSpec )
239
- @static if VERSION >= v " 1.7.0"
240
- if is_project_uuid (ctx. env, pkgspec. uuid)
241
- pkgspec. path = dirname (ctx. env. project_file)
242
- pkgspec. version = ctx. env. pkg. version
243
- else ! Pkg. Operations. is_stdlib (pkgspec. uuid)
244
- entry = manifest_info (ctx. env. manifest, pkgspec. uuid)
245
- pkgspec. version = entry. version
246
- pkgspec. tree_hash = entry. tree_hash
247
- pkgspec. repo = entry. repo
248
- pkgspec. path = entry. path
249
- pkgspec. pinned = entry. pinned
250
- if isnothing (pkgspec. path)
251
- pkgspec. path = source_path (ctx. env. project_file, pkgspec, ctx. julia_version)
252
- end
253
- end
254
- pkgfilepath = source_path (ctx. env. project_file, pkgspec, ctx. julia_version)
255
- elseif VERSION >= v " 1.4.0"
256
- if is_project_uuid (ctx, pkgspec. uuid)
257
- pkgspec. path = dirname (ctx. env. project_file)
258
- pkgspec. version = ctx. env. pkg. version
259
- else
260
- update_package_test! (pkgspec, manifest_info (ctx, pkgspec. uuid))
261
- pkgspec. path = project_rel_path (ctx, source_path (ctx, pkgspec))
262
- end
263
- pkgfilepath = source_path (ctx, pkgspec)
264
- elseif VERSION >= v " 1.2.0"
265
- pkgspec. special_action = Pkg. Types. PKGSPEC_TESTED
266
- if is_project_uuid (ctx. env, pkgspec. uuid)
267
- pkgspec. path = dirname (ctx. env. project_file)
268
- pkgspec. version = ctx. env. pkg. version
269
- else
270
- update_package_test! (pkgspec, manifest_info (ctx. env, pkgspec. uuid))
271
- pkgspec. path = joinpath (project_rel_path (ctx, source_path (pkgspec)))
272
- end
273
- pkgfilepath = project_rel_path (ctx, source_path (pkgspec))
274
- elseif VERSION >= v " 1.1.0"
275
- pkgspec. special_action = Pkg. Types. PKGSPEC_TESTED
276
- if is_project_uuid (ctx. env, pkgspec. uuid)
277
- pkgspec. version = ctx. env. pkg. version
278
- pkgfilepath = dirname (ctx. env. project_file)
279
- else
280
- entry = manifest_info (ctx. env, pkg. uuid)
281
- if entry. repo. tree_sha != = nothing
282
- pkgfilepath = find_installed (pkgspec. name, pkgspec. uuid, entry. repo. tree_sha)
283
- elseif entry. path != = nothing
284
- pkgfilepath = project_rel_path (ctx, entry. path)
285
- elseif pkgspec. uuid in keys (ctx. stdlibs)
286
- pkgfilepath = Pkg. Types. stdlib_path (pkgspec. name)
287
- else
288
- throw (PkgTestError (" Could not find either `git-tree-sha1` or `path` for package $(pkgspec. name) " ))
289
- end
290
- end
291
- else
292
- pkgspec. special_action = Pkg. Types. PKGSPEC_TESTED
293
- if is_project_uuid (ctx. env, pkgspec. uuid)
294
- pkgspec. version = ctx. env. pkg. version
295
- pkgfilepath = dirname (ctx. env. project_file)
296
- else
297
- info = manifest_info (ctx. env, pkgspec. uuid)
298
- if haskey (info, " git-tree-sha1" )
299
- pkgfilepath = find_installed (pkgspec. name, pkgspec. uuid, SHA1 (info[" git-tree-sha1" ]))
300
- elseif haskey (info, " path" )
301
- pkgfilepath = project_rel_path (ctx, info[" path" ])
302
- elseif pkgspec. uuid in keys (ctx. stdlibs)
303
- pkgfilepath = Pkg. Types. stdlib_path (pkgspec. name)
304
- else
305
- throw (PkgTestError (" Could not find either `git-tree-sha1` or `path` for package $(pkgspec. name) " ))
306
- end
307
- end
308
- pkgspec. path = pkgfilepath
309
- end
310
- testfilepath = joinpath (pkgfilepath, " test" , " runtests.jl" )
311
- return testfilepath
312
- end
313
-
314
194
test_project_filepath (testfilepath) = joinpath (dirname (testfilepath), " Project.toml" )
315
195
has_test_project_file (testfilepath) = isfile (test_project_filepath (testfilepath))
316
196
@@ -375,60 +255,26 @@ function test!(pkg::AbstractString,
375
255
# Copied from Pkg.test approach
376
256
julia_args = Cmd (julia_args)
377
257
test_args = Cmd (test_args)
378
- pkgspec = deepcopy (PackageSpec (pkg))
379
- ctx = Context ()
380
-
381
- if ! isinstalled! (ctx, pkgspec)
382
- push! (nopkgs, pkgspec. name)
383
- return
384
- end
385
- Pkg. instantiate (ctx)
386
- testfilepath = gettestfilepath (ctx, pkgspec)
387
-
258
+ ctx, pkgspec = try
259
+ TestEnv. ctx_and_pkgspec (pkg) # TODO : Don't use TestEnv internals
260
+ catch err
261
+ if err isa TestEnv. TestEnvError
262
+ push! (nopkgs, pkg)
263
+ return
264
+ else
265
+ rethrow ()
266
+ end
267
+ end
268
+ testfilepath = joinpath (TestEnv. get_test_dir (ctx, pkgspec), " runtests.jl" )
388
269
check_testreports_compatability (ctx, pkgspec, testfilepath)
389
270
390
271
if ! isfile (testfilepath)
391
272
push! (notests, pkg)
392
273
else
393
274
runner_code = gen_runner_code (testfilepath, logfilename, test_args)
394
275
cmd = gen_command (runner_code, julia_args, coverage)
395
- test_folder_has_project_file = has_test_project_file (testfilepath)
396
-
397
- if VERSION >= v " 1.4" || (VERSION >= v " 1.2" && test_folder_has_project_file)
398
- # Operations.sandbox() has different arguments between versions
399
- test_project_override = if VERSION >= v " 1.4" && ! test_folder_has_project_file
400
- if VERSION >= v " 1.8"
401
- gen_target_project (ctx, pkgspec, pkgspec. path:: String , " test" )
402
- elseif VERSION >= v " 1.7"
403
- gen_target_project (ctx. env, ctx. registries, pkgspec, pkgspec. path, " test" )
404
- else
405
- gen_target_project (ctx, pkgspec, pkgspec. path, " test" )
406
- end
407
- else
408
- nothing
409
- end
410
-
411
- sandbox_args = if VERSION >= v " 1.11-"
412
- (ctx, pkgspec, joinpath (pkgspec. path, " test" ), test_project_override)
413
- elseif VERSION >= v " 1.4"
414
- (ctx, pkgspec, pkgspec. path, joinpath (pkgspec. path, " test" ), test_project_override)
415
- else
416
- (ctx, pkgspec, pkgspec. path, joinpath (pkgspec. path, " test" ))
417
- end
418
-
419
- sandbox (sandbox_args... ) do
420
- flush (stdout )
421
- runtests! (errs, pkg, cmd, logfilename)
422
- end
423
- else
424
- with_dependencies_loadable_at_toplevel (ctx, pkgspec; might_need_to_resolve= true ) do localctx
425
- Pkg. activate (localctx. env. project_file)
426
- try
427
- runtests! (errs, pkg, cmd, logfilename)
428
- finally
429
- Pkg. activate (ctx. env. project_file)
430
- end
431
- end
276
+ TestEnv. activate (pkg) do
277
+ runtests! (errs, pkg, cmd, logfilename)
432
278
end
433
279
end
434
280
end
@@ -456,7 +302,7 @@ If `logfilename` is supplied, it must match the type (and length, if a vector) o
456
302
The tests are run in the same way as `Pkg.test`.
457
303
"""
458
304
function test (; kwargs... )
459
- ctx = Context ()
305
+ ctx = Pkg . Types . Context ()
460
306
# This error mirrors the message generated by Pkg.test in similar situations
461
307
ctx. env. pkg === nothing && throw (PkgTestError (" trying to test an unnamed project" ))
462
308
test (ctx. env. pkg. name; kwargs... )
0 commit comments