Browse Source

include inputfile in errors (#150)

pull/154/head
st-- 5 years ago committed by GitHub
parent
commit
d561b540e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/Literate.jl
  2. 5
      test/runtests.jl

16
src/Literate.jl

@ -458,7 +458,7 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !(config["documenter"]::Bool)) write_code = !(all(l -> endswith(l, "#hide"), chunk.lines) && !(config["documenter"]::Bool))
write_code && write(iomd, seekstart(iocode)) write_code && write(iomd, seekstart(iocode))
if config["execute"]::Bool if config["execute"]::Bool
execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir) execute_markdown!(iomd, sb, join(chunk.lines, '\n'), outputdir; inputfile=config["literate_inputfile"])
end end
end end
write(iomd, '\n') # add a newline between each chunk write(iomd, '\n') # add a newline between each chunk
@ -472,9 +472,9 @@ function markdown(inputfile, outputdir=pwd(); config::Dict=Dict(), kwargs...)
return outputfile return outputfile
end end
function execute_markdown!(io::IO, sb::Module, block::String, outputdir) function execute_markdown!(io::IO, sb::Module, block::String, outputdir; inputfile::String="<unknown>")
# TODO: Deal with explicit display(...) calls # TODO: Deal with explicit display(...) calls
r, str, _ = execute_block(sb, block) r, str, _ = execute_block(sb, block; inputfile=inputfile)
plain_fence = "\n```\n" => "\n```" # issue #101: consecutive codefenced blocks need newline plain_fence = "\n```\n" => "\n```" # issue #101: consecutive codefenced blocks need newline
if r !== nothing && !REPL.ends_with_semicolon(block) if r !== nothing && !REPL.ends_with_semicolon(block)
for (mime, ext) in [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")] for (mime, ext) in [(MIME("image/png"), ".png"), (MIME("image/jpeg"), ".jpeg")]
@ -601,7 +601,7 @@ function jupyter_notebook(chunks, config)
@info "executing notebook `$(config["name"] * ".ipynb")`" @info "executing notebook `$(config["name"] * ".ipynb")`"
try try
cd(config["literate_outputdir"]) do cd(config["literate_outputdir"]) do
nb = execute_notebook(nb) nb = execute_notebook(nb; inputfile=config["literate_inputfile"])
end end
catch err catch err
@error "error when executing notebook based on input file: " * @error "error when executing notebook based on input file: " *
@ -612,7 +612,7 @@ function jupyter_notebook(chunks, config)
return nb return nb
end end
function execute_notebook(nb) function execute_notebook(nb; inputfile::String="<unknown>")
sb = sandbox() sb = sandbox()
execution_count = 0 execution_count = 0
for cell in nb["cells"] for cell in nb["cells"]
@ -620,7 +620,7 @@ function execute_notebook(nb)
execution_count += 1 execution_count += 1
cell["execution_count"] = execution_count cell["execution_count"] = execution_count
block = join(cell["source"]) block = join(cell["source"])
r, str, display_dicts = execute_block(sb, block) r, str, display_dicts = execute_block(sb, block; inputfile=inputfile)
# str should go into stream # str should go into stream
if !isempty(str) if !isempty(str)
@ -702,7 +702,7 @@ function Base.display(ld::LiterateDisplay, mime::MIME, x)
end end
# Execute a code-block in a module and capture stdout/stderr and the result # Execute a code-block in a module and capture stdout/stderr and the result
function execute_block(sb::Module, block::String) function execute_block(sb::Module, block::String; inputfile::String="<unknown>")
@debug """execute_block($sb, block) @debug """execute_block($sb, block)
``` ```
$(block) $(block)
@ -724,7 +724,7 @@ function execute_block(sb::Module, block::String)
if c.error if c.error
error(""" error("""
$(sprint(showerror, c.value)) $(sprint(showerror, c.value))
when executing the following code block when executing the following code block in file `$(Base.contractuser(inputfile))`
```julia ```julia
$block $block

5
test/runtests.jl

@ -1038,14 +1038,15 @@ end end
# test error when executing notebook # test error when executing notebook
write(inputfile, "for i in 1:10\n println(i)") write(inputfile, "for i in 1:10\n println(i)")
r = @test_logs((:error, r"error when executing"), match_mode=:any, r = @test_logs((:error, r"error when executing notebook based on input file: "), match_mode=:any,
try try
Literate.notebook(inputfile, outdir) Literate.notebook(inputfile, outdir)
catch err catch err
err err
end) end)
@test isa(r, ErrorException) @test isa(r, ErrorException)
@test occursin("when executing the following code block", r.msg) @test occursin("when executing the following code block in file ", r.msg)
@test occursin(inputfile, r.msg)
# verify that inputfile exists # verify that inputfile exists
@test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir) @test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir)

Loading…
Cancel
Save