Skip to content

Commit

Permalink
hijack: be more precise with include=:static (#33)
Browse files Browse the repository at this point in the history
Instead of fetching top-level `@testset` expressions, we inspect
newly added testsets in the module via `get_tests(mod).news`.
This allows for example to catch testsets defined by
meta-programming.
  • Loading branch information
rfourquet authored Aug 6, 2021
1 parent 8ae57f1 commit d1fea44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/ReTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,29 @@ function replace_ts(source, mod, x::Expr, parent; static_include::Bool)
joinpath(sourcepath, path) :
:(joinpath($sourcepath, $path))
if static_include
length(x.args) == 2 || error("cannot handle include with two arguments: $x")
news = Expr(:block)
insert!(x.args, 2, extract_testsets(news.args))
news = InlineTest.get_tests(mod).news
newslen = length(news)
try
Core.eval(mod, x)
catch
@warn "could not statically include at $source"
deleteat!(x.args, 2)
return x, false
end
replace_ts(source, mod, news, parent; static_include=static_include)
newstmp = news[newslen+1:end]
resize!(news, newslen)
if !isempty(newstmp)
# we unfortunately re-wrap ts expressions in a `@testset ...` expression :(
included_ts = Expr(:block,
(Expr(:macrocall, Symbol("@testset"),
# NOTE: tsi.source has no effect here, it will be
# overwritten by source in the replace_ts call
# below; it's currently not very important
tsi.source, tsi.ts...)
for tsi in newstmp)...)
replace_ts(source, mod, included_ts, parent; static_include=static_include)
else
nothing, false
end
else
x, false
end
Expand Down
4 changes: 3 additions & 1 deletion test/Hijack/test/include_static_included1.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@testset "include_static_included1 $i" for i=1:2
# with @eval, we test that include=:static works even for a non-toplevel
# @testset, what matters is that a testset is defined after including the file
@eval @testset "include_static_included1 $i" for i=1:2
@test true
@testset "nested include_static_included1" begin
@test true
Expand Down

0 comments on commit d1fea44

Please sign in to comment.