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

Add teardown phase to doctests #2577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ test/prerender/build/
test/themes/dev/
test/workdir/builds/
test/quietly-logs/
test/docsxref/
docs/dev/
docs/build/
docs/build-pdf/
Expand Down
6 changes: 5 additions & 1 deletion src/DocMeta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module — a special variable is created in each module that has documentation m
# Supported metadata

* `DocTestSetup`: contains the doctest setup code for doctests in the module.
* `DocTestTeardown`: contains the doctest teardown code for doctests in the module.
"""
module DocMeta
import ..Documenter
Expand All @@ -26,7 +27,10 @@ const METAMODULES = Module[]
const METATYPE = Dict{Symbol,Any}

"Dictionary of all valid metadata keys and their types."
const VALIDMETA = Dict{Symbol,Type}(:DocTestSetup => Union{Expr,Symbol})
const VALIDMETA = Dict{Symbol,Type}(
:DocTestSetup => Union{Expr,Symbol},
:DocTestTeardown => Union{Expr,Symbol}
)

"""
"""
Expand Down
11 changes: 11 additions & 0 deletions src/doctests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@
```
""")
end
for expr in [get(ctx.meta, :DocTestTeardown, []); get(ctx.meta[:LocalDocTestArguments], :teardown, [])]
Meta.isexpr(expr, :block) && (expr.head = :toplevel)
try
Core.eval(sandbox, expr)

Check warning on line 204 in src/doctests.jl

View check run for this annotation

Codecov / codecov/patch

src/doctests.jl#L202-L204

Added lines #L202 - L204 were not covered by tests
catch e
push!(ctx.doc.internal.errors, :doctest)
@error("could not evaluate expression from doctest teardown.",

Check warning on line 207 in src/doctests.jl

View check run for this annotation

Codecov / codecov/patch

src/doctests.jl#L206-L207

Added lines #L206 - L207 were not covered by tests
expression = expr, exception = e)
return false

Check warning on line 209 in src/doctests.jl

View check run for this annotation

Codecov / codecov/patch

src/doctests.jl#L209

Added line #L209 was not covered by tests
end
end

Check warning on line 211 in src/doctests.jl

View check run for this annotation

Codecov / codecov/patch

src/doctests.jl#L211

Added line #L211 was not covered by tests
delete!(ctx.meta, :LocalDocTestArguments)
end
false
Expand Down
2 changes: 1 addition & 1 deletion src/expander_pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function Selectors.runner(::Type{Expanders.MetaBlocks}, node, page, doc)
# wants to hide. We should probably warn, but it is common enough that
# we will silently skip for now.
if Documenter.isassign(ex)
if !(ex.args[1] in (:CurrentModule, :DocTestSetup, :DocTestFilters, :EditURL, :Description, :Draft, :CollapsedDocStrings))
if !(ex.args[1] in (:CurrentModule, :DocTestSetup, :DocTestTeardown, :DocTestFilters, :EditURL, :Description, :Draft, :CollapsedDocStrings))
source = Documenter.locrepr(page.source, lines)
@warn(
"In $source: `@meta` block has an unsupported " *
Expand Down
Loading