Skip to content

Run the code formatter on ExUnit.CaptureIOTest #6912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 80 additions & 66 deletions lib/ex_unit/test/ex_unit/capture_io_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Code.require_file "../test_helper.exs", __DIR__
Code.require_file("../test_helper.exs", __DIR__)

defmodule ExUnit.CaptureIOTest do
use ExUnit.Case
Expand All @@ -9,16 +9,19 @@ defmodule ExUnit.CaptureIOTest do
end

def until_new_line(this_far, chars, stop_char) do
case Enum.split_while(chars, fn(c) -> c != stop_char end) 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
send device, {:io_request, self(), device, {:get_until, :unicode, "", __MODULE__, :until_new_line, [?\n]}}
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
Expand All @@ -32,6 +35,7 @@ defmodule ExUnit.CaptureIOTest do
group_leader = Process.group_leader()

test = self()

assert_raise ArgumentError, fn ->
capture_io(fn ->
send(test, {:string_io, Process.group_leader()})
Expand All @@ -44,6 +48,7 @@ defmodule ExUnit.CaptureIOTest do
ref = Process.monitor(pid)
assert_receive {:DOWN, ^ref, _, _, _}
end

assert Process.group_leader() == group_leader
end

Expand All @@ -53,42 +58,42 @@ defmodule ExUnit.CaptureIOTest do

test "with put chars" do
assert capture_io(fn ->
:io.put_chars("")
end) == ""
:io.put_chars("")
end) == ""

assert capture_io(fn ->
:io.put_chars("a")
:io.put_chars("b")
end) == "ab"
:io.put_chars("a")
:io.put_chars("b")
end) == "ab"

assert capture_io(fn ->
:io.put_chars("josé")
end) == "josé"
:io.put_chars("josé")
end) == "josé"

assert capture_io(fn ->
spawn(fn -> :io.put_chars("a") end)
Process.sleep(10)
end) == "a"
spawn(fn -> :io.put_chars("a") end)
Process.sleep(10)
end) == "a"

assert capture_io(fn ->
assert :io.put_chars("a") == :ok
end)
assert :io.put_chars("a") == :ok
end)
end

test "with put chars to stderr" do
assert capture_io(:stderr, fn ->
:io.put_chars(:standard_error, "a")
end) == "a"
:io.put_chars(:standard_error, "a")
end) == "a"
end

test "with get chars" do
assert capture_io(fn ->
:io.get_chars(">", 3)
end) == ">"
:io.get_chars(">", 3)
end) == ">"

assert capture_io([capture_prompt: false], fn ->
:io.get_chars(">", 3)
end) == ""
:io.get_chars(">", 3)
end) == ""

capture_io(fn ->
assert :io.get_chars(">", 3) == :eof
Expand All @@ -113,12 +118,12 @@ defmodule ExUnit.CaptureIOTest do

test "with get line" do
assert capture_io(fn ->
:io.get_line ">"
end) == ">"
:io.get_line(">")
end) == ">"

assert capture_io([capture_prompt: false], fn ->
:io.get_line ">"
end) == ""
:io.get_line(">")
end) == ""

capture_io(fn ->
assert :io.get_line(">") == :eof
Expand Down Expand Up @@ -195,29 +200,29 @@ defmodule ExUnit.CaptureIOTest do

test "with get until" do
assert capture_io(fn ->
:io.scan_erl_form('>')
end) == ">"
:io.scan_erl_form('>')
end) == ">"

assert capture_io("1.\n", fn ->
:io.scan_erl_form('>')
end) == ">"
:io.scan_erl_form('>')
end) == ">"

assert capture_io("1\n.\n", fn ->
:io.scan_erl_form('>')
end) == ">>"
:io.scan_erl_form('>')
end) == ">>"

assert capture_io([capture_prompt: false], fn ->
:io.scan_erl_form('>')
end) == ""
:io.scan_erl_form('>')
end) == ""

capture_io(fn ->
assert :io.scan_erl_form('>') == {:eof, 1}
end)

capture_io("1", fn ->
assert :io.scan_erl_form('>') == {:ok, [{:integer, 1, 1}], 1}
assert :io.scan_erl_form('>') == {:eof, 1}
end)
capture_io("1", fn ->
assert :io.scan_erl_form('>') == {:ok, [{:integer, 1, 1}], 1}
assert :io.scan_erl_form('>') == {:eof, 1}
end)

capture_io("1\n.", fn ->
assert :io.scan_erl_form('>') == {:ok, [{:integer, 1, 1}, {:dot, 2}], 2}
Expand All @@ -242,78 +247,82 @@ defmodule ExUnit.CaptureIOTest do

capture_io(":erl. mof*,,l", fn ->
assert :io.scan_erl_form('>') == {:ok, [{:":", 1}, {:atom, 1, :erl}, {:dot, 1}], 1}
assert :io.scan_erl_form('>') == {:ok, [{:atom, 1, :mof}, {:*, 1}, {:",", 1}, {:",", 1}, {:atom, 1, :l}], 1}

expected_tokens = [{:atom, 1, :mof}, {:*, 1}, {:",", 1}, {:",", 1}, {:atom, 1, :l}]
assert :io.scan_erl_form('>') == {:ok, expected_tokens, 1}

assert :io.scan_erl_form('>') == {: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
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) == ""
assert :io.setopts({:encoding, :latin1}) == {:error, :enotsup}
end) == ""
end

test "with getopts" do
assert capture_io(fn ->
assert :io.getopts == {:ok, [binary: true, encoding: :unicode]}
end) == ""
assert :io.getopts() == {:ok, [binary: true, encoding: :unicode]}
end) == ""
end

test "with columns" do
assert capture_io(fn ->
:io.columns
end) == ""
:io.columns()
end) == ""

capture_io(fn ->
assert :io.columns == {:error, :enotsup}
assert :io.columns() == {:error, :enotsup}
end)
end

test "with rows" do
assert capture_io(fn ->
:io.rows
end) == ""
:io.rows()
end) == ""

capture_io(fn ->
assert :io.rows == {:error, :enotsup}
assert :io.rows() == {:error, :enotsup}
end)
end

test "with multiple IO requests" do
requests = [{:put_chars, :unicode, "a"}, {:put_chars, :unicode, "b"}]

assert capture_io(fn ->
send_and_receive_io({:requests, [{:put_chars, :unicode, "a"},
{:put_chars, :unicode, "b"}]})
end) == "ab"
send_and_receive_io({:requests, requests})
end) == "ab"

capture_io(fn ->
assert send_and_receive_io({:requests, [{:put_chars, :unicode, "a"},
{:put_chars, :unicode, "b"}]}) == :ok
assert send_and_receive_io({:requests, requests}) == :ok
end)
end

test "with unknown IO request" do
assert capture_io(fn ->
send_and_receive_io(:unknown)
end) == ""
send_and_receive_io(:unknown)
end) == ""

capture_io(fn ->
assert send_and_receive_io(:unknown) == {:error, :request}
end)
end

test "device re-registering" do
{_pid, ref} = spawn_monitor(fn ->
capture_io(:stderr, fn ->
spawn_link(Kernel, :exit, [:shutdown])
Process.sleep(:infinity)
{_pid, ref} =
spawn_monitor(fn ->
capture_io(:stderr, fn ->
spawn_link(Kernel, :exit, [:shutdown])
Process.sleep(:infinity)
end)
end)
end)

# Assert the process is down then invoke capture_io
# to trigger the ExUnit.Server, ensuring the DOWN
Expand All @@ -337,15 +346,20 @@ defmodule ExUnit.CaptureIOTest do
test "capture :stderr by two processes" do
spawn(fn -> capture_io(:stderr, fn -> Process.sleep(100) end) end)
Process.sleep(10)
assert_raise RuntimeError, "IO device registered at :standard_error is already captured", fn ->

expected_message = "IO device registered at :standard_error is already captured"

assert_raise RuntimeError, expected_message, fn ->
capture_io(:stderr, fn -> nil end)
end

Process.sleep(100)
end

defp send_and_receive_io(req) do
pid = self()
send :erlang.group_leader, {:io_request, pid, pid, req}
send(:erlang.group_leader(), {:io_request, pid, pid, req})

receive do
{:io_reply, ^pid, res} -> res
end
Expand Down