Skip to content

Commit 2ccb546

Browse files
Add io_context keyword argument (#26)
1 parent 5fb07e3 commit 2ccb546

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
44
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## Version [v0.2.5] - 2024-06-12
7+
8+
### Added
9+
10+
* `iocapture` now accepts an `io_context` keyword argument that is passed to the `IOContext` that wraps `stdout` and `stderr`. ([#26])
11+
612
## Version [v0.2.4] - 2023-01-17
713

814
### Added
@@ -65,6 +71,7 @@ Initial release exporting the `iocapture` function.
6571
[v0.2.2]: https://github.com/JuliaDocs/IOCapture.jl/releases/tag/v0.2.2
6672
[v0.2.3]: https://github.com/JuliaDocs/IOCapture.jl/releases/tag/v0.2.3
6773
[v0.2.4]: https://github.com/JuliaDocs/IOCapture.jl/releases/tag/v0.2.4
74+
[v0.2.5]: https://github.com/JuliaDocs/IOCapture.jl/releases/tag/v0.2.5
6875
[#1]: https://github.com/JuliaDocs/IOCapture.jl/issues/1
6976
[#2]: https://github.com/JuliaDocs/IOCapture.jl/issues/2
7077
[#3]: https://github.com/JuliaDocs/IOCapture.jl/issues/3
@@ -76,4 +83,5 @@ Initial release exporting the `iocapture` function.
7683
[#20]: https://github.com/JuliaDocs/IOCapture.jl/issues/20
7784
[#21]: https://github.com/JuliaDocs/IOCapture.jl/issues/21
7885
[#23]: https://github.com/JuliaDocs/IOCapture.jl/issues/23
86+
[#26]: https://github.com/JuliaDocs/IOCapture.jl/issues/26
7987
[fredrikekre/Literate.jl#138]: https://github.com/fredrikekre/Literate.jl/issues/138

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IOCapture"
22
uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89"
33
authors = ["Morten Piibeleht", "Michael Hatherly", "IOCapture contributors"]
4-
version = "0.2.4"
4+
version = "0.2.5"
55

66
[deps]
77
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"

src/IOCapture.jl

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Random
44

55
"""
66
IOCapture.capture(
7-
f; rethrow=Any, color=false, passthrough=false, capture_buffer=IOBuffer()
7+
f; rethrow=Any, color=false, passthrough=false, capture_buffer=IOBuffer(), io_context=[],
88
)
99
1010
Runs the function `f` and captures the `stdout` and `stderr` outputs, without printing
@@ -39,6 +39,10 @@ The behaviour can be customized with the following keyword arguments:
3939
* `capture_buffer`: The internal buffer used to capture the combined `stdout`
4040
and `stderr`.
4141
42+
* `io_context`: An optional vector of `IOContext` key/value pairs that are passed to
43+
the `IOContext` that wraps the redirected `stdout` and `stderr` streams. This only
44+
has an effect on Julia v1.6 and later.
45+
4246
# Extended help
4347
4448
`capture` works by temporarily redirecting the standard output and error streams
@@ -98,8 +102,13 @@ function capture(
98102
rethrow::Type=Any,
99103
color::Bool=false,
100104
passthrough::Bool=false,
101-
capture_buffer=IOBuffer()
105+
capture_buffer=IOBuffer(),
106+
io_context::AbstractVector=[],
102107
)
108+
if any(x -> !isa(x, Pair{Symbol,<:Any}), io_context)
109+
throw(ArgumentError("`io_context` must be a `Vector` of `Pair{Symbol,<:Any}`."))
110+
end
111+
103112
# Original implementation from Documenter.jl (MIT license)
104113
# Save the default output streams.
105114
default_stdout = stdout
@@ -109,8 +118,8 @@ function capture(
109118
pipe = Pipe()
110119
Base.link_pipe!(pipe; reader_supports_async = true, writer_supports_async = true)
111120
@static if VERSION >= v"1.6.0-DEV.481" # https://github.com/JuliaLang/julia/pull/36688
112-
pe_stdout = IOContext(pipe.in, :color => get(stdout, :color, false) & color)
113-
pe_stderr = IOContext(pipe.in, :color => get(stderr, :color, false) & color)
121+
pe_stdout = IOContext(pipe.in, :color => get(stdout, :color, false) & color, io_context...)
122+
pe_stderr = IOContext(pipe.in, :color => get(stderr, :color, false) & color, io_context...)
114123
else
115124
pe_stdout = pipe.in
116125
pe_stderr = pipe.in

test/runtests.jl

+20
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,25 @@ end
294294
end
295295
end
296296

297+
if VERSION >= v"1.6.0-DEV.481"
298+
@testset "io_context" begin
299+
# Avoids needing to define this at top-level as a `module M` syntax.
300+
M = Module(:M)
301+
Core.eval(M, :(struct T end))
297302

303+
no_io_context = IOCapture.capture() do
304+
println(M.T())
305+
end
306+
@test rstrip(no_io_context.output) == "Main.M.T()"
307+
308+
with_io_context = IOCapture.capture(io_context=[:module => M]) do
309+
println(M.T())
310+
end
311+
@test rstrip(with_io_context.output) == "T()"
312+
313+
@test_throws ArgumentError IOCapture.capture(io_context=["module" => M]) do
314+
println(M.T())
315+
end
316+
end
317+
end
298318
end

0 commit comments

Comments
 (0)