Skip to content

Commit 1fa29be

Browse files
author
Yuki Ito
committed
Option that enable to decide whether capture_io captures a prompt or not
1 parent b9a60b2 commit 1fa29be

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

lib/ex_unit/lib/ex_unit/capture_io.ex

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ defmodule ExUnit.CaptureIO do
2828
named device like `:stderr` is also possible globally by
2929
giving the registered device name explicitly as argument.
3030
31-
When capturing of `:stdio`, this function captures a prompt,
32-
otherwise do not.
31+
When capturing of `:stdio` and the `:capture_prompt` option
32+
is not `false`, this function captures a prompt, otherwise
33+
do not.
3334
3435
A developer can set a string as an input. The default
3536
input is `:eof`.
@@ -42,36 +43,49 @@ defmodule ExUnit.CaptureIO do
4243
true
4344
iex> capture_io(:stderr, fn -> IO.write(:stderr, "josé") end) == "josé"
4445
true
45-
iex> capture_io("this is input", fn->
46+
iex> capture_io("this is input", fn ->
4647
...> input = IO.gets ">"
4748
...> IO.write input
4849
...> end) == ">this is input"
4950
true
51+
iex> capture_io([input: "this is input", capture_prompt: false], fn ->
52+
...> input = IO.gets ">"
53+
...> IO.write input
54+
...> end) == "this is input"
55+
true
5056
5157
"""
52-
def capture_io(device, input, fun) do
53-
do_capture_io(map_dev(device), input, fun)
58+
def capture_io(fun) do
59+
do_capture_io(:standard_io, [], fun)
5460
end
5561

5662
def capture_io(device, fun) when is_atom(device) do
57-
do_capture_io(map_dev(device), "", fun)
63+
capture_io(device, [], fun)
5864
end
5965

6066
def capture_io(input, fun) when is_binary(input) do
61-
do_capture_io(:standard_io, input, fun)
67+
capture_io(:standard_io, [input: input], fun)
6268
end
6369

64-
def capture_io(fun) do
65-
do_capture_io(:standard_io, "", fun)
70+
def capture_io(options, fun) when is_list(options) do
71+
capture_io(:standard_io, options, fun)
72+
end
73+
74+
def capture_io(device, input, fun) when is_binary(input) do
75+
capture_io(device, [input: input], fun)
76+
end
77+
78+
def capture_io(device, options, fun) when is_list(options) do
79+
do_capture_io(map_dev(device), options, fun)
6680
end
6781

6882
defp map_dev(:stdio), do: :standard_io
6983
defp map_dev(:stderr), do: :standard_error
7084
defp map_dev(other), do: other
7185

72-
defp do_capture_io(:standard_io, input, fun) do
86+
defp do_capture_io(:standard_io, options, fun) do
7387
original_gl = :erlang.group_leader
74-
capture_gl = new_group_leader(self, input, true)
88+
capture_gl = new_group_leader(self, options)
7589
:erlang.group_leader(capture_gl, self)
7690

7791
try do
@@ -86,13 +100,15 @@ defmodule ExUnit.CaptureIO do
86100
end
87101
end
88102

89-
defp do_capture_io(device, input, fun) do
103+
defp do_capture_io(device, options, fun) do
90104
unless original_io = Process.whereis(device) do
91105
raise "could not find IO device registered at #{inspect device}"
92106
end
93107

108+
options = Keyword.put(options, :capture_prompt, false)
109+
94110
Process.unregister(device)
95-
capture_io = new_group_leader(self, input)
111+
capture_io = new_group_leader(self, options)
96112
Process.register(capture_io, device)
97113

98114
try do
@@ -108,20 +124,19 @@ defmodule ExUnit.CaptureIO do
108124
end
109125
end
110126

111-
defp new_group_leader(runner, input, prompt_config // false) do
112-
spawn_link(fn -> group_leader_process(runner, input, prompt_config) end)
127+
defp new_group_leader(runner, options) do
128+
spawn_link(fn -> group_leader_process(runner, options) end)
113129
end
114130

115-
defp group_leader_process(runner, input, prompt_config) do
131+
defp group_leader_process(runner, options) do
132+
prompt_config = Keyword.get(options, :capture_prompt, true)
133+
input = Keyword.get(options, :input, "")
134+
116135
register_input(input)
117136
register_prompt_config(prompt_config)
118137
group_leader_loop(runner, :infinity, [])
119138
end
120139

121-
defp register_input(nil) do
122-
set_input(nil)
123-
end
124-
125140
defp register_input(input) do
126141
chars = :unicode.characters_to_list(input)
127142
set_input(chars)

lib/ex_unit/test/ex_unit/capture_io_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ defmodule ExUnit.CaptureIOTest do
8080
:io.get_chars(">", 3)
8181
end) == ">"
8282

83+
assert capture_io([capture_prompt: false], fn ->
84+
:io.get_chars(">", 3)
85+
end) == nil
86+
8387
capture_io(fn ->
8488
assert :io.get_chars(">", 3) == :eof
8589
end)
@@ -106,6 +110,10 @@ defmodule ExUnit.CaptureIOTest do
106110
:io.get_line ">"
107111
end) == ">"
108112

113+
assert capture_io([capture_prompt: false], fn ->
114+
:io.get_line ">"
115+
end) == nil
116+
109117
capture_io(fn ->
110118
assert :io.get_line(">") == :eof
111119
end)
@@ -155,6 +163,10 @@ defmodule ExUnit.CaptureIOTest do
155163
:io.scan_erl_form('>')
156164
end) == ">>"
157165

166+
assert capture_io([capture_prompt: false], fn ->
167+
:io.scan_erl_form('>')
168+
end) == nil
169+
158170
capture_io(fn ->
159171
assert :io.scan_erl_form('>') == { :eof, 1 }
160172
end)

0 commit comments

Comments
 (0)