Selaa lähdekoodia

A bad template index does not crash; shows warning

This should fix #955

The test is implicit as a bad index previously silently crashed rebar3.
By adding the bad index to the `new` suite's files, we can show that
things keep running.
pull/986/head
Fred Hebert 9 vuotta sitten
vanhempi
commit
d201d7f4a6
10 muutettua tiedostoa jossa 173 lisäystä ja 10 poistoa
  1. +16
    -4
      src/rebar_prv_new.erl
  2. +12
    -6
      src/rebar_templater.erl
  3. +29
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/LICENSE.dtl
  4. +9
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/README.md.dtl
  5. +27
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/app.erl.dtl
  6. +13
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/bad_index.template
  7. +18
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/gitignore.dtl
  8. +12
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/otp_app.app.src.dtl
  9. +2
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/rebar.config.dtl
  10. +35
    -0
      test/rebar_new_SUITE_data/.rebar3/templates/bad_index/sup.erl.dtl

+ 16
- 4
src/rebar_prv_new.erl Näytä tiedosto

@ -7,6 +7,7 @@
format_error/1]).
-include("rebar.hrl").
-include_lib("providers/include/providers.hrl").
-define(PROVIDER, new).
-define(DEPS, []).
@ -35,16 +36,16 @@ do(State) ->
case strip_flags(rebar_state:command_args(State)) of
["help"] ->
?CONSOLE("Call `rebar3 new help <template>` for a detailed description~n", []),
show_short_templates(rebar_templater:list_templates(State)),
show_short_templates(list_templates(State)),
{ok, State};
["help", TemplateName] ->
case lists:keyfind(TemplateName, 1, rebar_templater:list_templates(State)) of
case lists:keyfind(TemplateName, 1, list_templates(State)) of
false -> ?CONSOLE("template not found.", []);
Term -> show_template(Term)
end,
{ok, State};
[TemplateName | Opts] ->
case lists:keyfind(TemplateName, 1, rebar_templater:list_templates(State)) of
case lists:keyfind(TemplateName, 1, list_templates(State)) of
false ->
?CONSOLE("template not found.", []);
_ ->
@ -53,11 +54,13 @@ do(State) ->
end,
{ok, State};
[] ->
show_short_templates(rebar_templater:list_templates(State)),
show_short_templates(list_templates(State)),
{ok, State}
end.
-spec format_error(any()) -> iolist().
format_error({consult, File, Reason}) ->
io_lib:format("Error consulting file at ~s for reason ~p", [File, Reason]);
format_error(Reason) ->
io_lib:format("~p", [Reason]).
@ -65,6 +68,15 @@ format_error(Reason) ->
%% Internal functions
%% ===================================================================
list_templates(State) ->
lists:foldl(fun({error, {consult, File, Reason}}, Acc) ->
?WARN("Error consulting template file ~s for reason ~p",
[File, Reason]),
Acc
; (Tpl, Acc) ->
[Tpl|Acc]
end, [], lists:reverse(rebar_templater:list_templates(State))).
info() ->
io_lib:format(
"Create rebar3 project based on template and vars.~n"

+ 12
- 6
src/rebar_templater.erl Näytä tiedosto

@ -59,10 +59,14 @@ list_templates(State) ->
%% Expand a single template's value
list_template(Files, {Name, Type, File}, State) ->
TemplateTerms = consult(load_file(Files, Type, File)),
{Name, Type, File,
get_template_description(TemplateTerms),
get_template_vars(TemplateTerms, State)}.
case consult(load_file(Files, Type, File)) of
{error, Reason} ->
{error, {consult, File, Reason}};
TemplateTerms ->
{Name, Type, File,
get_template_description(TemplateTerms),
get_template_vars(TemplateTerms, State)}
end.
%% Load up the template description out from a list of attributes read in
%% a .template file.
@ -338,8 +342,10 @@ consult(Cont, Str, Acc) ->
{done, Result, Remaining} ->
case Result of
{ok, Tokens, _} ->
{ok, Term} = erl_parse:parse_term(Tokens),
consult([], Remaining, [Term | Acc]);
case erl_parse:parse_term(Tokens) of
{ok, Term} -> consult([], Remaining, [Term | Acc]);
{error, Reason} -> {error, Reason}
end;
{eof, _Other} ->
lists:reverse(Acc);
{error, Info, _} ->

+ 29
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/LICENSE.dtl Näytä tiedosto

@ -0,0 +1,29 @@
Copyright (c) {{copyright_year}}, {{author_name}} <{{author_email}}>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 9
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/README.md.dtl Näytä tiedosto

@ -0,0 +1,9 @@
{{name}}
=====
{{desc}}
Build
-----
$ rebar3 compile

+ 27
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/app.erl.dtl Näytä tiedosto

@ -0,0 +1,27 @@
%%%-------------------------------------------------------------------
%% @doc {{name}} public API
%% @end
%%%-------------------------------------------------------------------
-module({{name}}_app).
-behaviour(application).
%% Application callbacks
-export([start/2
,stop/1]).
%%====================================================================
%% API
%%====================================================================
start(_StartType, _StartArgs) ->
{{name}}_sup:start_link().
%%--------------------------------------------------------------------
stop(_State) ->
ok.
%%====================================================================
%% Internal functions
%%====================================================================

+ 13
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/bad_index.template Näytä tiedosto

@ -0,0 +1,13 @@
{description, "OTP Application"}.
{variables, [
{name, "mylib", "Name of the OTP application"},
{desc, "An OTP application", "Short description of the app"}
]}.
bad_term,
{template, "app.erl.dtl", "{{name}}/src/{{name}}_app.erl"}.
{template, "sup.erl.dtl", "{{name}}/src/{{name}}_sup.erl"}.
{template, "otp_app.app.src.dtl", "{{name}}/src/{{name}}.app.src"}.
{template, "rebar.config.dtl", "{{name}}/rebar.config"}.
{template, "gitignore.dtl", "{{name}}/.gitignore"}.
{template, "LICENSE.dtl", "{{name}}/LICENSE"}.
{template, "README.md.dtl", "{{name}}/README.md"}.

+ 18
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/gitignore.dtl Näytä tiedosto

@ -0,0 +1,18 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
_rel
_deps
_plugins
_tdeps
logs

+ 12
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/otp_app.app.src.dtl Näytä tiedosto

@ -0,0 +1,12 @@
{application, {{name}},
[{description, "{{desc}}"}
,{vsn, "0.1.0"}
,{registered, []}
,{mod, {'{{name}}_app', []}}
,{applications,
[kernel
,stdlib
]}
,{env,[]}
,{modules, []}
]}.

+ 2
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/rebar.config.dtl Näytä tiedosto

@ -0,0 +1,2 @@
{erl_opts, [debug_info]}.
{deps, []}.

+ 35
- 0
test/rebar_new_SUITE_data/.rebar3/templates/bad_index/sup.erl.dtl Näytä tiedosto

@ -0,0 +1,35 @@
%%%-------------------------------------------------------------------
%% @doc {{name}} top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module({{name}}_sup).
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
-define(SERVER, ?MODULE).
%%====================================================================
%% API functions
%%====================================================================
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
%%====================================================================
%% Supervisor callbacks
%%====================================================================
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init([]) ->
{ok, { {one_for_all, 0, 1}, []} }.
%%====================================================================
%% Internal functions
%%====================================================================

Ladataan…
Peruuta
Tallenna