Skip to content

Commit

Permalink
Refactor testsets, fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Feb 6, 2024
1 parent 7c65fd4 commit 718cd8d
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 97 deletions.
100 changes: 3 additions & 97 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,57 +25,14 @@ function with_extension(path, ext)
return "$root.$ext"
end

# Update and precompile all project TOMLs when in CI.
if get(ENV, "CI", "false") == "true"
for dir in ["integrations", "mimetypes"]
for (root, dirs, files) in walkdir(joinpath(@__DIR__, "examples", dir))
for each in files
if each == "Project.toml"
manifest = joinpath(root, "Manifest.toml")
if isfile(manifest)
rm(manifest; force = true)
end
run(
`$(Base.julia_cmd()) --project=$root -e 'push!(LOAD_PATH, "@stdlib"); import Pkg; Pkg.update()'`,
)
end
end
end
end
end
include("utilities/project_precompile.jl")
include("utilities/cleanup.jl")

@testset "QuartoNotebookRunner" begin

include("testsets/error_configuration/01.jl")
include("testsets/error_configuration/02.jl")

@testset "socket server" begin
cd(@__DIR__) do
node = NodeJS_18_jll.node()
client = joinpath(@__DIR__, "client.js")
port = 4001
server = QuartoNotebookRunner.serve(; port, showprogress = false)
sleep(1)
json(cmd) = JSON3.read(read(cmd, String), Any)

d1 = json(`$node $client $port run $(joinpath("examples", "cell_types.qmd"))`)
@test length(d1["notebook"]["cells"]) == 6

d2 = json(`$node $client $port run $(joinpath("examples", "cell_types.qmd"))`)
@test d1 == d2

d3 = json(`$node $client $port close $(joinpath("examples", "cell_types.qmd"))`)
@test d3["status"] == true

d4 = json(`$node $client $port run $(joinpath("examples", "cell_types.qmd"))`)
@test d1 == d4

d5 = json(`$node $client $port stop`)
@test d5["message"] == "Server stopped."

wait(server)
end
end
include("testsets/socket_server/socket_server.jl")

schema = JSONSchema.Schema(
open(JSON3.read, joinpath(@__DIR__, "schema/nbformat.v4.schema.json")),
Expand Down Expand Up @@ -627,57 +584,6 @@ end
# Switching exeflags within a running notebook causes it to restart so that
# the new exeflags can be applied.
mktempdir() do dir
@testset "non-standard mime types" begin
server = QuartoNotebookRunner.Server()
expected = Dict("typst" => "```{=typst}", "docx" => "```{=openxml}")

env = joinpath(dir, "integrations", "CairoMakie")
mkpath(env)
cp(joinpath(@__DIR__, "examples/integrations/CairoMakie"), env; force = true)

for (format, ext) in ("typst" => "pdf", "docx" => "docx")
cd(dir) do
source = joinpath(@__DIR__, "examples/$(format)_mimetypes.qmd")
content = read(source, String)
write("$(format)_mimetypes.qmd", content)
ipynb = "$(format)_mimetypes.ipynb"
QuartoNotebookRunner.run!(
server,
"$(format)_mimetypes.qmd";
output = ipynb,
showprogress = false,
options = Dict{String,Any}(
"format" => Dict("pandoc" => Dict("to" => format)),
),
)

json = JSON3.read(ipynb)
markdown = json.cells[end].outputs[1].data["text/markdown"]
@test contains(markdown, expected[format])

if !Sys.iswindows()
# No macOS ARM build, so just look for a local version that the dev
# should have installed. This avoids having to use rosetta2 to run
# the x86_64 version of Julia to get access to the x86_64 version of
# Quarto artifact.
quarto_bin =
quarto_jll.is_available() ? quarto_jll.quarto() :
setenv(`quarto`)
# Just a smoke test to make sure it runs. Use docx since it doesn't
# output a bunch of folders (html), or require a tinytex install
# (pdf). All we are doing here at the moment is ensuring quarto doesn't
# break on our notebook outputs.
if success(`$quarto_bin --version`)
@test success(`$quarto_bin render $ipynb --to $format`)
else
@error "quarto not found, skipping smoke test."
end
@test isfile("$(format)_mimetypes.$ext")
end
end
end
close!(server)
end
@testset "exeflags notebook restart" begin
content = read(joinpath(@__DIR__, "examples/stdout_exeflags.qmd"), String)
cd(dir) do
Expand Down
55 changes: 55 additions & 0 deletions test/testsets/non_standard_mimetypes/non_standard_mimetypes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Test, QuartoNotebookRunner
import JSON3, quarto_jll

@testset "non-standard mime types" begin
mktempdir() do dir
server = QuartoNotebookRunner.Server()
expected = Dict("typst" => "```{=typst}", "docx" => "```{=openxml}")

env = joinpath(dir, "integrations", "CairoMakie")
mkpath(env)
cp(joinpath(@__DIR__, "../examples/integrations/CairoMakie"), env; force = true)

for (format, ext) in ("typst" => "pdf", "docx" => "docx")
cd(dir) do
source = joinpath(@__DIR__, "../examples/$(format)_mimetypes.qmd")
content = read(source, String)
write("$(format)_mimetypes.qmd", content)
ipynb = "$(format)_mimetypes.ipynb"
QuartoNotebookRunner.run!(
server,
"$(format)_mimetypes.qmd";
output = ipynb,
showprogress = false,
options = Dict{String,Any}(
"format" => Dict("pandoc" => Dict("to" => format)),
),
)

json = JSON3.read(ipynb)
markdown = json.cells[end].outputs[1].data["text/markdown"]
@test contains(markdown, expected[format])

if !Sys.iswindows()
# No macOS ARM build, so just look for a local version that the dev
# should have installed. This avoids having to use rosetta2 to run
# the x86_64 version of Julia to get access to the x86_64 version of
# Quarto artifact.
quarto_bin =
quarto_jll.is_available() ? quarto_jll.quarto() : setenv(`quarto`)
# Just a smoke test to make sure it runs. Use docx since it doesn't
# output a bunch of folders (html), or require a tinytex install
# (pdf). All we are doing here at the moment is ensuring quarto doesn't
# break on our notebook outputs.
if success(`$quarto_bin --version`)
@test success(`$quarto_bin render $ipynb --to $format`)
else
@error "quarto not found, skipping smoke test."
end
@test isfile("$(format)_mimetypes.$ext")
end
end
end
close!(server)
end
end
File renamed without changes.
32 changes: 32 additions & 0 deletions test/testsets/socket_server/socket_server.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Test, QuartoNotebookRunner
import JSON3, NodeJS_18_jll

@testset "socket server" begin
cd(@__DIR__) do
node = NodeJS_18_jll.node()
client = joinpath(@__DIR__, "client.js")
port = 4001
server = QuartoNotebookRunner.serve(; port, showprogress = false)
sleep(1)
json(cmd) = JSON3.read(read(cmd, String), Any)

cell_types = "../../examples/cell_types.qmd"

d1 = json(`$node $client $port run $(cell_types)`)
@test length(d1["notebook"]["cells"]) == 6

d2 = json(`$node $client $port run $(cell_types)`)
@test d1 == d2

d3 = json(`$node $client $port close $(cell_types)`)
@test d3["status"] == true

d4 = json(`$node $client $port run $(cell_types)`)
@test d1 == d4

d5 = json(`$node $client $port stop`)
@test d5["message"] == "Server stopped."

wait(server)
end
end
20 changes: 20 additions & 0 deletions test/utilities/cleanup.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let removed = []
for (root, dirs, files) in walkdir(joinpath(@__DIR__, ".."); topdown = false)
for file in files
_, ext = splitext(file)
if ext in (".html", ".pdf", ".tex", ".docx", ".typ", ".ipynb", ".png", ".css")
path = joinpath(root, file)
push!(removed, path)
rm(path; force = true)
end
end
for dir in dirs
path = joinpath(root, dir)
if isempty(readdir(path))
push!(removed, path)
rm(path; force = true, recursive = true)
end
end
end
@info "removed files and directories" removed
end
18 changes: 18 additions & 0 deletions test/utilities/project_precompile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Update and precompile all project TOMLs when in CI.
if get(ENV, "CI", "false") == "true"
for dir in ["integrations", "mimetypes"]
for (root, dirs, files) in walkdir(joinpath(@__DIR__, "..", "examples", dir))
for each in files
if each == "Project.toml"
manifest = joinpath(root, "Manifest.toml")
if isfile(manifest)
rm(manifest; force = true)
end
run(
`$(Base.julia_cmd()) --project=$root -e 'push!(LOAD_PATH, "@stdlib"); import Pkg; Pkg.update()'`,
)
end
end
end
end
end

0 comments on commit 718cd8d

Please sign in to comment.