Skip to content

Commit

Permalink
Add teardown phase to doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 15, 2024
1 parent a0257f9 commit 4cc0b98
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
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 @@ function _doctest(ctx::DocTestContext, block_immutable::MarkdownAST.CodeBlock)
```
""")
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

0 comments on commit 4cc0b98

Please sign in to comment.