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

9
test/runtests.jl

@ -277,6 +277,9 @@ content = """
@test occursin("# # Example", script) @test occursin("# # Example", script)
@test occursin("# foo, bar", script) @test occursin("# foo, bar", script)
@test occursin("# \\int f(x) dx", script) @test occursin("# \\int f(x) dx", script)
# verify that inputfile exists
@test_throws ArgumentError Literate.script("nonexistent.jl", outdir)
end end
end end
end end
@ -414,6 +417,9 @@ end
@test occursin("name: foobar", markdown) @test occursin("name: foobar", markdown)
@test !occursin("name: inputfile", markdown) @test !occursin("name: inputfile", markdown)
@test !occursin("name: @__NAME__", markdown) @test !occursin("name: @__NAME__", markdown)
# verify that inputfile exists
@test_throws ArgumentError Literate.markdown("nonexistent.jl", outdir)
end end
end end
end end
@ -631,6 +637,9 @@ end
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", r.msg)
# verify that inputfile exists
@test_throws ArgumentError Literate.notebook("nonexistent.jl", outdir)
end end
end end
end end

Loading…
Cancel
Save