Skip to content

Commit

Permalink
Add group level ignore
Browse files Browse the repository at this point in the history
This commit intorduces a `ignore` clause on the same level as
the `dirs` or `filter` clause, it can take both atoms or regex's.
Atoms are turned into a regex by taking advantage of erlangs
convention that module name and file name have to be the same.
  • Loading branch information
Licenser committed Nov 26, 2015
1 parent c5e813e commit 7efc306
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/elvis_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
normalize/1,
%% Geters
dirs/1,
ignore/1,
filter/1,
files/1,
rules/1,
Expand Down Expand Up @@ -104,6 +105,14 @@ dirs(_RuleGroup = #{dirs := Dirs}) ->
dirs(#{}) ->
[].

-spec ignore(config()) -> [string()].
ignore(Config) when is_list(Config) ->
lists:flatmap(fun ignore/1, Config);
ignore(_RuleGroup = #{ignore := Ignore}) ->
lists:map(fun ignore_to_regexp/1, Ignore);
ignore(#{}) ->
[].

-spec filter(config()) -> [string()].
filter(_RuleGroup = #{filter := Filter}) ->
Filter;
Expand Down Expand Up @@ -161,3 +170,15 @@ apply_to_files(Fun, Config) when is_list(Config) ->
apply_to_files(Fun, RuleGroup = #{files := Files}) ->
NewFiles = lists:map(Fun, Files),
RuleGroup#{files => NewFiles}.

%% @doc Ensures the ignore is a regexp, this is used
%% to allow using 'module name' atoms in the ignore
%% list by taking advantage of the fact that erlang
%% enforces the module and the file name to be the
%% same.
%% @end
-spec ignore_to_regexp(string() | atom()) -> string().
ignore_to_regexp(R) when is_list(R) ->
R;
ignore_to_regexp(A) when is_atom(A) ->
"/" ++ atom_to_list(A) ++ "\\.erl$".
4 changes: 3 additions & 1 deletion src/elvis_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ rock_this(Path, Config) ->
FilterFun = fun(Cfg) ->
Filter = elvis_config:filter(Cfg),
Dirs = elvis_config:dirs(Cfg),
[] =/= elvis_file:filter_files([File], Dirs, Filter)
IgnoreList = elvis_config:ignore(Cfg),
[] =/= elvis_file:filter_files([File], Dirs, Filter,
IgnoreList)
end,
FilteredConfig = lists:filter(FilterFun, NewConfig),

Expand Down
18 changes: 13 additions & 5 deletions src/elvis_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

find_files/2,
find_files/3,
filter_files/3
filter_files/4
]).

-export_type([file/0]).
Expand Down Expand Up @@ -81,8 +81,8 @@ find_files(Dirs, Pattern, Option) ->
[#{path => Path} || Path <- lists:flatmap(Fun, Dirs)].

%% @doc Filter files based on the glob provided.
-spec filter_files([file()], [string()], string()) -> [file()].
filter_files(Files, Dirs, Filter) ->
-spec filter_files([file()], [string()], string(), [string()]) -> [file()].
filter_files(Files, Dirs, Filter, IgnoreList) ->
FullFilters = lists:map(fun(Dir) -> Dir ++ "/" ++ Filter end, Dirs),
Regexes = lists:map(fun glob_to_regex/1, FullFilters),
FlatmapFun =
Expand All @@ -93,8 +93,16 @@ filter_files(Files, Dirs, Filter) ->
end,
lists:filter(FilterFun, Files)
end,

lists:flatmap(FlatmapFun, Regexes).
IgnoreFun =
fun(#{path := Path}) ->
IsIgnored =
fun(Regex) ->
match == re:run(Path, Regex, [{capture, none}])
end,
not lists:any(IsIgnored, IgnoreList)
end,
Found = lists:flatmap(FlatmapFun, Regexes),
lists:filter(IgnoreFun, Found).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Private
Expand Down
12 changes: 12 additions & 0 deletions test/elvis_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
%% Utill & Config
throw_configuration/1,
find_file_and_check_src/1,
find_file_with_ignore/1,
invalid_file/1,
to_string/1
]).
Expand Down Expand Up @@ -197,6 +198,17 @@ find_file_and_check_src(_Config) ->
{<<"-module(small).\n">>, _} = elvis_file:src(File),
{error, enoent} = elvis_file:src(#{path => "doesnt_exist.erl"}).


-spec find_file_with_ignore(config()) -> any().
find_file_with_ignore(_Config) ->
Dirs = ["../../test/examples"],
Filter = "find_test*.erl",
Ignore = elvis_config:ignore(#{ignore => [find_test1]}),
Files = [_, _] = elvis_file:find_files(Dirs, Filter),
[_, _] = elvis_file:filter_files(Files, Dirs, Filter, []),
[#{path := "../../test/examples/find_test2.erl"}] =
elvis_file:filter_files(Files, Dirs, Filter, Ignore).

-spec invalid_file(config()) -> any().
invalid_file(_Config) ->
ok = try
Expand Down
1 change: 1 addition & 0 deletions test/examples/find_test1.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module(find_test1).
1 change: 1 addition & 0 deletions test/examples/find_test2.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-module(find_test2).

0 comments on commit 7efc306

Please sign in to comment.