Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/phoenix/verified_routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,19 @@ defmodule Phoenix.VerifiedRoutes do
defp to_param(data), do: Phoenix.Param.to_param(data)

defp build_route(route_ast, sigil_p, env, endpoint_ctx, router) do
config = Module.get_attribute(env.module, :phoenix_verified_config, [])
config = Module.get_attribute(env.module, :phoenix_verified_config)

if is_nil(config) do
raise ArgumentError, """
attempted to use Phoenix.VerifiedRoutes without calling `use Phoenix.VerifiedRoutes` first.

You must use `use Phoenix.VerifiedRoutes` with the appropriate options instead of importing it:

use Phoenix.VerifiedRoutes, endpoint: MyAppWeb.Endpoint, router: MyAppWeb.Router

See the documentation for more details on configuration options.
"""
end

router =
case Macro.expand(router, env) do
Expand Down
14 changes: 14 additions & 0 deletions test/phoenix/verified_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ defmodule Phoenix.VerifiedRoutesTest do
:code.delete(__MODULE__.SigilPPrefix)
end

test "~p raises when VerifiedRoutes is imported instead of used" do
assert_raise ArgumentError,
~r|attempted to use Phoenix.VerifiedRoutes without calling|,
fn ->
defmodule ImportedVerifiedRoutes do
import Phoenix.VerifiedRoutes
def test, do: ~p"/posts/1"
end
end
after
:code.purge(__MODULE__.ImportedVerifiedRoutes)
:code.delete(__MODULE__.ImportedVerifiedRoutes)
end

test "path arities" do
assert path(Endpoint, ~p"/posts/1") == "/posts/1"
assert path(conn_with_endpoint(), ~p"/posts/1") == "/posts/1"
Expand Down