From bc774336a5f9c0cbcb46e6d535f7a77ad11f0794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Almir=20Saraj=C4=8Di=C4=87?= Date: Sat, 11 Jan 2025 16:24:18 +0100 Subject: [PATCH] Raise an error when using a reserved app name (#6011) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Raise an error when using a reserved app name * Apply suggestions from code review Co-authored-by: José Valim --------- Co-authored-by: José Valim --- installer/lib/mix/tasks/phx.new.ex | 19 ++++++++++++++++++- installer/test/phx_new_test.exs | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/installer/lib/mix/tasks/phx.new.ex b/installer/lib/mix/tasks/phx.new.ex index dc4807b652..24773ce64d 100644 --- a/installer/lib/mix/tasks/phx.new.ex +++ b/installer/lib/mix/tasks/phx.new.ex @@ -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}") @@ -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 " <> diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs index d33502a688..358a396915 100644 --- a/installer/test/phx_new_test.exs +++ b/installer/test/phx_new_test.exs @@ -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