Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an error when using a reserved app name #6011

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
19 changes: 18 additions & 1 deletion installer/lib/mix/tasks/phx.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ defmodule Mix.Tasks.Phx.New do
adapter: :string
]

@reserved_app_names ~w(server table)

@impl true
def run([version]) when version in ~w(-v --version) do
Mix.shell().info("Phoenix installer v#{@version}")
Expand Down Expand Up @@ -329,7 +331,22 @@ defmodule Mix.Tasks.Phx.New do
end

defp check_app_name!(name, from_app_flag) do
unless name =~ Regex.recompile!(~r/^[a-z][a-z0-9_]*$/) do
with :ok <- validate_not_reserved(name),
:ok <- validate_app_name_format(name, from_app_flag) do
:ok
end
end

defp validate_not_reserved(name) when name in @reserved_app_names do
Mix.raise("Application name cannot be #{inspect(name)} as it is reserved")
end

defp validate_not_reserved(_name), do: :ok

defp validate_app_name_format(name, from_app_flag) do
if name =~ ~r/^[a-z][a-z0-9_]*$/ do
:ok
else
extra =
if !from_app_flag do
". The application name is inferred from the path, if you'd like to " <>
Expand Down
10 changes: 10 additions & 0 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,14 @@ defmodule Mix.Tasks.Phx.NewTest do
"Creates a new Phoenix project."
end)
end

test "new with reserved name" do
assert_raise Mix.Error, ~r/Application name cannot be "server" as it is reserved/, fn ->
Mix.Tasks.Phx.New.run(["server"])
end

assert_raise Mix.Error, ~r/Application name cannot be "table" as it is reserved/, fn ->
Mix.Tasks.Phx.New.run(["table"])
end
end
end
Loading