Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add verbosity config option, output only errors unless set #88

Merged
merged 2 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/test.pass.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[ % this check passes
{
elvis,
[
{config,
[#{dirs => ["../../_build/test/lib/elvis/test/examples"],
filter => "**.erl",
rules => [{elvis_style, line_length, #{limit => 800}]
}]
},
{output_format, plain}
]
}
].

18 changes: 10 additions & 8 deletions src/elvis_result.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(elvis_result).

-compile({no_auto_import, [error/2]}).

%% API
-export([
new/3,
Expand Down Expand Up @@ -112,22 +114,22 @@ print([Result | Results]) ->
%% File
print(#{file := File, rules := Rules}) ->
Path = elvis_file:path(File),
Status = case status(Rules) of
ok -> "{{green-bold}}OK";
fail -> "{{red-bold}}FAIL"
end,

elvis_utils:notice("# ~s [~s{{white-bold}}]", [Path, Status]),
case status(Rules) of
ok ->
elvis_utils:notice("# ~s [{{green-bold}}OK{{white-bold}}]", [Path]);
fail ->
elvis_utils:error("# ~s [{{red-bold}}FAIL{{white-bold}}]", [Path])
end,
print(Rules);
%% Rule
print(#{items := []}) ->
ok;
print(#{name := Name, items := Items}) ->
elvis_utils:notice(" - ~s", [atom_to_list(Name)]),
elvis_utils:error(" - ~s", [atom_to_list(Name)]),
print(Items);
%% Item
print(#{message := Msg, info := Info}) ->
elvis_utils:notice(" - " ++ Msg, Info);
elvis_utils:error(" - " ++ Msg, Info);
%% Error
print(#{error_msg := Msg, info := Info}) ->
elvis_utils:error_prn(Msg, Info).
Expand Down
23 changes: 21 additions & 2 deletions src/elvis_utils.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(elvis_utils).

-compile({no_auto_import, [error/2]}).

-export([
%% Rules
check_lines/3,
Expand All @@ -16,6 +18,8 @@
info/2,
notice/1,
notice/2,
error/1,
error/2,
error_prn/1,
error_prn/2,
parse_colors/1
Expand Down Expand Up @@ -129,7 +133,7 @@ info(Message) ->
-spec info(string(), [term()]) -> ok.
info(Message, Args) ->
ColoredMessage = Message ++ "{{reset}}~n",
print(ColoredMessage, Args).
print_info(ColoredMessage, Args).

-spec notice(string()) -> ok.
notice(Message) ->
Expand All @@ -138,8 +142,16 @@ notice(Message) ->
-spec notice(string(), [term()]) -> ok.
notice(Message, Args) ->
ColoredMessage = "{{white-bold}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).
print_info(ColoredMessage, Args).

-spec error(string()) -> ok.
error(Message) ->
error(Message, []).

-spec error(string(), [term()]) -> ok.
error(Message, Args) ->
ColoredMessage = "{{white-bold}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).

-spec error_prn(string()) -> ok.
error_prn(Message) ->
Expand All @@ -150,6 +162,13 @@ error_prn(Message, Args) ->
ColoredMessage = "{{red}}Error: {{reset}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).

-spec print_info(string(), [term()]) -> ok.
print_info(Message, Args) ->
case application:get_env(elvis, verbose) of
{ok, true} -> print(Message, Args);
_ -> ok
end.

-spec print(string(), [term()]) -> ok.
print(Message, Args) ->
case application:get_env(elvis, no_output) of
Expand Down
50 changes: 49 additions & 1 deletion test/elvis_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
rock_this_not_skipping_files/1,
rock_this_skipping_files/1,
rock_without_colors/1,
rock_with_no_output_has_no_output/1,
rock_with_errors_has_output/1,
rock_without_errors_has_no_output/1,
rock_without_errors_and_with_verbose_has_output/1,
rock_with_rule_groups/1,
%% Utill & Config
%% Util & Config
throw_configuration/1,
find_file_and_check_src/1,
find_file_with_ignore/1,
Expand Down Expand Up @@ -185,6 +189,44 @@ rock_without_colors(_Config) ->
_:{badmatch, []} -> ok
end.

-spec rock_with_no_output_has_no_output(config()) -> ok.
rock_with_no_output_has_no_output(_Config) ->
application:set_env(elvis, no_output, true),
ConfigPath = "../../config/test.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis_core:rock(ElvisConfig) end,
[] = check_no_line_output(Fun),
application:unset_env(elvis, no_output),
ok.

-spec rock_with_errors_has_output(config()) -> ok.
rock_with_errors_has_output(_Config) ->
ConfigPath = "../../config/test.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis_core:rock(ElvisConfig) end,
Expected = "FAIL",
[_|_] = check_some_line_output(Fun, Expected, fun matches_regex/2),
ok.

-spec rock_without_errors_has_no_output(config()) -> ok.
rock_without_errors_has_no_output(_Config) ->
ConfigPath = "../../config/test.pass.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis_core:rock(ElvisConfig) end,
[] = check_no_line_output(Fun),
ok.

-spec rock_without_errors_and_with_verbose_has_output(config()) -> ok.
rock_without_errors_and_with_verbose_has_output(_Config) ->
application:set_env(elvis, verbose, true),
ConfigPath = "../../config/test.pass.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis_core:rock(ElvisConfig) end,
Expected = "OK",
[_|_] = check_some_line_output(Fun, Expected, fun matches_regex/2),
application:unset_env(elvis, verbose),
ok.

-spec rock_with_rule_groups(Config::config()) -> ok.
rock_with_rule_groups(_Config) ->
% elvis_config will load default elvis_core rules for every
Expand Down Expand Up @@ -315,6 +357,12 @@ check_some_line_output(Fun, Expected, FilterFun) ->
ListFun = fun(Line) -> FilterFun(Line, Expected) end,
[_ | _] = lists:filter(ListFun, Lines).

check_no_line_output(Fun) ->
_ = ct:capture_start(),
_ = Fun(),
_ = ct:capture_stop(),
[] = ct:capture_get([]).

matches_regex(Result, Regex) ->
case re:run(Result, Regex) of
{match, _} -> true;
Expand Down