Skip to content

Commit

Permalink
Do not return closures from init
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 11, 2023
1 parent b4c7ab3 commit 7b8575d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
9 changes: 3 additions & 6 deletions lib/phoenix/code_reloader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,14 @@ defmodule Phoenix.CodeReloader do
API used by Plug to start the code reloader.
"""
def init(opts) do
case Keyword.pop(opts, :reloader) do
{nil, opts} -> &Phoenix.CodeReloader.reload(&1, opts)
{fun, _opts} -> fun
end
Keyword.put_new(opts, :reloader, &Phoenix.CodeReloader.reload/2)
end

@doc """
API used by Plug to invoke the code reloader on every request.
"""
def call(conn, reloader) do
case reloader.(conn.private.phoenix_endpoint) do
def call(conn, opts) do
case opts[:reloader].(conn.private.phoenix_endpoint, opts) do
:ok ->
conn

Expand Down
24 changes: 15 additions & 9 deletions test/phoenix/code_reloader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Phoenix.CodeReloaderTest do
def config(:reloadable_apps), do: nil
end

def reload(_) do
def reload(_, _) do
{:error, "oops"}
end

Expand Down Expand Up @@ -38,20 +38,26 @@ defmodule Phoenix.CodeReloaderTest do
:erlang.trace(pid, true, [:receive])

opts = Phoenix.CodeReloader.init([])
conn = conn(:get, "/")
|> Plug.Conn.put_private(:phoenix_endpoint, Endpoint)
|> Phoenix.CodeReloader.call(opts)

conn =
conn(:get, "/")
|> Plug.Conn.put_private(:phoenix_endpoint, Endpoint)
|> Phoenix.CodeReloader.call(opts)

assert conn.state == :unset

assert_receive {:trace, ^pid, :receive, {_, _, {:reload!, Endpoint, _}}}
end

test "renders compilation error on failure" do
opts = Phoenix.CodeReloader.init(reloader: &__MODULE__.reload/1)
conn = conn(:get, "/")
|> Plug.Conn.put_private(:phoenix_endpoint, Endpoint)
|> Phoenix.CodeReloader.call(opts)
assert conn.state == :sent
opts = Phoenix.CodeReloader.init(reloader: &__MODULE__.reload/2)

conn =
conn(:get, "/")
|> Plug.Conn.put_private(:phoenix_endpoint, Endpoint)
|> Phoenix.CodeReloader.call(opts)

assert conn.state == :sent
assert conn.status == 500
assert conn.resp_body =~ "oops"
assert conn.resp_body =~ "CompileError"
Expand Down

0 comments on commit 7b8575d

Please sign in to comment.