Skip to content

Commit

Permalink
Add the option allow_errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp authored and fredrikekre committed Oct 16, 2024
1 parent 1adce12 commit 5c1311b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/Literate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ function create_configuration(inputfile; user_config, user_kwargs, type=nothing)
cfg["softscope"] = type === (:nb) ? true : false # on for Jupyter notebooks
cfg["keep_comments"] = false
cfg["execute"] = type === :md ? false : true
cfg["allow_errors"] = false
cfg["codefence"] = pick_codefence(
get(user_config, "flavor", cfg["flavor"]),
get(user_config, "execute", cfg["execute"]),
Expand Down Expand Up @@ -433,6 +434,9 @@ Available options:
output script. Only applicable for `Literate.script`.
- `execute` (default: `true` for notebook, `false` for markdown): Whether to execute and
capture the output. Only applicable for `Literate.notebook` and `Literate.markdown`.
- `allow_errors` (default: `false`): Whether to capture and display error messages. If
`true`, blocks that error will display the error message. If `false`, errors will be
(re)thrown as is. Ignored when `execute == false`.
- `codefence` (default: `````"````@example \$(name)" => "````"````` for `DocumenterFlavor()`
and `````"````julia" => "````"````` otherwise): Pair containing opening and closing
code fence for wrapping code blocks.
Expand Down Expand Up @@ -621,6 +625,7 @@ function markdown(inputfile, outputdir=pwd(); config::AbstractDict=Dict(), kwarg
image_formats=config["image_formats"],
file_prefix="$(config["name"])-$(chunknum)",
softscope=config["softscope"],
allow_errors=config["allow_errors"],
)
end
end
Expand All @@ -639,9 +644,10 @@ end
function execute_markdown!(io::IO, sb::Module, block::String, outputdir;
inputfile::String, fake_source::String,
flavor::AbstractFlavor, image_formats::Vector, file_prefix::String,
softscope::Bool)
softscope::Bool, allow_errors::Bool)
# TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source, softscope=softscope)
r, str, _ = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source,
softscope=softscope, allow_errors=allow_errors)
# issue #101: consecutive codefenced blocks need newline
# issue #144: quadruple backticks allow for triple backticks in the output
plain_fence = "\n````\n" => "\n````"
Expand Down Expand Up @@ -777,7 +783,9 @@ function jupyter_notebook(chunks, config)
cd(config["literate_outputdir"]) do
nb = execute_notebook(nb; inputfile=config["literate_inputfile"],
fake_source=config["literate_outputfile"],
softscope=config["softscope"])
softscope=config["softscope"],
allow_errors=config["allow_errors"],
)
end
catch err
@error "error when executing notebook based on input file: " *
Expand All @@ -788,15 +796,18 @@ function jupyter_notebook(chunks, config)
return nb
end

function execute_notebook(nb; inputfile::String, fake_source::String, softscope::Bool)
function execute_notebook(nb; inputfile::String, fake_source::String, softscope::Bool,
allow_errors=allow_errors)
sb = sandbox()
execution_count = 0
for cell in nb["cells"]
cell["cell_type"] == "code" || continue
execution_count += 1
cell["execution_count"] = execution_count
block = join(cell["source"])
r, str, display_dicts = execute_block(sb, block; inputfile=inputfile, fake_source=fake_source, softscope=softscope)
r, str, display_dicts = execute_block(sb, block; inputfile=inputfile,
fake_source=fake_source, softscope=softscope,
allow_errors=allow_errors)

# str should go into stream
if !isempty(str)
Expand Down Expand Up @@ -878,7 +889,8 @@ function Base.display(ld::LiterateDisplay, mime::MIME, x)
end

# Execute a code-block in a module and capture stdout/stderr and the result
function execute_block(sb::Module, block::String; inputfile::String, fake_source::String, softscope::Bool)
function execute_block(sb::Module, block::String; inputfile::String, fake_source::String,
softscope::Bool, allow_errors::Bool)
@debug """execute_block($sb, block)
```
$(block)
Expand All @@ -904,7 +916,7 @@ function execute_block(sb::Module, block::String; inputfile::String, fake_source
end
end
popdisplay(disp) # IOCapture.capture has a try-catch so should always end up here
if c.error
if c.error && !allow_errors
error("""
$(sprint(showerror, c.value))
when executing the following code block from inputfile `$(Base.contractuser(inputfile))`
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,22 @@ end end
end # mktempdir
end end

@testset "allow_errors=true" begin
input_with_error =
"""
# The following will error
sqrt(-1.0)
"""
mktempdir(@__DIR__) do sandbox
inputfile = joinpath(sandbox, "input.jl")
write(inputfile, input_with_error)
Literate.markdown(inputfile, sandbox; allow_errors = true, execute = true)
output_md = read(joinpath(sandbox, "input.md"), String)
@test occursin("DomainError(-1.0", output_md)
end
end

@testset "Configuration" begin; Base.CoreLogging.with_logger(Base.CoreLogging.NullLogger()) do
mktempdir(@__DIR__) do sandbox
cd(sandbox) do
Expand Down

0 comments on commit 5c1311b

Please sign in to comment.