diff --git a/config/test.pass.config b/config/test.pass.config new file mode 100644 index 00000000..6098e555 --- /dev/null +++ b/config/test.pass.config @@ -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} + ] + } +]. + diff --git a/src/elvis_result.erl b/src/elvis_result.erl index f2f09fb9..c916b86c 100644 --- a/src/elvis_result.erl +++ b/src/elvis_result.erl @@ -1,5 +1,7 @@ -module(elvis_result). +-compile({no_auto_import, [error/2]}). + %% API -export([ new/3, @@ -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). diff --git a/src/elvis_utils.erl b/src/elvis_utils.erl index 00098f92..244a7a00 100644 --- a/src/elvis_utils.erl +++ b/src/elvis_utils.erl @@ -1,5 +1,7 @@ -module(elvis_utils). +-compile({no_auto_import, [error/2]}). + -export([ %% Rules check_lines/3, @@ -16,6 +18,8 @@ info/2, notice/1, notice/2, + error/1, + error/2, error_prn/1, error_prn/2, parse_colors/1 @@ -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) -> @@ -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) -> @@ -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 diff --git a/test/elvis_SUITE.erl b/test/elvis_SUITE.erl index b76b8cec..c54dfbc6 100644 --- a/test/elvis_SUITE.erl +++ b/test/elvis_SUITE.erl @@ -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, @@ -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 @@ -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;