From dbda334f463b0033c5514e17c9afa52be6e00012 Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Wed, 11 Oct 2017 12:59:27 -0700 Subject: [PATCH 1/2] Run the code formatter on ExUnit.CaptureIOTest --- lib/ex_unit/test/ex_unit/capture_io_test.exs | 148 ++++++++++--------- 1 file changed, 82 insertions(+), 66 deletions(-) diff --git a/lib/ex_unit/test/ex_unit/capture_io_test.exs b/lib/ex_unit/test/ex_unit/capture_io_test.exs index 827846bb4da..0436e5f9c3f 100644 --- a/lib/ex_unit/test/ex_unit/capture_io_test.exs +++ b/lib/ex_unit/test/ex_unit/capture_io_test.exs @@ -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 @@ -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 @@ -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()}) @@ -44,6 +48,7 @@ defmodule ExUnit.CaptureIOTest do ref = Process.monitor(pid) assert_receive {:DOWN, ^ref, _, _, _} end + assert Process.group_leader() == group_leader end @@ -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 @@ -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 @@ -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} @@ -242,65 +247,70 @@ 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" + + requests = [{:put_chars, :unicode, "a"}, {:put_chars, :unicode, "b"}] 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} @@ -308,12 +318,13 @@ defmodule ExUnit.CaptureIOTest do 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 @@ -337,15 +348,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 From fdd31f1c113312a790de29e8327e8799fc4d6e3d Mon Sep 17 00:00:00 2001 From: Michael Oliver Date: Wed, 11 Oct 2017 13:07:49 -0700 Subject: [PATCH 2/2] Remove redundant line --- lib/ex_unit/test/ex_unit/capture_io_test.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/ex_unit/test/ex_unit/capture_io_test.exs b/lib/ex_unit/test/ex_unit/capture_io_test.exs index 0436e5f9c3f..9bd02db101e 100644 --- a/lib/ex_unit/test/ex_unit/capture_io_test.exs +++ b/lib/ex_unit/test/ex_unit/capture_io_test.exs @@ -300,8 +300,6 @@ defmodule ExUnit.CaptureIOTest do send_and_receive_io({:requests, requests}) end) == "ab" - requests = [{:put_chars, :unicode, "a"}, {:put_chars, :unicode, "b"}] - capture_io(fn -> assert send_and_receive_io({:requests, requests}) == :ok end)