From 7a410e4d272dfce5de61465d4246904ab0ec32ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Almir=20Saraj=C4=8Di=C4=87?= Date: Wed, 11 Dec 2024 09:42:43 +0100 Subject: [PATCH 1/2] Raise an error when using a reserved app name --- installer/lib/mix/tasks/phx.new.ex | 26 +++++++++++++++++++++++++- installer/test/phx_new_test.exs | 10 ++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/installer/lib/mix/tasks/phx.new.ex b/installer/lib/mix/tasks/phx.new.ex index dc4807b652..7119a54877 100644 --- a/installer/lib/mix/tasks/phx.new.ex +++ b/installer/lib/mix/tasks/phx.new.ex @@ -113,6 +113,13 @@ defmodule Mix.Tasks.Phx.New do You can read more about umbrella projects using the official [Elixir guide](https://hexdocs.pm/elixir/dependencies-and-umbrella-projects.html#umbrella-projects) + + ## Reserved Names + + The application name cannot be any of the following reserved names: + + * server + * table """ use Mix.Task alias Phx.New.{Generator, Project, Single, Umbrella, Web, Ecto} @@ -143,6 +150,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 +338,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 '#{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..f38d5b5fd5 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 From c4b51e2b1bc0a0946ef55f685b6b2db5d3526623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Almir=20Saraj=C4=8Di=C4=87?= Date: Mon, 23 Dec 2024 10:57:17 +0100 Subject: [PATCH 2/2] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- installer/lib/mix/tasks/phx.new.ex | 9 +-------- installer/test/phx_new_test.exs | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/installer/lib/mix/tasks/phx.new.ex b/installer/lib/mix/tasks/phx.new.ex index 7119a54877..24773ce64d 100644 --- a/installer/lib/mix/tasks/phx.new.ex +++ b/installer/lib/mix/tasks/phx.new.ex @@ -113,13 +113,6 @@ defmodule Mix.Tasks.Phx.New do You can read more about umbrella projects using the official [Elixir guide](https://hexdocs.pm/elixir/dependencies-and-umbrella-projects.html#umbrella-projects) - - ## Reserved Names - - The application name cannot be any of the following reserved names: - - * server - * table """ use Mix.Task alias Phx.New.{Generator, Project, Single, Umbrella, Web, Ecto} @@ -345,7 +338,7 @@ defmodule Mix.Tasks.Phx.New do end defp validate_not_reserved(name) when name in @reserved_app_names do - Mix.raise("Application name cannot be '#{name}' as it is reserved") + Mix.raise("Application name cannot be #{inspect(name)} as it is reserved") end defp validate_not_reserved(_name), do: :ok diff --git a/installer/test/phx_new_test.exs b/installer/test/phx_new_test.exs index f38d5b5fd5..358a396915 100644 --- a/installer/test/phx_new_test.exs +++ b/installer/test/phx_new_test.exs @@ -810,11 +810,11 @@ defmodule Mix.Tasks.Phx.NewTest do end test "new with reserved name" do - assert_raise Mix.Error, ~r/Application name cannot be 'server' as it is reserved/, fn -> + 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 -> + assert_raise Mix.Error, ~r/Application name cannot be "table" as it is reserved/, fn -> Mix.Tasks.Phx.New.run(["table"]) end end