Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix include path within testsets #16

Merged
merged 1 commit into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ should be fixable:
is the current implemented heuristic to allow `Revise` do its magic.


#### Including files from within testsets

TLDR: don't use `include(file)` within testsets when `file` defines
other testsets.

There is limited support for `include(path)` expressions within testsets: all
what `ReTest` does is to adjust the `path` according to the location of the
containing file `parentfile`. This is necessary, because `include` is not run
immediately when that file is evaluated; when the given testset is triggered
(via a `retest` call), `include` doesn't have the same "context" as
`parentfile`, which would lead to `path` being interpreted as non-existing
(unless `path` is an absolute path). So when parsing testsets, `ReTest`
prepends the directory name of `parentfile` to `path`.

The important point is that `include` is executed at `retest`-time; if the
included file defines other `@testset` expressions, this will define new
testsets in the enclosing module, but these won't be run immediately; upon a
new `retest()` invocation, these new testsets will be run, but the old one too
(the one containing `include`), which will redefine included testsets. This is
brittle, and it's recommended to not include, within testsets, files defining
other testsets.


## Switching from `Test` to `ReTest`

When used in a package `MyPackage`, the test code can be organized as follows:
Expand Down
7 changes: 7 additions & 0 deletions src/ReTest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function replace_ts(source, mod, x::Expr, parent)
ts, false # hasbroken counts only "proper" @test_broken, not recursive ones
elseif x.head === :macrocall && x.args[1] === Symbol("@test_broken")
x, true
elseif x.head == :call && x.args[1] == :include
path = x.args[end]
sourcepath = dirname(string(source.file))
x.args[end] = path isa AbstractString ?
joinpath(sourcepath, path) :
:(joinpath($sourcepath, $path))
x, false
else
body_br = map(z -> replace_ts(source, mod, z, parent), x.args)
Expr(x.head, first.(body_br)...), any(last.(body_br))
Expand Down
2 changes: 2 additions & 0 deletions test/FakePackage/test/included.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@test true
push!(RUN, 0)
17 changes: 17 additions & 0 deletions test/FakePackage/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,20 @@ retest(FakePackage, recursive=true)
@test FakePackage.RUN == [1, 2, 1, 2, 1, 2, 3, 4]
retest(FakePackage) # equivalent to recursive=true
@test FakePackage.RUN == [1, 2, 1, 2, 1, 2, 3, 4, 1, 2, 3, 4]

module Included
using ReTest
RUN = []

@testset "include parent" begin
include("included.jl")
include(joinpath(@__DIR__, "included.jl"))
if VERSION >= v"1.5"
include(identity, "included.jl")
include(identity, joinpath(@__DIR__, "included.jl"))
end
end
end

retest(Included)
@test Included.RUN == (VERSION >= v"1.5" ? [0, 0, 0, 0] : [0, 0])