Browse Source

Verify that inputfile exist before working with it, fix #24.

pull/28/head
Fredrik Ekre 7 years ago
parent
commit
4da2e86d07
  1. 21
      src/Literate.jl
  2. 9
      test/runtests.jl

21
src/Literate.jl

@ -229,10 +229,12 @@ function script(inputfile, outputdir; preprocess = identity, postprocess = ident @@ -229,10 +229,12 @@ function script(inputfile, outputdir; preprocess = identity, postprocess = ident
name = filename(inputfile), documenter = true, credit = true,
keep_comments::Bool=false, kwargs...)
# normalize paths
isfile(inputfile) || throw(ArgumentError("cannot find inputfile `$(inputfile)`"))
inputfile = realpath(abspath(inputfile))
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
@info "generating plain script file from $(inputfile)"
@info "generating plain script file from `$(Base.contractuser(inputfile))`"
# read content
content = read(inputfile, String)
@ -266,7 +268,7 @@ function script(inputfile, outputdir; preprocess = identity, postprocess = ident @@ -266,7 +268,7 @@ function script(inputfile, outputdir; preprocess = identity, postprocess = ident
isdir(outputdir) || error("not a directory: $(outputdir)")
outputfile = joinpath(outputdir, name * ".jl")
@info "writing result to $(outputfile)"
@info "writing result to `$(Base.contractuser(outputfile))`"
write(outputfile, content)
return outputfile
@ -303,10 +305,12 @@ function markdown(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -303,10 +305,12 @@ function markdown(inputfile, outputdir; preprocess = identity, postprocess = ide
codefence::Pair = documenter ? "```@example $(name)" => "```" : "```julia" => "```",
kwargs...)
# normalize paths
isfile(inputfile) || throw(ArgumentError("cannot find inputfile `$(inputfile)`"))
inputfile = realpath(abspath(inputfile))
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
@info "generating markdown page from $(inputfile)"
@info "generating markdown page from `$(Base.contractuser(inputfile))`"
# read content
content = read(inputfile, String)
@ -372,7 +376,7 @@ function markdown(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -372,7 +376,7 @@ function markdown(inputfile, outputdir; preprocess = identity, postprocess = ide
isdir(outputdir) || error("not a directory: $(outputdir)")
outputfile = joinpath(outputdir, name * ".md")
@info "writing result to $(outputfile)"
@info "writing result to `$(Base.contractuser(outputfile))`"
write(outputfile, content)
return outputfile
@ -402,11 +406,12 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -402,11 +406,12 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
execute::Bool=true, documenter::Bool=true, credit = true,
name = filename(inputfile), kwargs...)
# normalize paths
isfile(inputfile) || throw(ArgumentError("cannot find inputfile `$(inputfile)`"))
inputfile = realpath(abspath(inputfile))
mkpath(outputdir)
outputdir = realpath(abspath(outputdir))
@info "generating notebook from $(inputfile)"
@info "generating notebook from `$(Base.contractuser(inputfile))`"
# read content
content = read(inputfile, String)
@ -469,13 +474,13 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -469,13 +474,13 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
nb = postprocess(nb)
if execute
@info "executing notebook $(name * ".ipynb")"
@info "executing notebook `$(name * ".ipynb")`"
try
cd(outputdir) do
nb = execute_notebook(nb)
end
catch err
@error "error when executing notebook based on input file: $(inputfile)"
@error "error when executing notebook based on input file: `$(Base.contractuser(inputfile))`"
rethrow(err)
end
end
@ -484,7 +489,7 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide @@ -484,7 +489,7 @@ function notebook(inputfile, outputdir; preprocess = identity, postprocess = ide
isdir(outputdir) || error("not a directory: $(outputdir)")
outputfile = joinpath(outputdir, name * ".ipynb")
@info "writing result to $(outputfile)"
@info "writing result to `$(Base.contractuser(outputfile))`"
ionb = IOBuffer()
JSON.print(ionb, nb, 1)
write(outputfile, seekstart(ionb))

9
test/runtests.jl

@ -277,6 +277,9 @@ content = """ @@ -277,6 +277,9 @@ content = """
@test occursin("# # Example", script)
@test occursin("# foo, bar", script)
@test occursin("# \\int f(x) dx", script)
# verify that inputfile exists
@test_throws ArgumentError Literate.script("nonexistent.jl", outdir)
end
end
end
@ -414,6 +417,9 @@ end @@ -414,6 +417,9 @@ end
@test occursin("name: foobar", markdown)
@test !occursin("name: inputfile", markdown)
@test !occursin("name: @__NAME__", markdown)
# verify that inputfile exists
@test_throws ArgumentError Literate.markdown("nonexistent.jl", outdir)
end
end
end
@ -631,6 +637,9 @@ end @@ -631,6 +637,9 @@ end
end
@test isa(r, ErrorException)
@test occursin("when executing the following code block", r.msg)
# verify that inputfile exists
@test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir)
end
end
end

Loading…
Cancel
Save