From 6158785b165c64cc7dcce8cb232368193debf840 Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Tue, 18 Aug 2020 22:16:52 +0200 Subject: [PATCH 1/4] Improve comments Signed-off-by: Mario Uher --- lib/bypass.ex | 30 +++++++++++++----------------- lib/bypass/application.ex | 2 ++ lib/bypass/instance.ex | 2 ++ lib/bypass/plug.ex | 2 ++ test/bypass_test.exs | 36 +++++++++++++----------------------- 5 files changed, 32 insertions(+), 40 deletions(-) diff --git a/lib/bypass.ex b/lib/bypass.ex index 79d58d4..f7aa907 100644 --- a/lib/bypass.ex +++ b/lib/bypass.ex @@ -1,8 +1,8 @@ defmodule Bypass do @moduledoc """ - Bypass provides a quick way to create a custom Plug that can be put - in place instead of an actual HTTP server to return prebaked responses - to client requests. + Bypass provides a quick way to create a custom Plug that can be put in place + instead of an actual HTTP server to return prebaked responses to client + requests. This module is the main interface to the library. """ @@ -10,7 +10,7 @@ defmodule Bypass do defstruct pid: nil, port: nil @typedoc """ - Represents a Bypass server process + Represents a Bypass server process. """ @type t :: %__MODULE__{pid: pid, port: non_neg_integer} @@ -18,11 +18,11 @@ defmodule Bypass do require Logger @doc """ - Starts an Elixir process running a minimal Plug app. The process - is a HTTP handler and listens to requests on a TCP port on localhost. + Starts an Elixir process running a minimal Plug app. The process is a HTTP + handler and listens to requests on a TCP port on localhost. - Use the other functions in this module to declare which requests are - handled and set expectations on the calls. + Use the other functions in this module to declare which requests are handled + and set expectations on the calls. """ def open(opts \\ []) do case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do @@ -38,8 +38,6 @@ defmodule Bypass do end end - # Raise an error if called with an unknown framework - # defp setup_framework_integration(:ex_unit, bypass = %{pid: pid}) do ExUnit.Callbacks.on_exit({Bypass, pid}, fn -> do_verify_expectations(bypass.pid, ExUnit.AssertionError) @@ -47,12 +45,11 @@ defmodule Bypass do end defp setup_framework_integration(:espec, _bypass) do - # Entry point for more advanced ESpec configurations end @doc """ - Can be called to immediately verify if the declared request - expectations have been met. + Can be called to immediately verify if the declared request expectations have + been met. Returns `:ok` on success and raises an error on failure. """ @@ -104,16 +101,15 @@ defmodule Bypass do end @doc """ - Re-opens the TCP socket on the same port. - Blocks until the operation is complete. + Re-opens the TCP socket on the same port. Blocks until the operation is + complete. """ @spec up(Bypass.t()) :: :ok | {:error, :already_up} def up(%Bypass{pid: pid}), do: Bypass.Instance.call(pid, :up) @doc """ - Closes the TCP socket. - Blocks until the operation is complete. + Closes the TCP socket. Blocks until the operation is complete. """ @spec down(Bypass.t()) :: :ok | {:error, :already_down} def down(%Bypass{pid: pid}), diff --git a/lib/bypass/application.ex b/lib/bypass/application.ex index 0b3f461..4b2ab3b 100644 --- a/lib/bypass/application.ex +++ b/lib/bypass/application.ex @@ -1,4 +1,6 @@ defmodule Bypass.Application do + @moduledoc false + use Application def start(_type, _args) do diff --git a/lib/bypass/instance.ex b/lib/bypass/instance.ex index c3d74ef..9cfd09e 100644 --- a/lib/bypass/instance.ex +++ b/lib/bypass/instance.ex @@ -1,4 +1,6 @@ defmodule Bypass.Instance do + @moduledoc false + use GenServer, restart: :transient import Bypass.Utils diff --git a/lib/bypass/plug.ex b/lib/bypass/plug.ex index 4b9bcf8..49372e8 100644 --- a/lib/bypass/plug.ex +++ b/lib/bypass/plug.ex @@ -1,4 +1,6 @@ defmodule Bypass.Plug do + @moduledoc false + def init([pid]), do: pid def call(%{method: method, request_path: request_path} = conn, pid) do diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 9b91498..2b40c1e 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -37,7 +37,6 @@ defmodule BypassTest do defp specify_port(port, expect_fun) do bypass = Bypass.open(port: port) - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn conn -> @@ -62,7 +61,6 @@ defmodule BypassTest do defp down_socket(expect_fun) do bypass = Bypass.open() - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn conn -> Plug.Conn.send_resp(conn, 200, "") end @@ -101,13 +99,12 @@ defmodule BypassTest do defp not_called(expect_fun) do bypass = Bypass.open() - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn _conn -> assert false end ]) - # Override Bypass' on_exit handler + # Override Bypass' on_exit handler. ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn -> exit_result = Bypass.Instance.call(bypass.pid, :on_exit) assert {:error, :not_called, {:any, :any}} = exit_result @@ -125,7 +122,6 @@ defmodule BypassTest do defp pass(expect_fun) do bypass = Bypass.open() - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn _conn -> @@ -152,11 +148,10 @@ defmodule BypassTest do defp closing_in_flight(expect_fun) do bypass = Bypass.open() - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn _conn -> - # mark the request as arrived, since we're shutting it down now + # Mark the request as arrived, since we're shutting it down now. Bypass.pass(bypass) Bypass.down(bypass) end @@ -179,7 +174,6 @@ defmodule BypassTest do ref = make_ref() bypass = Bypass.open() - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, fn conn -> @@ -192,8 +186,8 @@ defmodule BypassTest do assert {:ok, 200, ""} = request(bypass.port) - # Here we make sure that Bypass.down waits until the plug process finishes its work - # before shutting down + # Here we make sure that Bypass.down waits until the plug process finishes + # its work before shutting down. refute_received ^ref Bypass.down(bypass) assert_received ^ref @@ -236,8 +230,8 @@ defmodule BypassTest do end) end) - # Here we make sure that Bypass.down waits until the plug process finishes its work - # before shutting down + # Here we make sure that Bypass.down waits until the plug process finishes + # its work before shutting down. refute_received ^ref :timer.sleep(200) Bypass.down(bypass) @@ -256,7 +250,7 @@ defmodule BypassTest do assert {:ok, 500, ""} = request(bypass.port) end) - # Override Bypass' on_exit handler + # Override Bypass' on_exit handler. ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn -> exit_result = Bypass.Instance.call(bypass.pid, :on_exit) assert {:error, :unexpected_request, {:any, :any}} = exit_result @@ -282,7 +276,7 @@ defmodule BypassTest do assert_receive :request_received end) - # Override Bypass' on_exit handler + # Override Bypass' on_exit handler. ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn -> :ok == Bypass.Instance.call(bypass.pid, :on_exit) end) @@ -303,7 +297,7 @@ defmodule BypassTest do assert_receive :request_received refute_receive :request_received - # Override Bypass' on_exit handler + # Override Bypass' on_exit handler. ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn -> exit_result = Bypass.Instance.call(bypass.pid, :on_exit) assert {:error, :too_many_requests, {:any, :any}} = exit_result @@ -347,7 +341,6 @@ defmodule BypassTest do method = "POST" path = "/this" - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, method, @@ -382,7 +375,6 @@ defmodule BypassTest do pattern = "/this/:resource/get/:id" path = "/this/my_resource/get/1234" - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, method, @@ -421,7 +413,6 @@ defmodule BypassTest do paths = ["/this", "/that"] Enum.each(paths, fn path -> - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, method, @@ -448,7 +439,7 @@ defmodule BypassTest do @doc ~S""" Open a new HTTP connection and perform the request. We don't want to use httpc, hackney or another "high-level" HTTP client, since they do connection pooling and we will sometimes get a connection - closed error and not a failed to connect error, when we test Bypass.down + closed error and not a failed to connect error, when we test Bypass.down. """ def request(port, path \\ "/example_path", method \\ "POST") do with {:ok, conn} <- Mint.HTTP.connect(:http, "127.0.0.1", port), @@ -490,11 +481,11 @@ defmodule BypassTest do end end - test "Bypass.expect/4 can be used to define a specific route and then redefined" do + test "Bypass.expect/4 can be used to define a specific route and then redefine it later" do :expect |> specific_route_redefined end - test "Bypass.expect_once/4 can be used to define a specific route and then redefined" do + test "Bypass.expect_once/4 can be used to define a specific route and then redefine it later" do :expect_once |> specific_route_redefined end @@ -503,7 +494,6 @@ defmodule BypassTest do method = "POST" path = "/this" - # one of Bypass.expect or Bypass.expect_once apply(Bypass, expect_fun, [ bypass, method, @@ -519,7 +509,7 @@ defmodule BypassTest do assert {:ok, 200, ""} = request(bypass.port, path) end) - # redefining the expect + # Redfine the expect apply(Bypass, expect_fun, [ bypass, method, From a6295b81fbd531650661de2f76451c913eaeb78e Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Tue, 18 Aug 2020 22:19:45 +0200 Subject: [PATCH 2/4] Remove Elixir 1.0 related code --- test/bypass_test.exs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 2b40c1e..128a0d2 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -2,18 +2,7 @@ defmodule BypassTest do use ExUnit.Case doctest Bypass - if Code.ensure_loaded?(ExUnit.CaptureLog) do - defdelegate capture_log(fun), to: ExUnit.CaptureLog - else - # Shim capture_log for Elixir 1.0 - defp capture_log(fun) do - ExUnit.CaptureIO.capture_io(:user, fn -> - fun.() - Logger.flush() - end) - |> String.strip() - end - end + defdelegate capture_log(fun), to: ExUnit.CaptureLog test "show ISSUE #51" do Enum.each( From b71c8fee7c9328ccae47e7a4ef0564a817ef2119 Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Tue, 18 Aug 2020 22:28:37 +0200 Subject: [PATCH 3/4] Fix some issues reported by credo --- lib/bypass/instance.ex | 4 ++-- test/bypass_test.exs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/bypass/instance.ex b/lib/bypass/instance.ex index 9cfd09e..80d87f1 100644 --- a/lib/bypass/instance.ex +++ b/lib/bypass/instance.ex @@ -266,7 +266,7 @@ defmodule Bypass.Instance do problem_route = expectations |> Enum.reject(fn {_route, expectations} -> expectations[:expected] == :none_or_more end) - |> Enum.find(fn {_route, expectations} -> length(expectations.results) == 0 end) + |> Enum.find(fn {_route, expectations} -> Enum.empty?(expectations.results) end) case problem_route do {route, _} -> @@ -436,7 +436,7 @@ defmodule Bypass.Instance do defp so_reuseport() do case :os.type() do {:unix, :linux} -> [{:raw, 1, 15, <<1::32-native>>}] - {:unix, :darwin} -> [{:raw, 65535, 512, <<1::32-native>>}] + {:unix, :darwin} -> [{:raw, 65_535, 512, <<1::32-native>>}] _ -> [] end end diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 128a0d2..642c841 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -432,7 +432,7 @@ defmodule BypassTest do """ def request(port, path \\ "/example_path", method \\ "POST") do with {:ok, conn} <- Mint.HTTP.connect(:http, "127.0.0.1", port), - {:ok, conn, ref} = Mint.HTTP.request(conn, method, path, [], "") do + {:ok, conn, ref} <- Mint.HTTP.request(conn, method, path, [], "") do receive_responses(conn, ref, 100, []) end end From 8d096da84b5350bac69884c1be47e90c0b941db0 Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Wed, 19 Aug 2020 14:08:23 +0200 Subject: [PATCH 4/4] Fix a typo --- test/bypass_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 642c841..7a30ea8 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -498,7 +498,7 @@ defmodule BypassTest do assert {:ok, 200, ""} = request(bypass.port, path) end) - # Redfine the expect + # Redefine the expect apply(Bypass, expect_fun, [ bypass, method,