|
| 1 | +@testsetup module Precompile |
| 2 | + |
| 3 | +using Test |
| 4 | +using ReTestItems |
| 5 | + |
| 6 | +export precompile_test_harness, check_presence, create_standalone |
| 7 | + |
| 8 | +function precompile_test_harness(@nospecialize(f), testset::String) |
| 9 | + @testset "$testset" begin |
| 10 | + precompile_test_harness(f, true) |
| 11 | + end |
| 12 | +end |
| 13 | +function precompile_test_harness(@nospecialize(f), separate::Bool) |
| 14 | + load_path = mktempdir() |
| 15 | + load_cache_path = separate ? mktempdir() : load_path |
| 16 | + try |
| 17 | + pushfirst!(LOAD_PATH, load_path) |
| 18 | + pushfirst!(DEPOT_PATH, load_cache_path) |
| 19 | + f(load_path) |
| 20 | + finally |
| 21 | + try |
| 22 | + rm(load_path, force=true, recursive=true) |
| 23 | + catch err |
| 24 | + @show err |
| 25 | + end |
| 26 | + if separate |
| 27 | + try |
| 28 | + rm(load_cache_path, force=true, recursive=true) |
| 29 | + catch err |
| 30 | + @show err |
| 31 | + end |
| 32 | + end |
| 33 | + filter!((≠)(load_path), LOAD_PATH) |
| 34 | + separate && filter!((≠)(load_cache_path), DEPOT_PATH) |
| 35 | + end |
| 36 | + nothing |
| 37 | +end |
| 38 | + |
| 39 | +function check_presence(mi, token) |
| 40 | + found = false |
| 41 | + ci = isdefined(mi, :cache) ? mi.cache : nothing |
| 42 | + while ci !== nothing |
| 43 | + if ci.owner === token && ci.max_world == typemax(UInt) |
| 44 | + found = true |
| 45 | + break |
| 46 | + end |
| 47 | + ci = isdefined(ci, :next) ? ci.next : nothing |
| 48 | + end |
| 49 | + return found |
| 50 | +end |
| 51 | + |
| 52 | +function create_standalone(load_path, name::String, file) |
| 53 | + cp(joinpath(@__DIR__, "runtime.jl"), joinpath(load_path, "runtime.jl"), force=true) |
| 54 | + |
| 55 | + TS = include(file) |
| 56 | + code = TS.code |
| 57 | + if code.head == :begin |
| 58 | + code.head = :block |
| 59 | + end |
| 60 | + @assert code.head == :block |
| 61 | + code = Expr(:module, true, Symbol(name), code) |
| 62 | + |
| 63 | + # Write out the test setup as a micro package |
| 64 | + write(joinpath(load_path, "$name.jl"), string(code)) |
| 65 | + Base.compilecache(Base.PkgId(name)) |
| 66 | +end |
| 67 | + |
| 68 | +end # testsetup |
0 commit comments