From 5e8b723473ef2e4d3c6975d5117b0d6fb0c463e2 Mon Sep 17 00:00:00 2001 From: "Paulo F. Oliveira" Date: Wed, 17 Jan 2024 23:43:28 +0000 Subject: [PATCH] Fix our spec.s as per elvis_core and output more context on error 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 --- src/rebar3_lint_prv.erl | 14 ++++++++------ test/test_app_SUITE.erl | 4 +++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/rebar3_lint_prv.erl b/src/rebar3_lint_prv.erl index 208ce91..a140195 100644 --- a/src/rebar3_lint_prv.erl +++ b/src/rebar3_lint_prv.erl @@ -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) -> diff --git a/test/test_app_SUITE.erl b/test/test_app_SUITE.erl index d55e235..4479f53 100644 --- a/test/test_app_SUITE.erl +++ b/test/test_app_SUITE.erl @@ -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})]).