Browse files

Validate all app properties and do not accept nil values

  • Loading branch information...
1 parent cf5f57f commit 4e8fb9915b1f1e8d86b6d4e517736a4c417c78d6 @josevalim josevalim committed Dec 2, 2012
Showing with 47 additions and 41 deletions.
  1. +45 −39 lib/mix/lib/mix/tasks/compile.app.ex
  2. +2 −2 lib/mix/test/mix/tasks/compile.app_test.exs
View
84 lib/mix/lib/mix/tasks/compile.app.ex
@@ -19,7 +19,7 @@ defmodule Mix.Tasks.Compile.App do
http://www.erlang.org/doc/design_principles/applications.html
## Configuration
-
+
* `:app` - The application name as a binary (required)
* `:version` - The application version as a binary (required)
@@ -86,51 +86,57 @@ defmodule Mix.Tasks.Compile.App do
defp ensure_correct_properties(app, properties) do
properties = Keyword.from_enum(properties)
- properties = Keyword.put properties, :description,
+ properties = Keyword.put properties, :description,
to_char_list(properties[:description] || app)
properties = Keyword.put properties, :registered, (properties[:registered] || [])
validate_properties(properties)
properties
end
defp validate_properties(properties) do
- unless nil?(properties[:description]) or is_list(properties[:description]) do
- raise(Mix.Error, message: "Application description (:description) is not a character list (got #{inspect properties[:description]})")
- end
- unless nil?(properties[:id]) or is_list(properties[:id]) do
- raise(Mix.Error, message: "Application id (:id) is not a character list (got #{inspect properties[:id]} instead)")
- end
- unless nil?(properties[:vsn]) or is_list(properties[:vsn]) do
- raise(Mix.Error, message: "Application vsn (:vsn) is not a character list (got #{inspect properties[:vsn]} instead)")
- end
- unless nil?(properties[:modules]) or (is_list(properties[:modules]) and Enum.all?(properties[:modules], is_atom(&1))) do
- raise(Mix.Error, message: "Application modules (:modules) should be a list of atoms (got #{inspect properties[:modules]} instead)")
- end
- unless nil?(properties[:maxT]) or properties[:maxT] == :infinity or is_integer(properties[:maxT]) do
- raise(Mix.Error, message: "Application maximum time (:maxT) is not an integer or :infinity (got #{inspect properties[:maxT]} instead)")
- end
- unless nil?(properties[:registered]) or is_list(properties[:registered]) and (Enum.all?(properties[:registered], is_atom(&1))) do
- raise(Mix.Error, message: "Application registered processes (:registered) should be a list of atoms (got #{inspect properties[:registered]} instead)")
- end
- unless nil?(properties[:included_applications]) or (is_list(properties[:included_applications]) and Enum.all?(properties[:included_applications], is_atom(&1))) do
- raise(Mix.Error, message: "Application included applications (:included_applications) should be a list of atoms (got #{inspect properties[:included_applications]} instead)")
- end
- unless nil?(properties[:applications]) or (is_list(properties[:applications]) and Enum.all?(properties[:applications], is_atom(&1))) do
- raise(Mix.Error, message: "Application dependencies (:applications) should be a list of atoms (got #{inspect properties[:applications]} instead)")
- end
- unless nil?(properties[:env]) or (Keyword.keyword?(properties[:env])) do
- raise(Mix.Error, message: "Application dependencies (:env) should be a keyword list (got #{inspect properties[:env]} instead)")
- end
- unless nil?(properties[:mod]) do
- case properties[:mod] do
- [] -> :ok
- {module, _start_args} when is_atom(module) -> :ok
- other ->
- raise(Mix.Error, message: "Application callback module (:mod) should be either [] or {module, start_args} (got #{inspect properties[:mod]} instead)")
- end
- end
- unless nil?(properties[:start_phases]) or (Keyword.keyword?(properties[:start_phases])) do
- raise(Mix.Error, message: "Application start phases (:start_phases) should be a keyword list (got #{inspect properties[:start_phases]} instead)")
+ Enum.each properties, fn
+ { :description, value } ->
+ unless is_list(value), do:
+ invalid "Application description (:description) is not a character list (got #{inspect value})"
+ { :id, value } ->
+ unless is_list(value), do:
+ invalid "Application id (:id) is not a character list (got #{inspect value} instead)"
+ { :vsn, value } ->
+ unless is_list(value), do:
+ invalid "Application vsn (:vsn) is not a character list (got #{inspect value} instead)"
+ { :maxT, value } ->
+ unless value == :infinity or is_integer(value), do:
+ invalid "Application maximum time (:maxT) is not an integer or :infinity (got #{inspect value} instead)"
+ { :modules, value } ->
+ unless is_list(value) and Enum.all?(value, is_atom(&1)), do:
+ invalid "Application modules (:modules) should be a list of atoms (got #{inspect value} instead)"
+ { :registered, value } ->
+ unless is_list(value) and Enum.all?(value, is_atom(&1)), do:
+ invalid "Application registered processes (:registered) should be a list of atoms (got #{inspect value} instead)"
+ { :included_applications, value } ->
+ unless is_list(value) and Enum.all?(value, is_atom(&1)), do:
+ invalid "Application included applications (:included_applications) should be a list of atoms (got #{inspect value} instead)"
+ { :applications, value } ->
+ unless is_list(value) and Enum.all?(value, is_atom(&1)), do:
+ invalid "Application dependencies (:applications) should be a list of atoms (got #{inspect value} instead)"
+ { :env, value } ->
+ unless Keyword.keyword?(value), do:
+ invalid "Application dependencies (:env) should be a keyword list (got #{inspect value} instead)"
+ { :start_phases, value } ->
+ unless Keyword.keyword?(value), do:
+ invalid "Application start phases (:start_phases) should be a keyword list (got #{inspect value} instead)"
+ { :mod, [] } ->
+ :ok
+ { :mod, { module, _args } } when is_atom(module) ->
+ :ok
+ { :mod, value } ->
+ invalid "Application callback module (:mod) should be either [] or {module, start_args} (got #{inspect value} instead)"
+ { key, value } ->
@yrashk
yrashk added a note Dec 2, 2012

Lets drop this. Application files are permitted to have optional keys. Agner, for example, used this property.

@josevalim
Elixir member

Ah, so go ahead. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ invalid "Unknown application key #{inspect key} with value #{inspect value}"
end
end
+
+ defp invalid(message) do
+ raise Mix.Error, message: message
+ end
end
View
4 lib/mix/test/mix/tasks/compile.app_test.exs
@@ -15,7 +15,7 @@ defmodule Mix.Tasks.Compile.AppTest do
end
def application do
- [hello: 'beautiful']
+ [maxT: :infinity]
end
end
@@ -66,7 +66,7 @@ defmodule Mix.Tasks.Compile.AppTest do
Mix.Tasks.Compile.App.run([])
contents = File.read!("ebin/custom_project.app")
assert contents =~ %r/0.2.0/
- assert contents =~ %r/{hello,"beautiful"}/
+ assert contents =~ %r/{maxT,infinity}/
end
after
purge [A, B, C]

0 comments on commit 4e8fb99

Please sign in to comment.