diff --git a/lib/phoenix/code_reloader.ex b/lib/phoenix/code_reloader.ex index d1c48fb541..809d55525b 100644 --- a/lib/phoenix/code_reloader.ex +++ b/lib/phoenix/code_reloader.ex @@ -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 diff --git a/test/phoenix/code_reloader_test.exs b/test/phoenix/code_reloader_test.exs index 70b5d0cfe7..36f2911e23 100644 --- a/test/phoenix/code_reloader_test.exs +++ b/test/phoenix/code_reloader_test.exs @@ -7,7 +7,7 @@ defmodule Phoenix.CodeReloaderTest do def config(:reloadable_apps), do: nil end - def reload(_) do + def reload(_, _) do {:error, "oops"} end @@ -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"