Skip to content

Commit

Permalink
Fix our spec.s as per elvis_core and output more context on error
Browse files Browse the repository at this point in the history
The test is a dumb one, not unlike the prior version of it,
but it makes sure we don't crash on function call (mimics
rebar3) and on io:format'ing it
  • Loading branch information
paulo-ferraz-oliveira committed Jan 17, 2024
1 parent e3e28b8 commit 5e8b723
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/rebar3_lint_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,25 @@ init(State) ->
{desc, "A rebar linter plugin based on elvis"}]),
{ok, rebar_state:add_provider(State, Provider)}.

-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, {string(), any()}}.
do(State) ->
Elvis = get_elvis_config(State),
_ = rebar_log:log(info, "elvis analysis starting, this may take a while...", []),
try elvis_core:rock(Elvis) of
ok ->
{ok, State};
{fail, _} ->
{error, "Linting failed"}
{fail, [{throw, Error} | _]} ->
rebar_api:abort("elvis_core threw an exception: ~p", [Error]);
{fail, Reason} ->
{error, {"Linting failed with ~p", Reason}}
catch
Error ->
rebar_api:abort("elvis_core threw an exception: ~p", [Error])
end.

-spec format_error(any()) -> iolist().
format_error(Reason) ->
io_lib:format("~p", [Reason]).
-spec format_error({string(), any()}) -> iolist().
format_error({Message, Reason}) ->
io_lib:format(Message, [Reason]).

-spec get_elvis_config(rebar_state:t()) -> elvis_config:configs().
get_elvis_config(State) ->
Expand Down
4 changes: 3 additions & 1 deletion test/test_app_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ test_app(_Config) ->
[#{dirs => ["src"],
filter => "*.erl",
rules => [{elvis_style, max_function_length, #{max_length => 1}}]}]),
{error, "Linting failed"} = rebar3_lint_prv:do(BadState).
{error, {Message = "Linting failed with ~p", Reason}} = rebar3_lint_prv:do(BadState),
% Check that this doesn't throw
io:format("~p", [rebar3_lint_prv:format_error({Message, Reason})]).

0 comments on commit 5e8b723

Please sign in to comment.