Skip to content

Commit

Permalink
Add tests, change parsable to be a format_option
Browse files Browse the repository at this point in the history
  • Loading branch information
define-null committed Apr 10, 2019
1 parent ae22776 commit 01593b8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/elvis_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ apply_rules(Config, File) ->
{RulesResults, _, _} = lists:foldl(fun apply_rule/2, Acc, Rules),

Results = elvis_result:new(file, File, RulesResults),
elvis_result:print(Results),
elvis_result:print_results(Results),
Results.

apply_rule({Module, Function}, {Result, Config, File}) ->
Expand Down
70 changes: 37 additions & 33 deletions src/elvis_result.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
new/4,
status/1,
clean/1,
print/1
print_results/1
]).

-export([
Expand Down Expand Up @@ -105,60 +105,64 @@ get_line_num(#{line_num := LineNum}) -> LineNum.

%% Print

-spec print(item() | rule() | elvis_error() | [file()]) -> ok.
print([]) ->
-spec print_results(file()) -> ok.
print_results(Results) ->
Format = application:get_env(elvis, output_format, colors),
print(Format, Results).

-spec print(plain | colors | parsable, [file()] | file()) -> ok.
print(_, []) ->
ok;
print([Result | Results]) ->
print(Result),
print(Results);
print(Format, [Result | Results]) ->
print(Format, Result),
print(Format, Results);
%% File
print(#{file := File, rules := Rules}) ->
print(Format, #{file := File, rules := Rules}) ->
Path = elvis_file:path(File),
case application:get_env(elvis, parsable, false) of
false ->
case Format of
parsable -> ok;
_ ->
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;
true ->
ok
end
end,
print_rules(Path, Rules);
print(Error) ->
print_rules(Format, Path, Rules);
print(_, Error) ->
print_error(Error).

print_rules(_File, []) ->
print_rules(_Format, _File, []) ->
ok;
print_rules(File, [#{items := []} | Items]) ->
print_rules(File, Items);
print_rules(File, [#{items := Items, name := Name} | EItems]) ->
case application:get_env(elvis, parsable, false) of
true -> ok;
false->
print_rules(Format, File, [#{items := []} | Items]) ->
print_rules(Format, File, Items);
print_rules(Format, File, [#{items := Items, name := Name} | EItems]) ->
case Format of
parsable -> ok;
_ ->
elvis_utils:error(" - ~s", [atom_to_list(Name)])
end,
print_item(File, Name, Items),
print_rules(File, EItems);
print_rules(File, [Error | Items]) ->
print_item(Format, File, Name, Items),
print_rules(Format, File, EItems);
print_rules(Format, File, [Error | Items]) ->
print_error(Error),
print_rules(File, Items).
print_rules(Format, File, Items).

%% Item
print_item(File, Name, [#{message := Msg, line_num := Ln, info := Info} | Items]) ->
case application:get_env(elvis, parsable, false) of
true ->
print_item(Format, File, Name, [#{message := Msg, line_num := Ln, info := Info} | Items]) ->
case Format of
parsable ->
FMsg = io_lib:format(Msg, Info),
io:format("~s:~p:~p:~s~n", [File, Ln, Name, FMsg]);
false->
_ ->
elvis_utils:error(" - " ++ Msg, Info)
end,
print_item(File, Name, Items);
print_item(File, Name, [Error|Items]) ->
print_item(Format, File, Name, Items);
print_item(Format, File, Name, [Error|Items]) ->
print_error(Error),
print_item(File, Name, Items);
print_item(_File, _Name, []) ->
print_item(Format, File, Name, Items);
print_item(_Format, _File, _Name, []) ->
ok.

print_error(#{error_msg := Msg, info := Info}) ->
Expand Down
22 changes: 21 additions & 1 deletion test/elvis_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
rock_with_rebar_default_config/1,
rock_this/1,
rock_without_colors/1,
rock_with_parsable/1,
rock_with_no_output_has_no_output/1,
rock_with_errors_has_output/1,
rock_without_errors_has_no_output/1,
Expand Down Expand Up @@ -190,6 +191,24 @@ rock_without_colors(_Config) ->
_:{badmatch, []} -> ok
end.

-spec rock_with_parsable(config()) -> ok.
rock_with_parsable(_Config) ->
{ok, Default} = application:get_env(elvis, output_format),
application:set_env(elvis, output_format, parsable),
ConfigPath = "../../config/test.config",
ElvisConfig = elvis_config:load_file(ConfigPath),
Fun = fun() -> elvis_core:rock(ElvisConfig) end,
Expected = ".*\\.erl:\\d:[a-zA-Z0-9_]+:.*",
ok = try check_some_line_output(Fun, Expected, fun matches_regex/2) of
Result ->
io:format("~p~n", [Result])
catch
_:{badmatch, []} ->
ct:fail("Unexpected result ~p")
after
application:set_env(elvis, output_format, Default)
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),
Expand Down Expand Up @@ -424,5 +443,6 @@ check_no_line_output(Fun) ->
matches_regex(Result, Regex) ->
case re:run(Result, Regex) of
{match, _} -> true;
nomatch -> false
nomatch ->
false
end.

0 comments on commit 01593b8

Please sign in to comment.