Skip to content

Commit

Permalink
Distinguish stdout and display contexts, see #2727
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwe committed Feb 26, 2024
1 parent b0851f5 commit c2447ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/runner/PlutoRunner/src/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,17 @@ const default_iocontext = IOContext(devnull,
:pluto_published_to_js => (io, x) -> core_published_to_js(io, x),
)

# `stdout` mimics a TTY, the only relevant property is :color
const default_stdout_iocontext = IOContext(devnull,
:color => true,
:limit => true,
:displaysize => (18, 75),
:color => true,
:is_pluto => false,
)

# `display` sees a richer context like in the REPL, see #2727
const default_display_iocontext = IOContext(devnull,
:color => true,
:limit => true,
:displaysize => (18, 75),
:is_pluto => false,
)

Expand Down Expand Up @@ -2715,7 +2722,7 @@ function with_io_to_logs(f::Function; enabled::Bool=true, loglevel::Logging.LogL
end

# To make the `display` function work.
redirect_display = TextDisplay(pe_stdout)
redirect_display = TextDisplay(IOContext(pe_stdout, default_display_iocontext))
pushdisplay(redirect_display)

# Run the function `f`, capturing all output that it might have generated.
Expand Down
26 changes: 16 additions & 10 deletions test/StdIOContext.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using Test
import Pluto: PlutoRunner, Notebook, WorkspaceManager, Cell, ServerSession, update_run!

@testset "stdout/stderr IOContext" begin
@testset "stdout/stderr/display IOContext" begin
🍭 = ServerSession()
🍭.options.evaluation.workspace_use_distributed = true

notebook = Notebook(reduce(
vcat,
# $(repr(p)) rather than just $p for parseable output, e.g., Symbols with colons
[
Cell("($(repr(p))) in stdout"),
Cell("($(repr(p))) in stderr"),
] for p in pairs(PlutoRunner.default_stdout_iocontext.dict)
))
# $(repr(p)) rather than $p for parseable interpolation, e.g., symbols with colons
cells = Cell[]
for (k, v) in pairs(PlutoRunner.default_stdout_iocontext.dict)
append!(cells, [
Cell("($(repr(k)) => $(repr(v))) in stdout"),
Cell("($(repr(k)) => $(repr(v))) in stderr"),
])
end
for (k, v) in pairs(PlutoRunner.default_display_iocontext.dict)
# We can popdisplay() once per cell since Pluto cells have separate display stacks
push!(cells, Cell("($(repr(k)) => $(repr(v))) in popdisplay().io"))
end
notebook = Notebook(cells)

update_run!(🍭, notebook, notebook.cells)
for cell in values(notebook.cells_dict)
@test cell.output.body == "true"
# @test cell.output.body == "true", but with more informative output on failure
@test endswith("($(cell.code)) == $(cell.output.body)", "true")
end

WorkspaceManager.unmake_workspace((🍭, notebook))
Expand Down

0 comments on commit c2447ab

Please sign in to comment.