From a0281e772e9be07aedb147de140a220d9f78980d Mon Sep 17 00:00:00 2001 From: Morten Piibeleht Date: Tue, 25 Jun 2024 12:25:34 +1200 Subject: [PATCH] more tests, remove named sandboxes --- src/CodeEvaluation.jl | 1 - src/namedsandboxes.jl | 22 ---------------------- test/runtests.jl | 37 +++++++++++++++++++++++++++---------- 3 files changed, 27 insertions(+), 33 deletions(-) delete mode 100644 src/namedsandboxes.jl diff --git a/src/CodeEvaluation.jl b/src/CodeEvaluation.jl index 24dcb01..05d21fb 100644 --- a/src/CodeEvaluation.jl +++ b/src/CodeEvaluation.jl @@ -3,6 +3,5 @@ using IOCapture: IOCapture using REPL: REPL include("sandbox.jl") -include("namedsandboxes.jl") end diff --git a/src/namedsandboxes.jl b/src/namedsandboxes.jl deleted file mode 100644 index 019bdc0..0000000 --- a/src/namedsandboxes.jl +++ /dev/null @@ -1,22 +0,0 @@ -struct NamedSandboxes - _pwd::String - _prefix::String - _sandboxes::Dict{Symbol,Sandbox} - - function NamedSandboxes(pwd::AbstractString, prefix::AbstractString="") - unique_prefix = _gensym_string() - prefix = isempty(prefix) ? unique_prefix : string(prefix, "_", unique_prefix) - return new(pwd, prefix, Dict{Symbol,Sandbox}()) - end -end - -function Base.get!(s::NamedSandboxes, name::Union{AbstractString,Nothing}=nothing) - sym = if isnothing(name) || isempty(name) - Symbol("__", s._prefix, "__", _gensym_string()) - else - Symbol("__", s._prefix, "__named__", name) - end - # Either fetch and return an existing sandbox from the meta dictionary (based on the generated name), - # or initialize a new clean one, which gets stored in meta for future re-use. - return get!(() -> Sandbox(sym; workingdirectory=s._pwd), s._sandboxes, sym) -end diff --git a/test/runtests.jl b/test/runtests.jl index 4a6daeb..45bad09 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,16 +46,6 @@ using Test end end - @testset "NamedSandboxes" begin - sandboxes = CodeEvaluation.NamedSandboxes(@__DIR__, "testsandbox") - sb1 = get!(sandboxes, "foo") - sb2 = get!(sandboxes, "bar") - sb3 = get!(sandboxes, "foo") - @test sb1.m !== sb2.m - @test sb1.m === sb3.m - @test sb2.m !== sb3.m - end - @testset "evaluate!" begin let sb = CodeEvaluation.Sandbox(:foo; workingdirectory=@__DIR__) write(sb, "2 + 2") @@ -93,5 +83,32 @@ using Test @test r.value[] === 5 @test r.output === "4" end + + let sb = CodeEvaluation.Sandbox(:foo; workingdirectory=@__DIR__) + r = CodeEvaluation.evaluate!(sb, """error("x")""") + @test r isa CodeEvaluation.Result + @test r.sandbox === sb + @test r.value isa CodeEvaluation.ExceptionValue + @test r.value[] isa ErrorException + @test r.value[].msg == "x" + @test r.output === "" + end + + let sb = CodeEvaluation.Sandbox(:foo; workingdirectory=@__DIR__) + r = CodeEvaluation.evaluate!( + sb, + """ + print("x") + error("x") + print("y") + """ + ) + @test r isa CodeEvaluation.Result + @test r.sandbox === sb + @test r.value isa CodeEvaluation.ExceptionValue + @test r.value[] isa ErrorException + @test r.value[].msg == "x" + @test r.output === "x" + end end end