Skip to content
Closed
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
11 changes: 11 additions & 0 deletions test/phoenix/verified_routes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,17 @@
:code.delete(__MODULE__.SigilPPrefix)
end

test "~p raises when VerifiedRoutes is imported instead of used" do

Check failure on line 305 in test/phoenix/verified_routes_test.exs

View workflow job for this annotation

GitHub Actions / mix test (OTP 28.3.3 | Elixir 1.19.5)

test ~p raises when VerifiedRoutes is imported instead of used (Phoenix.VerifiedRoutesTest)

Check failure on line 305 in test/phoenix/verified_routes_test.exs

View workflow job for this annotation

GitHub Actions / mix test (OTP 25.3.2.9 | Elixir 1.15.8)

test ~p raises when VerifiedRoutes is imported instead of used (Phoenix.VerifiedRoutesTest)

Check failure on line 305 in test/phoenix/verified_routes_test.exs

View workflow job for this annotation

GitHub Actions / mix test (OTP 27.3 | Elixir 1.18.4)

test ~p raises when VerifiedRoutes is imported instead of used (Phoenix.VerifiedRoutesTest)
assert_raise ArgumentError,
~r|attempted to use Phoenix.VerifiedRoutes without calling|,
fn ->
defmodule BadImportedVerifiedRoutes do
import Phoenix.VerifiedRoutes
def test, do: ~p"/posts/1"
end
end
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
Loading