@@ -28,8 +28,9 @@ defmodule ExUnit.CaptureIO do
28
28
named device like `:stderr` is also possible globally by
29
29
giving the registered device name explicitly as argument.
30
30
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.
33
34
34
35
A developer can set a string as an input. The default
35
36
input is `:eof`.
@@ -42,36 +43,49 @@ defmodule ExUnit.CaptureIO do
42
43
true
43
44
iex> capture_io(:stderr, fn -> IO.write(:stderr, "josé") end) == "josé"
44
45
true
45
- iex> capture_io("this is input", fn->
46
+ iex> capture_io("this is input", fn ->
46
47
...> input = IO.gets ">"
47
48
...> IO.write input
48
49
...> end) == ">this is input"
49
50
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
50
56
51
57
"""
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 )
54
60
end
55
61
56
62
def capture_io( device, fun) when is_atom ( device ) do
57
- do_capture_io ( map_dev ( device ) , "" , fun )
63
+ capture_io ( device , [ ] , fun )
58
64
end
59
65
60
66
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 )
62
68
end
63
69
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 )
66
80
end
67
81
68
82
defp map_dev ( :stdio ) , do: :standard_io
69
83
defp map_dev ( :stderr ) , do: :standard_error
70
84
defp map_dev ( other ) , do: other
71
85
72
- defp do_capture_io ( :standard_io , input , fun ) do
86
+ defp do_capture_io ( :standard_io , options , fun ) do
73
87
original_gl = :erlang . group_leader
74
- capture_gl = new_group_leader ( self , input , true )
88
+ capture_gl = new_group_leader ( self , options )
75
89
:erlang . group_leader ( capture_gl , self )
76
90
77
91
try do
@@ -86,13 +100,15 @@ defmodule ExUnit.CaptureIO do
86
100
end
87
101
end
88
102
89
- defp do_capture_io ( device , input , fun ) do
103
+ defp do_capture_io ( device , options , fun ) do
90
104
unless original_io = Process . whereis ( device ) do
91
105
raise "could not find IO device registered at #{ inspect device } "
92
106
end
93
107
108
+ options = Keyword . put ( options , :capture_prompt , false )
109
+
94
110
Process . unregister ( device )
95
- capture_io = new_group_leader ( self , input )
111
+ capture_io = new_group_leader ( self , options )
96
112
Process . register ( capture_io , device )
97
113
98
114
try do
@@ -108,20 +124,19 @@ defmodule ExUnit.CaptureIO do
108
124
end
109
125
end
110
126
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 )
113
129
end
114
130
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
+
116
135
register_input ( input )
117
136
register_prompt_config ( prompt_config )
118
137
group_leader_loop ( runner , :infinity , [ ] )
119
138
end
120
139
121
- defp register_input ( nil ) do
122
- set_input ( nil )
123
- end
124
-
125
140
defp register_input( input) do
126
141
chars = :unicode . characters_to_list ( input )
127
142
set_input ( chars )
0 commit comments