j-fu / PlutoVista.jl

Plot library for Pluto notebooks based on plotly.js for 1D data and vtk.js (thus using WebGL) for 2/3D data.
Other
26 stars 4 forks source link

example of checking that the notebook did not fail #6

Closed fonsp closed 3 years ago

j-fu commented 3 years ago

Found this way for runtests. A markdown cell marked with PkgTestCellForTest is turned in to a code cell and a tmp version with this is executed for test...

using Test, Pluto,PlutoVista,Pkg

function testnotebook(name)
    input=joinpath(@__DIR__,"..","examples",name*".jl")

    notebook=Pluto.load_notebook_nobackup(input)

    pkgcellfortest=findfirst(c->occursin("PkgCellForTest",c.code),notebook.cells)
    @assert pkgcellfortest!=nothing
    notebook.cells[pkgcellfortest].code=replace(notebook.cells[pkgcellfortest].code,"md"=>"","\"\"\""=>"")
    @info "PkgCellForTest=$(pkgcellfortest)\n$(notebook.cells[pkgcellfortest].code)"
    Pluto.save_notebook(notebook,"tmp.jl")

    sleep(1)

    session = Pluto.ServerSession();
    notebook = Pluto.SessionActions.open(session, "tmp.jl"; run_async=false)
    errored=false
    for c in notebook.cells
        if c.errored
            errored=true
            @error "Error in  $(c.cell_id): $(c.output.body[:msg])\n $(c.code)"
        end
    end
    !errored
end

notebooks=["plutovista"]

@testset "notebooks" begin
    for notebook in notebooks
        @test testnotebook(notebook)
    end
end