@@ -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 )
0 commit comments