Permalink
Browse files

additional checks

  • Loading branch information...
1 parent 6ce1501 commit 21e9045b7bf22a225bf3e13f2198f80cba9ec312 @joewilliams joewilliams committed Apr 6, 2012
Showing with 70 additions and 47 deletions.
  1. +1 −1 README.md
  2. +59 −44 src/childspec_validator.erl
  3. +10 −2 test/childspec_validator_tests.erl
View
@@ -1,6 +1,6 @@
### Erlang Childspec Validator
-Validate your childspecs before you use them. This will ensure things start properly and increase the probability hot upgrades will be performed correctly.
+Validate your childspecs before you use them. It attempts to find the most common mistakes in childspecs and let you know about them. This will ensure things start properly and increase the probability hot upgrades will be performed correctly.
### Usage
View
@@ -3,52 +3,67 @@
-export([validate/1]).
validate({Id, {M, F, A}, Restart, Shutdown, Type, Modules}) when is_atom(Id) ->
- MFARes = validate_and_error(check_mfa(M, F, A),
- "Childspec validation error [~p]:"
- " MFA check failed, the arity or"
- " function name is incorrect.~n",
- [Id]),
-
- RestartRes = validate_and_error(check_restart(Restart),
- "Childspec validation error [~p]:"
- " Restart is not permanent, temporary"
- " or transient.~n",
- [Id]),
-
- ShutdownRes = validate_and_error(check_shutdown(Shutdown),
- "Childspec validation error [~p]:"
- " Shutdown is not brutal_kill,"
- " infinity or an integer.~n",
- [Id]),
-
- TypeRes = validate_and_error(check_type(M, Type),
- "Childspec validation error [~p]:"
- " Module type mismatch, if module is"
- " a supervisor it should be the atom"
- " 'supervisor', if not it should be"
- " the atom 'worker'.~n",
- [Id]),
-
- ModsRes = validate_and_error(check_modules(M, Modules),
- "Childspec validation error [~p]:"
- " Modules mismatch, if gen_event it"
- " should be the atom 'dynamic', else"
- " should be a list including the"
- " callback module as its only"
- " element.~n",
- [Id]),
-
- Results = [MFARes, RestartRes, ShutdownRes, TypeRes, ModsRes],
-
- case lists:member(false, Results) of
- true ->
- false;
- _ ->
- true
+ case code:ensure_loaded(M) of
+ {module, _} ->
+ MFARes = validate_and_error(check_mfa(M, F, A),
+ "Childspec validation error [~p]:"
+ " MFA check failed, make sure the"
+ " arity and function name are correct~n",
+ [Id]),
+
+ RestartRes = validate_and_error(check_restart(Restart),
+ "Childspec validation error [~p]:"
+ " Restart is not permanent, temporary"
+ " or transient.~n",
+ [Id]),
+
+ ShutdownRes = validate_and_error(check_shutdown(Shutdown),
+ "Childspec validation error [~p]:"
+ " Shutdown is not brutal_kill,"
+ " infinity or an integer.~n",
+ [Id]),
+
+ TypeRes = validate_and_error(check_type(M, Type),
+ "Childspec validation error [~p]:"
+ " Module type mismatch, if module is"
+ " a supervisor it should be the atom"
+ " 'supervisor', if not it should be"
+ " the atom 'worker'.~n",
+ [Id]),
+
+ ModsRes = validate_and_error(check_modules(M, Modules),
+ "Childspec validation error [~p]:"
+ " Modules mismatch, if gen_event it"
+ " should be the atom 'dynamic', else"
+ " should be a list including the"
+ " callback module as its only"
+ " element.~n",
+ [Id]),
+
+ Results = [MFARes, RestartRes, ShutdownRes, TypeRes, ModsRes],
+
+ case lists:member(false, Results) of
+ true ->
+ false;
+ _ ->
+ true
+ end;
+ {error, _} ->
+ error_logger:error_msg("Childspec validation error [~p]:"
+ " The module specified in childspec"
+ " is not on the code path.~n",
+ [Id]),
+ false
end;
-validate(_) ->
+
+validate({Id, {_, _, _}, _, _, _, _}) when is_atom(Id) == false ->
error_logger:error_msg("Childspec validation error:"
" Childspec ID is not an atom.~n", []),
+ false;
+validate(_) ->
+ error_logger:error_msg("Childspec validation error:"
+ " Childspec does not match correct tuple"
+ " pattern.~n", []),
false.
%%
@@ -76,7 +91,7 @@ check_mfa(M, F, A) ->
case proplists:get_value(F, Exports) of
Arity ->
ok;
- _ ->
+ _ ->
error
end;
_ ->
@@ -56,6 +56,14 @@ negative() ->
false = childspec_validator:validate(Web),
- NotAtomID = {123, {m, f, []}, blah, werker, []},
+ NotAtomID = {123, {m, f, []}, blah, 123, werker, []},
- false = childspec_validator:validate(NotAtomID).
+ false = childspec_validator:validate(NotAtomID),
+
+ NotOnPath = {asdf, {blahblahblah123, f, []}, blah, 123, blah, []},
+
+ false = childspec_validator:validate(NotOnPath),
+
+ BadPattern = {zxc, asdf, asdf, 123, asdf},
+
+ false = childspec_validator:validate(BadPattern).

0 comments on commit 21e9045

Please sign in to comment.