Skip to content

Commit 81ad054

Browse files
authored
Merge pull request #89 from JuliaTesting/mm/use-test-env
Use TestEnv
2 parents 5cb90bc + 13e6f78 commit 81ad054

File tree

5 files changed

+19
-215
lines changed

5 files changed

+19
-215
lines changed

.github/workflows/ci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
fail-fast: false
1414
matrix:
1515
version:
16-
- '1.0'
1716
- '1.6'
1817
- '1'
1918
- 'nightly'

Project.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
name = "TestReports"
22
uuid = "dcd651b4-b50a-5b6b-8f22-87e9f253a252"
3-
version = "1.1.1"
3+
version = "1.1.2"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
77
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
88
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
99
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1010
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
11+
TestEnv = "1e6cf692-eddd-4d53-88a5-2d735e33781b"
1112

1213
[compat]
1314
EzXML = "1"
14-
julia = "1"
15+
julia = "1.6"
16+
TestEnv = "1"
1517

1618
[extras]
1719
ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf"

src/TestReports.jl

+1-21
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,13 @@ using EzXML
55
using Pkg
66
using Printf
77
using Test
8-
9-
using Pkg: PackageSpec
10-
using Pkg.Types: Context, ensure_resolved, is_project_uuid
11-
using Pkg.Operations: manifest_info, manifest_resolve!, project_deps_resolve!,
12-
project_rel_path, project_resolve!
8+
using TestEnv
139

1410
using Test: AbstractTestSet, DefaultTestSet, Result, Pass, Fail, Error, Broken,
1511
get_testset, get_testset_depth, scrub_backtrace
1612

1713
import Test: finish, record
1814

19-
# Version specific imports
20-
@static if VERSION >= v"1.4.0"
21-
using Pkg.Operations: gen_target_project
22-
else
23-
using Pkg.Operations: with_dependencies_loadable_at_toplevel
24-
end
25-
@static if VERSION >= v"1.2.0"
26-
using Pkg.Operations: sandbox, source_path
27-
@static if VERSION < v"1.7.0"
28-
using Pkg.Operations: update_package_test!
29-
end
30-
else
31-
using Pkg.Operations: find_installed
32-
using Pkg.Types: SHA1
33-
end
34-
3515
export ReportingTestSet, any_problems, report, record_test_property, record_testset_property
3616

3717
const TESTREPORTS_VERSION = let # Copied from Documenter.jl

src/runner.jl

+14-168
Original file line numberDiff line numberDiff line change
@@ -191,126 +191,6 @@ function gen_command(runner_code, julia_args, coverage)
191191
return cmd
192192
end
193193

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-
314194
test_project_filepath(testfilepath) = joinpath(dirname(testfilepath), "Project.toml")
315195
has_test_project_file(testfilepath) = isfile(test_project_filepath(testfilepath))
316196

@@ -375,60 +255,26 @@ function test!(pkg::AbstractString,
375255
# Copied from Pkg.test approach
376256
julia_args = Cmd(julia_args)
377257
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")
388269
check_testreports_compatability(ctx, pkgspec, testfilepath)
389270

390271
if !isfile(testfilepath)
391272
push!(notests, pkg)
392273
else
393274
runner_code = gen_runner_code(testfilepath, logfilename, test_args)
394275
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)
432278
end
433279
end
434280
end
@@ -456,7 +302,7 @@ If `logfilename` is supplied, it must match the type (and length, if a vector) o
456302
The tests are run in the same way as `Pkg.test`.
457303
"""
458304
function test(; kwargs...)
459-
ctx = Context()
305+
ctx = Pkg.Types.Context()
460306
# This error mirrors the message generated by Pkg.test in similar situations
461307
ctx.env.pkg === nothing && throw(PkgTestError("trying to test an unnamed project"))
462308
test(ctx.env.pkg.name; kwargs...)

test/runnerinternals.jl

-23
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,6 @@ using Pkg
22
using Test
33
using TestReports
44

5-
if VERSION < v"1.2.0"
6-
@testset "gettestfilepath - V1.0.5" begin
7-
# Stdlibs are not tested by other functions for V1.0.5
8-
stdlibname = "Dates"
9-
ctx = Pkg.Types.Context()
10-
pkg = Pkg.PackageSpec(stdlibname)
11-
TestReports.isinstalled!(ctx, pkg)
12-
delete!(ctx.env.manifest[stdlibname][1], "path") # Remove path to force stdlib check
13-
testfilepath = joinpath(abspath(joinpath(dirname(Base.find_package(stdlibname)), "..")), "test", "runtests.jl")
14-
@test TestReports.gettestfilepath(ctx, pkg) == testfilepath
15-
16-
# PkgTestError when PkgSpec has missing info when finding path - V1.0.5 only
17-
pkgname = "PassingTests"
18-
Pkg.develop(Pkg.PackageSpec(path=joinpath(@__DIR__, "test_packages", pkgname)))
19-
ctx = Pkg.Types.Context()
20-
pkg = Pkg.PackageSpec(pkgname)
21-
TestReports.isinstalled!(ctx, pkg)
22-
delete!(ctx.env.manifest["PassingTests"][1], "path")
23-
@test_throws TestReports.PkgTestError TestReports.gettestfilepath(ctx, pkg)
24-
Pkg.rm(Pkg.PackageSpec(path=joinpath(@__DIR__, "test_packages", pkgname)))
25-
end
26-
end
27-
285
@testset "showerror" begin
296
@test_throws TestReports.PkgTestError throw(TestReports.PkgTestError("Test"))
307
@test sprint(showerror, TestReports.PkgTestError("Error text"), "") == "Error text"

0 commit comments

Comments
 (0)