/
capture_io.ex
283 lines (220 loc) · 8.48 KB
/
capture_io.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
defmodule ExUnit.CaptureIO do
@moduledoc ~S"""
Functionality to capture IO for testing.
## Examples
defmodule AssertionTest do
use ExUnit.Case
import ExUnit.CaptureIO
test "example" do
assert capture_io(fn -> IO.puts("a") end) == "a\n"
end
test "another example" do
assert with_io(fn ->
IO.puts("a")
IO.puts("b")
2 + 2
end) == {4, "a\nb\n"}
end
end
"""
@doc """
Captures IO generated when evaluating `fun`.
Returns the binary which is the captured output.
By default, `capture_io` replaces the `group_leader` (`:stdio`)
for the current process. Capturing the group leader is done per
process and therefore can be done concurrently.
However, the capturing of any other named device, such as `:stderr`,
happens globally and persists until the function has ended. While this means
it is safe to run your tests with `async: true` in many cases, captured output
may include output from a different test and care must be taken when using
`capture_io` with a named process asynchronously.
A developer can set a string as an input. The default input is an empty
string. If capturing a named device asynchronously, an input can only be given
to the first capture. Any further capture that is given to a capture on that
device will raise an exception and would indicate that the test should be run
synchronously.
Similarly, once a capture on a named device has begun, the encoding on that
device cannot be changed in a subsequent concurrent capture. An error will
be raised in this case.
## IO devices
You may capture the IO from any registered IO device. The device name given
must be an atom representing the name of a registered process. In addition,
Elixir provides two shortcuts:
* `:stdio` - a shortcut for `:standard_io`, which maps to
the current `Process.group_leader/0` in Erlang
* `:stderr` - a shortcut for the named process `:standard_error`
provided in Erlang
## Options
* `:input` - An input to the IO device, defaults to `""`.
* `:capture_prompt` - Define if prompts (specified as arguments to
`IO.get*` functions) should be captured. Defaults to `true`. For
IO devices other than `:stdio`, the option is ignored.
* `:encoding` (since v1.10.0) - encoding of the IO device. Allowed
values are `:unicode` (default) and `:latin1`.
## Examples
iex> capture_io(fn -> IO.write("john") end) == "john"
true
iex> capture_io(:stderr, fn -> IO.write(:stderr, "john") end) == "john"
true
iex> capture_io(:standard_error, fn -> IO.write(:stderr, "john") end) == "john"
true
iex> capture_io("this is input", fn ->
...> input = IO.gets("> ")
...> IO.write(input)
...> end) == "> this is input"
true
iex> capture_io([input: "this is input", capture_prompt: false], fn ->
...> input = IO.gets("> ")
...> IO.write(input)
...> end) == "this is input"
true
## Returning values
As seen in the examples above, `capture_io` returns the captured output.
If you want to also capture the result of the function executed,
use `with_io/2`.
"""
@spec capture_io((() -> any())) :: String.t()
def capture_io(fun) when is_function(fun, 0) do
{_result, capture} = with_io(fun)
capture
end
@doc """
Captures IO generated when evaluating `fun`.
See `capture_io/1` for more information.
"""
@spec capture_io(atom() | String.t() | keyword(), (() -> any())) :: String.t()
def capture_io(device_input_or_options, fun)
def capture_io(device, fun) when is_atom(device) and is_function(fun, 0) do
{_result, capture} = with_io(device, fun)
capture
end
def capture_io(input, fun) when is_binary(input) and is_function(fun, 0) do
{_result, capture} = with_io(input, fun)
capture
end
def capture_io(options, fun) when is_list(options) and is_function(fun, 0) do
{_result, capture} = with_io(options, fun)
capture
end
@doc """
Captures IO generated when evaluating `fun`.
See `capture_io/1` for more information.
"""
@spec capture_io(atom(), String.t() | keyword(), (() -> any())) :: String.t()
def capture_io(device, input_or_options, fun)
def capture_io(device, input, fun)
when is_atom(device) and is_binary(input) and is_function(fun, 0) do
{_result, capture} = with_io(device, input, fun)
capture
end
def capture_io(device, options, fun)
when is_atom(device) and is_list(options) and is_function(fun, 0) do
{_result, capture} = with_io(device, options, fun)
capture
end
@doc ~S"""
Invokes the given `fun` and returns the result and captured output.
It accepts the same arguments and options as `capture_io/1`.
## Examples
{result, output} =
assert with_io(fn ->
IO.puts("a")
IO.puts("b")
2 + 2
end)
assert result == 4
assert output == "a\nb\n"
"""
@doc since: "1.13.0"
@spec with_io((() -> any())) :: {any(), String.t()}
def with_io(fun) when is_function(fun, 0) do
with_io(:stdio, [], fun)
end
@doc """
Invokes the given `fun` and returns the result and captured output.
See `with_io/1` for more information.
"""
@doc since: "1.13.0"
@spec with_io(atom() | String.t() | keyword(), (() -> any())) :: {any(), String.t()}
def with_io(device_input_or_options, fun)
def with_io(device, fun) when is_atom(device) and is_function(fun, 0) do
with_io(device, [], fun)
end
def with_io(input, fun) when is_binary(input) and is_function(fun, 0) do
with_io(:stdio, [input: input], fun)
end
def with_io(options, fun) when is_list(options) and is_function(fun, 0) do
with_io(:stdio, options, fun)
end
@doc """
Invokes the given `fun` and returns the result and captured output.
See `with_io/1` for more information.
"""
@doc since: "1.13.0"
@spec with_io(atom(), String.t() | keyword(), (() -> any())) :: {any(), String.t()}
def with_io(device, input_or_options, fun)
def with_io(device, input, fun)
when is_atom(device) and is_binary(input) and is_function(fun, 0) do
with_io(device, [input: input], fun)
end
def with_io(device, options, fun)
when is_atom(device) and is_list(options) and is_function(fun, 0) do
do_with_io(map_dev(device), options, fun)
end
defp map_dev(:stdio), do: :standard_io
defp map_dev(:stderr), do: :standard_error
defp map_dev(other), do: other
defp do_with_io(:standard_io, options, fun) do
prompt_config = Keyword.get(options, :capture_prompt, true)
encoding = Keyword.get(options, :encoding, :unicode)
input = Keyword.get(options, :input, "")
original_gl = Process.group_leader()
{:ok, capture_gl} = StringIO.open(input, capture_prompt: prompt_config, encoding: encoding)
try do
Process.group_leader(self(), capture_gl)
do_capture_gl(capture_gl, fun)
after
Process.group_leader(self(), original_gl)
end
end
defp do_with_io(device, options, fun) do
input = Keyword.get(options, :input, "")
encoding = Keyword.get(options, :encoding, :unicode)
case ExUnit.CaptureServer.device_capture_on(device, encoding, input) do
{:ok, ref} ->
try do
result = fun.()
{result, ExUnit.CaptureServer.device_output(device, ref)}
after
ExUnit.CaptureServer.device_capture_off(ref)
end
{:error, :no_device} ->
raise "could not find IO device registered at #{inspect(device)}"
{:error, {:changed_encoding, current_encoding}} ->
raise ArgumentError, """
attempted to change the encoding for a currently captured device #{inspect(device)}.
Currently set as: #{inspect(current_encoding)}
Given: #{inspect(encoding)}
If you need to use multiple encodings on a captured device, you cannot \
run your test asynchronously
"""
{:error, :input_on_already_captured_device} ->
raise ArgumentError,
"attempted multiple captures on device #{inspect(device)} with input. " <>
"If you need to give an input to a captured device, you cannot run your test asynchronously"
end
end
defp do_capture_gl(string_io, fun) do
try do
fun.()
catch
kind, reason ->
_ = StringIO.close(string_io)
:erlang.raise(kind, reason, __STACKTRACE__)
else
result ->
{:ok, {_input, output}} = StringIO.close(string_io)
{result, output}
end
end
end