Skip to content

Commit 62a469d

Browse files
author
José Valim
committed
Improve mix new error messages
1 parent 8b0c412 commit 62a469d

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/mix/lib/mix/tasks/new.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,20 @@ defmodule Mix.Tasks.New do
143143

144144
defp check_application_name!(name, from_app_flag) do
145145
unless name =~ ~r/^[a-z][\w_]*$/ do
146-
error = "Application name must start with a letter and have only lowercase letters, numbers and underscore"
147-
if !from_app_flag, do: error = error <> ". The application name is inferred from the path, if you'd like to explicitly name the application then use the `--app APP` option."
148-
Mix.raise error
146+
Mix.raise "Application name must start with a letter and have only lowercase " <>
147+
"letters, numbers and underscore, got: #{inspect name}" <>
148+
(if !from_app_flag do
149+
". The application name is inferred from the path, if you'd like to " <>
150+
"explicitly name the application then use the `--app APP` option."
151+
else
152+
""
153+
end)
149154
end
150155
end
151156

152157
defp check_mod_name!(name) do
153-
unless name =~ ~r/^[A-Z][A-Za-z0-9]*(\.[A-Z][A-Za-z0-9]*)*$/ do
154-
error = "Module name must start with a capital letter, have all periods immediately followed by a capital letter, and must contain only letters, numbers, and periods"
155-
Mix.raise error
158+
unless name =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/ do
159+
Mix.raise "Module name must be a valid Elixir alias (for example: Foo.Bar), got: #{inspect name}"
156160
end
157161
end
158162

lib/mix/test/mix/tasks/new_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,19 @@ defmodule Mix.Tasks.NewTest do
111111

112112
test "new with invalid args" do
113113
in_tmp "new with an invalid application name", fn ->
114-
assert_raise Mix.Error, "Application name must start with a letter and have only lowercase letters, numbers and underscore. The application name is inferred from the path, if you'd like to explicitly name the application then use the `--app APP` option.", fn ->
114+
assert_raise Mix.Error, ~r"Application name must start with a letter and ", fn ->
115115
Mix.Tasks.New.run ["007invalid"]
116116
end
117117
end
118118

119119
in_tmp "new with an invalid application name from the app option", fn ->
120-
assert_raise Mix.Error, "Application name must start with a letter and have only lowercase letters, numbers and underscore", fn ->
120+
assert_raise Mix.Error, ~r"Application name must start with a letter and ", fn ->
121121
Mix.Tasks.New.run ["valid", "--app", "007invalid"]
122122
end
123123
end
124124

125125
in_tmp "new with an invalid module name from the module options", fn ->
126-
assert_raise Mix.Error, "Module name must start with a capital letter, have all periods immediately followed by a capital letter, and must contain only letters, numbers, and periods", fn ->
126+
assert_raise Mix.Error, ~r"Module name must be a valid Elixir alias", fn ->
127127
Mix.Tasks.New.run ["valid", "--module", "not.valid"]
128128
end
129129
end

0 commit comments

Comments
 (0)