Skip to content

Commit 87bd9f0

Browse files
authored
Merge pull request #100 from PSPDFKit-labs/credo
Improve code based on credo feedback
2 parents c88c867 + 8d096da commit 87bd9f0

File tree

5 files changed

+36
-55
lines changed

5 files changed

+36
-55
lines changed

lib/bypass.ex

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
defmodule Bypass do
22
@moduledoc """
3-
Bypass provides a quick way to create a custom Plug that can be put
4-
in place instead of an actual HTTP server to return prebaked responses
5-
to client requests.
3+
Bypass provides a quick way to create a custom Plug that can be put in place
4+
instead of an actual HTTP server to return prebaked responses to client
5+
requests.
66
77
This module is the main interface to the library.
88
"""
99

1010
defstruct pid: nil, port: nil
1111

1212
@typedoc """
13-
Represents a Bypass server process
13+
Represents a Bypass server process.
1414
"""
1515
@type t :: %__MODULE__{pid: pid, port: non_neg_integer}
1616

1717
import Bypass.Utils
1818
require Logger
1919

2020
@doc """
21-
Starts an Elixir process running a minimal Plug app. The process
22-
is a HTTP handler and listens to requests on a TCP port on localhost.
21+
Starts an Elixir process running a minimal Plug app. The process is a HTTP
22+
handler and listens to requests on a TCP port on localhost.
2323
24-
Use the other functions in this module to declare which requests are
25-
handled and set expectations on the calls.
24+
Use the other functions in this module to declare which requests are handled
25+
and set expectations on the calls.
2626
"""
2727
def open(opts \\ []) do
2828
case DynamicSupervisor.start_child(Bypass.Supervisor, Bypass.Instance.child_spec(opts)) do
@@ -38,21 +38,18 @@ defmodule Bypass do
3838
end
3939
end
4040

41-
# Raise an error if called with an unknown framework
42-
#
4341
defp setup_framework_integration(:ex_unit, bypass = %{pid: pid}) do
4442
ExUnit.Callbacks.on_exit({Bypass, pid}, fn ->
4543
do_verify_expectations(bypass.pid, ExUnit.AssertionError)
4644
end)
4745
end
4846

4947
defp setup_framework_integration(:espec, _bypass) do
50-
# Entry point for more advanced ESpec configurations
5148
end
5249

5350
@doc """
54-
Can be called to immediately verify if the declared request
55-
expectations have been met.
51+
Can be called to immediately verify if the declared request expectations have
52+
been met.
5653
5754
Returns `:ok` on success and raises an error on failure.
5855
"""
@@ -104,16 +101,15 @@ defmodule Bypass do
104101
end
105102

106103
@doc """
107-
Re-opens the TCP socket on the same port.
108-
Blocks until the operation is complete.
104+
Re-opens the TCP socket on the same port. Blocks until the operation is
105+
complete.
109106
"""
110107
@spec up(Bypass.t()) :: :ok | {:error, :already_up}
111108
def up(%Bypass{pid: pid}),
112109
do: Bypass.Instance.call(pid, :up)
113110

114111
@doc """
115-
Closes the TCP socket.
116-
Blocks until the operation is complete.
112+
Closes the TCP socket. Blocks until the operation is complete.
117113
"""
118114
@spec down(Bypass.t()) :: :ok | {:error, :already_down}
119115
def down(%Bypass{pid: pid}),

lib/bypass/application.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Application do
2+
@moduledoc false
3+
24
use Application
35

46
def start(_type, _args) do

lib/bypass/instance.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Instance do
2+
@moduledoc false
3+
24
use GenServer, restart: :transient
35

46
import Bypass.Utils
@@ -264,7 +266,7 @@ defmodule Bypass.Instance do
264266
problem_route =
265267
expectations
266268
|> Enum.reject(fn {_route, expectations} -> expectations[:expected] == :none_or_more end)
267-
|> Enum.find(fn {_route, expectations} -> length(expectations.results) == 0 end)
269+
|> Enum.find(fn {_route, expectations} -> Enum.empty?(expectations.results) end)
268270

269271
case problem_route do
270272
{route, _} ->
@@ -434,7 +436,7 @@ defmodule Bypass.Instance do
434436
defp so_reuseport() do
435437
case :os.type() do
436438
{:unix, :linux} -> [{:raw, 1, 15, <<1::32-native>>}]
437-
{:unix, :darwin} -> [{:raw, 65535, 512, <<1::32-native>>}]
439+
{:unix, :darwin} -> [{:raw, 65_535, 512, <<1::32-native>>}]
438440
_ -> []
439441
end
440442
end

lib/bypass/plug.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule Bypass.Plug do
2+
@moduledoc false
3+
24
def init([pid]), do: pid
35

46
def call(%{method: method, request_path: request_path} = conn, pid) do

test/bypass_test.exs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,7 @@ defmodule BypassTest do
22
use ExUnit.Case
33
doctest Bypass
44

5-
if Code.ensure_loaded?(ExUnit.CaptureLog) do
6-
defdelegate capture_log(fun), to: ExUnit.CaptureLog
7-
else
8-
# Shim capture_log for Elixir 1.0
9-
defp capture_log(fun) do
10-
ExUnit.CaptureIO.capture_io(:user, fn ->
11-
fun.()
12-
Logger.flush()
13-
end)
14-
|> String.strip()
15-
end
16-
end
5+
defdelegate capture_log(fun), to: ExUnit.CaptureLog
176

187
test "show ISSUE #51" do
198
Enum.each(
@@ -37,7 +26,6 @@ defmodule BypassTest do
3726
defp specify_port(port, expect_fun) do
3827
bypass = Bypass.open(port: port)
3928

40-
# one of Bypass.expect or Bypass.expect_once
4129
apply(Bypass, expect_fun, [
4230
bypass,
4331
fn conn ->
@@ -62,7 +50,6 @@ defmodule BypassTest do
6250
defp down_socket(expect_fun) do
6351
bypass = Bypass.open()
6452

65-
# one of Bypass.expect or Bypass.expect_once
6653
apply(Bypass, expect_fun, [
6754
bypass,
6855
fn conn -> Plug.Conn.send_resp(conn, 200, "") end
@@ -101,13 +88,12 @@ defmodule BypassTest do
10188
defp not_called(expect_fun) do
10289
bypass = Bypass.open()
10390

104-
# one of Bypass.expect or Bypass.expect_once
10591
apply(Bypass, expect_fun, [
10692
bypass,
10793
fn _conn -> assert false end
10894
])
10995

110-
# Override Bypass' on_exit handler
96+
# Override Bypass' on_exit handler.
11197
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
11298
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
11399
assert {:error, :not_called, {:any, :any}} = exit_result
@@ -125,7 +111,6 @@ defmodule BypassTest do
125111
defp pass(expect_fun) do
126112
bypass = Bypass.open()
127113

128-
# one of Bypass.expect or Bypass.expect_once
129114
apply(Bypass, expect_fun, [
130115
bypass,
131116
fn _conn ->
@@ -152,11 +137,10 @@ defmodule BypassTest do
152137
defp closing_in_flight(expect_fun) do
153138
bypass = Bypass.open()
154139

155-
# one of Bypass.expect or Bypass.expect_once
156140
apply(Bypass, expect_fun, [
157141
bypass,
158142
fn _conn ->
159-
# mark the request as arrived, since we're shutting it down now
143+
# Mark the request as arrived, since we're shutting it down now.
160144
Bypass.pass(bypass)
161145
Bypass.down(bypass)
162146
end
@@ -179,7 +163,6 @@ defmodule BypassTest do
179163
ref = make_ref()
180164
bypass = Bypass.open()
181165

182-
# one of Bypass.expect or Bypass.expect_once
183166
apply(Bypass, expect_fun, [
184167
bypass,
185168
fn conn ->
@@ -192,8 +175,8 @@ defmodule BypassTest do
192175

193176
assert {:ok, 200, ""} = request(bypass.port)
194177

195-
# Here we make sure that Bypass.down waits until the plug process finishes its work
196-
# before shutting down
178+
# Here we make sure that Bypass.down waits until the plug process finishes
179+
# its work before shutting down.
197180
refute_received ^ref
198181
Bypass.down(bypass)
199182
assert_received ^ref
@@ -236,8 +219,8 @@ defmodule BypassTest do
236219
end)
237220
end)
238221

239-
# Here we make sure that Bypass.down waits until the plug process finishes its work
240-
# before shutting down
222+
# Here we make sure that Bypass.down waits until the plug process finishes
223+
# its work before shutting down.
241224
refute_received ^ref
242225
:timer.sleep(200)
243226
Bypass.down(bypass)
@@ -256,7 +239,7 @@ defmodule BypassTest do
256239
assert {:ok, 500, ""} = request(bypass.port)
257240
end)
258241

259-
# Override Bypass' on_exit handler
242+
# Override Bypass' on_exit handler.
260243
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
261244
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
262245
assert {:error, :unexpected_request, {:any, :any}} = exit_result
@@ -282,7 +265,7 @@ defmodule BypassTest do
282265
assert_receive :request_received
283266
end)
284267

285-
# Override Bypass' on_exit handler
268+
# Override Bypass' on_exit handler.
286269
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
287270
:ok == Bypass.Instance.call(bypass.pid, :on_exit)
288271
end)
@@ -303,7 +286,7 @@ defmodule BypassTest do
303286
assert_receive :request_received
304287
refute_receive :request_received
305288

306-
# Override Bypass' on_exit handler
289+
# Override Bypass' on_exit handler.
307290
ExUnit.Callbacks.on_exit({Bypass, bypass.pid}, fn ->
308291
exit_result = Bypass.Instance.call(bypass.pid, :on_exit)
309292
assert {:error, :too_many_requests, {:any, :any}} = exit_result
@@ -347,7 +330,6 @@ defmodule BypassTest do
347330
method = "POST"
348331
path = "/this"
349332

350-
# one of Bypass.expect or Bypass.expect_once
351333
apply(Bypass, expect_fun, [
352334
bypass,
353335
method,
@@ -382,7 +364,6 @@ defmodule BypassTest do
382364
pattern = "/this/:resource/get/:id"
383365
path = "/this/my_resource/get/1234"
384366

385-
# one of Bypass.expect or Bypass.expect_once
386367
apply(Bypass, expect_fun, [
387368
bypass,
388369
method,
@@ -421,7 +402,6 @@ defmodule BypassTest do
421402
paths = ["/this", "/that"]
422403

423404
Enum.each(paths, fn path ->
424-
# one of Bypass.expect or Bypass.expect_once
425405
apply(Bypass, expect_fun, [
426406
bypass,
427407
method,
@@ -448,11 +428,11 @@ defmodule BypassTest do
448428
@doc ~S"""
449429
Open a new HTTP connection and perform the request. We don't want to use httpc, hackney or another
450430
"high-level" HTTP client, since they do connection pooling and we will sometimes get a connection
451-
closed error and not a failed to connect error, when we test Bypass.down
431+
closed error and not a failed to connect error, when we test Bypass.down.
452432
"""
453433
def request(port, path \\ "/example_path", method \\ "POST") do
454434
with {:ok, conn} <- Mint.HTTP.connect(:http, "127.0.0.1", port),
455-
{:ok, conn, ref} = Mint.HTTP.request(conn, method, path, [], "") do
435+
{:ok, conn, ref} <- Mint.HTTP.request(conn, method, path, [], "") do
456436
receive_responses(conn, ref, 100, [])
457437
end
458438
end
@@ -490,11 +470,11 @@ defmodule BypassTest do
490470
end
491471
end
492472

493-
test "Bypass.expect/4 can be used to define a specific route and then redefined" do
473+
test "Bypass.expect/4 can be used to define a specific route and then redefine it later" do
494474
:expect |> specific_route_redefined
495475
end
496476

497-
test "Bypass.expect_once/4 can be used to define a specific route and then redefined" do
477+
test "Bypass.expect_once/4 can be used to define a specific route and then redefine it later" do
498478
:expect_once |> specific_route_redefined
499479
end
500480

@@ -503,7 +483,6 @@ defmodule BypassTest do
503483
method = "POST"
504484
path = "/this"
505485

506-
# one of Bypass.expect or Bypass.expect_once
507486
apply(Bypass, expect_fun, [
508487
bypass,
509488
method,
@@ -519,7 +498,7 @@ defmodule BypassTest do
519498
assert {:ok, 200, ""} = request(bypass.port, path)
520499
end)
521500

522-
# redefining the expect
501+
# Redefine the expect
523502
apply(Bypass, expect_fun, [
524503
bypass,
525504
method,

0 commit comments

Comments
 (0)