forked from elixir-lang/elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_io_test.exs
545 lines (431 loc) · 14.2 KB
/
capture_io_test.exs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
Code.require_file("../test_helper.exs", __DIR__)
defmodule ExUnit.CaptureIOTest do
use ExUnit.Case
defmodule GetUntil do
def until_new_line(_, :eof, _) do
{:done, :eof, []}
end
def until_new_line(this_far, chars, stop_char) do
case Enum.split_while(chars, fn c -> c != stop_char end) do
{l, []} ->
{:more, this_far ++ l}
{l, [stop_char | rest]} ->
{:done, this_far ++ l ++ [stop_char], rest}
end
end
def get_line(device \\ Process.group_leader()) do
request = {:get_until, :unicode, "", __MODULE__, :until_new_line, [?\n]}
send(device, {:io_request, self(), device, request})
receive do
{:io_reply, _, data} -> data
end
end
end
defmodule MockProc do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, [])
end
@impl GenServer
def init(_), do: {:ok, nil}
@impl GenServer
def handle_call({:stdio, message}, _from, state) do
IO.puts(message)
{:reply, :ok, state}
end
@impl GenServer
def handle_call({:prompt, prompt}, _from, state) do
prompt
|> IO.gets()
|> IO.puts()
{:reply, :ok, state}
end
@impl GenServer
def handle_call({:stderr, message}, _from, state) do
IO.puts(:stderr, message)
{:reply, :ok, state}
end
end
import ExUnit.CaptureIO
doctest ExUnit.CaptureIO, import: true
describe "I/O protocol" do
test "with put chars" do
assert capture_io(fn ->
:io.put_chars("")
end) == ""
assert capture_io(fn ->
:io.put_chars("a")
:io.put_chars("b")
end) == "ab"
assert capture_io(fn ->
:io.put_chars("josé")
end) == "josé"
assert capture_io(fn ->
spawn(fn -> :io.put_chars("a") end)
Process.sleep(10)
end) == "a"
assert capture_io(fn ->
assert :io.put_chars("a") == :ok
end)
end
test "with fwrite" do
assert capture_io(fn ->
:io.fwrite(<<127, 128>>)
end) == <<127, 194, 128>>
assert capture_io([encoding: :latin1], fn ->
:io.fwrite(<<127, 128>>)
end) == <<127, 128>>
end
test "with get chars" do
assert capture_io(fn ->
:io.get_chars(">", 3)
end) == ">"
assert capture_io([capture_prompt: false], fn ->
:io.get_chars(">", 3)
end) == ""
capture_io(fn ->
assert :io.get_chars(">", 3) == :eof
end)
capture_io("", fn ->
assert :io.get_chars(">", 3) == :eof
end)
capture_io("abc\ndef", fn ->
assert :io.get_chars(">", 3) == "abc"
assert :io.get_chars(">", 5) == "\ndef"
assert :io.get_chars(">", 7) == :eof
end)
capture_io("あいう", fn ->
assert :io.get_chars(">", 2) == "あい"
assert :io.get_chars(">", 1) == "う"
assert :io.get_chars(">", 1) == :eof
end)
end
test "with get line" do
assert capture_io(fn ->
:io.get_line(">")
end) == ">"
assert capture_io([capture_prompt: false], fn ->
:io.get_line(">")
end) == ""
capture_io(fn ->
assert :io.get_line(">") == :eof
end)
capture_io("", fn ->
assert :io.get_line(">") == :eof
end)
capture_io("\n", fn ->
assert :io.get_line(">") == "\n"
assert :io.get_line(">") == :eof
end)
capture_io("a", fn ->
assert :io.get_line(">") == "a"
assert :io.get_line(">") == :eof
end)
capture_io("a\n", fn ->
assert :io.get_line(">") == "a\n"
assert :io.get_line(">") == :eof
end)
capture_io("a\nb", fn ->
assert :io.get_line(">") == "a\n"
assert :io.get_line(">") == "b"
assert :io.get_line(">") == :eof
end)
capture_io("あい\nう", fn ->
assert :io.get_line(">") == "あい\n"
assert :io.get_line(">") == "う"
assert :io.get_line(">") == :eof
end)
end
test "with get password" do
capture_io(fn ->
assert :io.get_password() == :eof
end)
capture_io("", fn ->
assert :io.get_password() == :eof
end)
capture_io("abc", fn ->
assert :io.get_password() == "abc"
assert :io.get_password() == :eof
end)
capture_io("abc\n", fn ->
assert :io.get_password() == "abc\n"
assert :io.get_password() == :eof
end)
capture_io("\n", fn ->
assert :io.get_password() == "\n"
assert :io.get_password() == :eof
end)
capture_io("a\nb", fn ->
assert :io.get_password() == "a\n"
assert :io.get_password() == "b"
assert :io.get_password() == :eof
end)
capture_io("あい\nう", fn ->
assert :io.get_password() == "あい\n"
assert :io.get_password() == "う"
assert :io.get_password() == :eof
end)
end
test "with get until" do
assert capture_io(fn ->
:io.scan_erl_form(~c">")
end) == ">"
assert capture_io("1.\n", fn ->
:io.scan_erl_form(~c">")
end) == ">"
assert capture_io("1\n.\n", fn ->
:io.scan_erl_form(~c">")
end) == ">>"
assert capture_io([capture_prompt: false], fn ->
:io.scan_erl_form(~c">")
end) == ""
capture_io(fn ->
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("1", fn ->
assert :io.scan_erl_form(~c">") == {:ok, [{:integer, 1, 1}], 1}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("1\n.", fn ->
assert :io.scan_erl_form(~c">") == {:ok, [{:integer, 1, 1}, {:dot, 2}], 2}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("1.\n.", fn ->
assert :io.scan_erl_form(~c">") == {:ok, [{:integer, 1, 1}, {:dot, 1}], 2}
assert :io.scan_erl_form(~c">") == {:ok, [dot: 1], 1}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("\"a", fn ->
# TODO: Remove me when we require Erlang/OTP 27+
expected_error =
if :erlang.system_info(:otp_release) >= ~c"27" do
{1, :erl_scan, {:unterminated, :string, ~c"a"}}
else
{1, :erl_scan, {:string, 34, ~c"a"}}
end
assert :io.scan_erl_form(~c">") == {:error, expected_error, 1}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("\"a\n\"", fn ->
assert :io.scan_erl_form(~c">") == {:ok, [{:string, 1, ~c"a\n"}], 2}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io(":erl. mof*,,l", fn ->
assert :io.scan_erl_form(~c">") == {:ok, [{:":", 1}, {:atom, 1, :erl}, {:dot, 1}], 1}
expected_tokens = [{:atom, 1, :mof}, {:*, 1}, {:",", 1}, {:",", 1}, {:atom, 1, :l}]
assert :io.scan_erl_form(~c">") == {:ok, expected_tokens, 1}
assert :io.scan_erl_form(~c">") == {:eof, 1}
end)
capture_io("a\nb\nc", fn ->
assert GetUntil.get_line() == "a\n"
assert GetUntil.get_line() == "b\n"
assert GetUntil.get_line() == :eof
end)
end
test "with setopts" do
assert capture_io(fn ->
assert :io.setopts({:encoding, :latin1}) == {:error, :enotsup}
end) == ""
end
test "with getopts" do
assert capture_io(fn ->
assert :io.getopts() == [binary: true, encoding: :unicode]
end) == ""
end
test "with columns" do
assert capture_io(fn ->
:io.columns()
end) == ""
capture_io(fn ->
assert :io.columns() == {:error, :enotsup}
end)
end
test "with rows" do
assert capture_io(fn ->
:io.rows()
end) == ""
capture_io(fn ->
assert :io.rows() == {:error, :enotsup}
end)
end
test "with multiple requests" do
requests = [{:put_chars, :unicode, "a"}, {:put_chars, :unicode, "b"}]
assert capture_io(fn ->
send_and_receive_io({:requests, requests})
end) == "ab"
capture_io(fn ->
assert send_and_receive_io({:requests, requests}) == :ok
end)
end
test "with unknown request" do
assert capture_io(fn ->
send_and_receive_io(:unknown)
end) == ""
capture_io(fn ->
assert send_and_receive_io(:unknown) == {:error, :request}
end)
end
end
describe "stdin" do
test "double capture from the same process" do
assert capture_io(fn ->
IO.puts("hello")
assert capture_io(fn -> IO.puts("middle") end) == "middle\n"
IO.puts("world")
end) == "hello\nworld\n"
end
test "no leakage on failures" do
group_leader = Process.group_leader()
test = self()
assert_raise ArgumentError, fn ->
capture_io(fn ->
send(test, {:string_io, Process.group_leader()})
raise ArgumentError
end)
end
receive do
{:string_io, pid} ->
ref = Process.monitor(pid)
assert_receive {:DOWN, ^ref, _, _, _}
end
assert Process.group_leader() == group_leader
end
end
describe "stderr" do
test "supports writes" do
assert capture_io(:stderr, fn ->
:io.put_chars(:standard_error, "a")
end) == "a"
end
test "double capture from the same process" do
assert capture_io(:stderr, fn ->
IO.puts(:stderr, "hello")
assert capture_io(:stderr, fn -> IO.puts(:stderr, "middle") end) == "middle\n"
IO.puts(:stderr, "world")
end) == "hello\nmiddle\nworld\n"
end
test "supports async capture_io" do
parent = self()
[pid1, pid2, pid3] =
for num <- 1..3 do
pid =
spawn_link(fn ->
captured =
capture_io(:stderr, fn ->
:io.put_chars(:standard_error, "before:#{num}\n")
send(parent, {self(), :logged})
assert_receive :continue
:io.put_chars(:standard_error, "after:#{num}\n")
end)
send(parent, captured)
end)
assert_receive {^pid, :logged}
pid
end
send(pid3, :continue)
assert_receive "before:3\nafter:3\n"
send(pid2, :continue)
assert_receive "before:2\nbefore:3\nafter:3\nafter:2\n"
send(pid1, :continue)
assert_receive "before:1\nbefore:2\nbefore:3\nafter:3\nafter:2\nafter:1\n"
end
test "raises when async capturing a named device with a different encoding than the first" do
parent = self()
pid =
spawn_link(fn ->
output =
capture_io(:stderr, [encoding: :latin1], fn ->
:io.put_chars(:standard_error, "a")
send(parent, {self(), :logged})
assert_receive :continue
end)
send(parent, output)
end)
assert_receive {^pid, :logged}
assert_raise ArgumentError,
~r"attempted to change the encoding for a currently captured device :standard_error",
fn ->
capture_io(:stderr, [encoding: :unicode], fn ->
:io.put_chars(:standard_error, "b")
end)
end
assert capture_io(:stderr, [encoding: :latin1], fn ->
:io.put_chars(:standard_error, "c")
end) == "c"
send(pid, :continue)
assert_receive "ac"
end
test "raises when async capturing a named device with an input given to an already captured device" do
parent = self()
pid =
spawn_link(fn ->
capture_io(:stderr, [input: "first"], fn ->
send(parent, {self(), :logged})
Process.sleep(:infinity)
end)
end)
assert_receive {^pid, :logged}
message =
"attempted multiple captures on device :standard_error with input. If you need to give an input to a captured device, you cannot run your test asynchronously"
assert_raise ArgumentError, message, fn ->
capture_io(:stderr, [input: "second"], fn ->
:io.put_chars(:standard_error, "b")
end)
end
assert_raise ArgumentError, message, fn ->
capture_io(:stderr, [input: ""], fn ->
:io.put_chars(:standard_error, "b")
end)
end
end
test "no leakage on failures" do
parent = self()
pid =
spawn(fn ->
capture_io(:stderr, [input: "a"], fn ->
send(parent, :ready)
Process.sleep(:infinity)
end)
end)
assert_receive :ready
ref = Process.monitor(pid)
# Kill the process and make sure the capture is released
Process.exit(pid, :shutdown)
# Make sure the process has exited before we try and start a new capture
assert_receive {:DOWN, ^ref, _, _, _}
assert capture_io(:stderr, [input: "b"], fn -> :ok end)
end
end
test "capture_io with a separate process" do
{:ok, pid} = MockProc.start_link()
assert capture_io(pid, fn ->
GenServer.call(pid, {:stdio, "a"})
end) == "a\n"
assert capture_io(pid, [input: "b"], fn ->
GenServer.call(pid, {:prompt, "> "})
end) == "> b\n"
assert capture_io(pid, "c", fn ->
GenServer.call(pid, {:prompt, "> "})
end) == "> c\n"
assert capture_io(pid, [input: "d", capture_prompt: false], fn ->
GenServer.call(pid, {:prompt, "> "})
end) == "d\n"
assert capture_io(:stderr, fn ->
GenServer.call(pid, {:stderr, "uhoh"})
end) == "uhoh\n"
GenServer.stop(pid)
end
test "with_io" do
assert with_io(fn ->
:io.put_chars("xyz")
2 + 2
end) == {4, "xyz"}
end
defp send_and_receive_io(req) do
pid = self()
send(:erlang.group_leader(), {:io_request, pid, pid, req})
receive do
{:io_reply, ^pid, res} -> res
end
end
end