From 99d6fb4d45980e6c60a119fca2bff63ba8f7d226 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 17:14:57 -0300 Subject: [PATCH 1/3] [#252] Take the dirs from the rule group into account when filtering files --- src/elvis.erl | 3 ++- src/elvis_config.erl | 10 ++++++---- src/elvis_file.erl | 23 +++++++++++++++-------- src/elvis_webhook.erl | 3 +-- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/elvis.erl b/src/elvis.erl index c668b4d..7efa76b 100644 --- a/src/elvis.erl +++ b/src/elvis.erl @@ -86,7 +86,8 @@ rock_this(Path, Config) -> FilterFun = fun(Cfg) -> Filter = elvis_config:filter(Cfg), - [] =/= elvis_file:filter_files([File], Filter) + Dirs = elvis_config:dirs(Cfg), + [] =/= elvis_file:filter_files([File], Dirs, Filter) end, FilteredConfig = lists:filter(FilterFun, NewConfig), diff --git a/src/elvis_config.erl b/src/elvis_config.erl index 7514199..c54b124 100644 --- a/src/elvis_config.erl +++ b/src/elvis_config.erl @@ -116,7 +116,7 @@ files(_RuleGroup = #{files := Files}) -> files(#{}) -> undefined. --spec rules(config()) -> [string()]. +-spec rules(config()) -> [string()] | undefined. rules(_RuleGroup = #{rules := Rules}) -> Rules; rules(#{}) -> @@ -131,11 +131,13 @@ resolve_files(Config, Files) when is_list(Config) -> Fun = fun(RuleGroup) -> resolve_files(RuleGroup, Files) end, lists:map(Fun, Config); resolve_files(RuleGroup = #{filter := Filter}, Files) -> - Filter = maps:get(filter, RuleGroup), - FilteredFiles = elvis_file:filter_files(Files, Filter), + Filter = filter(RuleGroup), + Dirs = dirs(RuleGroup), + FilteredFiles = elvis_file:filter_files(Files, Dirs, Filter), RuleGroup#{files => FilteredFiles}; resolve_files(RuleGroup, Files) -> - FilteredFiles = elvis_file:filter_files(Files, ?DEFAULT_FILTER), + Dirs = dirs(RuleGroup), + FilteredFiles = elvis_file:filter_files(Files, Dirs, ?DEFAULT_FILTER), RuleGroup#{files => FilteredFiles}. %% @doc Takes a configuration and finds all files according to its 'dirs' diff --git a/src/elvis_file.erl b/src/elvis_file.erl index 2b15139..2d3bc59 100644 --- a/src/elvis_file.erl +++ b/src/elvis_file.erl @@ -8,7 +8,7 @@ find_files/2, find_files/3, - filter_files/2 + filter_files/3 ]). -export_type([file/0]). @@ -81,13 +81,20 @@ 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()) -> [file()]. -filter_files(Files, Filter) -> - Regex = glob_to_regex(Filter), - FilterFun = fun(#{path := Path}) -> - match == re:run(Path, Regex, [{capture, none}]) - end, - lists:filter(FilterFun, Files). +-spec filter_files([file()], [string()], string()) -> [file()]. +filter_files(Files, Dirs, Filter) -> + FullFilters = lists:map(fun(Dir) -> Dir ++ "/" ++ Filter end, Dirs), + Regexes = lists:map(fun glob_to_regex/1, FullFilters), + FlatmapFun = + fun(Regex) -> + FilterFun = + fun(#{path := Path}) -> + match == re:run(Path, Regex, [{capture, none}]) + end, + lists:filter(FilterFun, Files) + end, + + lists:flatmap(FlatmapFun, Regexes). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Private diff --git a/src/elvis_webhook.erl b/src/elvis_webhook.erl index 9fab569..a94ebb7 100644 --- a/src/elvis_webhook.erl +++ b/src/elvis_webhook.erl @@ -60,8 +60,7 @@ repo_config(Cred, Repo, Branch, LocalConfig) -> case egithub:file_content(Cred, Repo, Branch, "elvis.config") of {ok, ConfigContent} -> ConfigEval = ktn_code:eval(ConfigContent), - RepoConfig = elvis_config:load(ConfigEval), - RepoConfig; + elvis_config:load(ConfigEval); {error, _} -> LocalConfig end. From 3ba7fcdb1280f90121beef5c16ef870f8dcd6d7d Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 18:02:50 -0300 Subject: [PATCH 2/3] [#252] Fixed tests based on filter_files changes --- test/elvis_SUITE.erl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/elvis_SUITE.erl b/test/elvis_SUITE.erl index 1a0e417..6429b42 100644 --- a/test/elvis_SUITE.erl +++ b/test/elvis_SUITE.erl @@ -348,17 +348,19 @@ main_git_hook_fail(_Config) -> LongLine = <<"Loooooooooooooooooooooooooooooooooooooooooooooooooooong ", "Liiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiine">>, Files = [ - #{path => "fake_long_line.erl", + #{path => "../../src/fake_long_line.erl", content => LongLine}, - #{path => "README.md", + #{path => "../../test/fake_long_line.erl", + content => LongLine}, + #{path => "../../src/README.md", content => <<"### Title">>}, - #{path => "Makefile", + #{path => "../../src/Makefile", content => <<"@Some text\n\nCT_OPTS =">>} ], FakeStagedFiles = fun() -> Files end, meck:expect(elvis_git, staged_files, FakeStagedFiles), - Expected = "# fake_long_line.erl.*FAIL", + Expected = "# ../../src/fake_long_line.erl.*FAIL", ConfigArgs = "git-hook -c ../../config/elvis-test.config", ConfigFun = fun() -> elvis:main(ConfigArgs) end, From cafd665daed851d10bafd37b55cb55c50e5ec85d Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 18:06:03 -0300 Subject: [PATCH 3/3] [#252] Add module to cover spec --- test/elvis.coverspec | 1 + 1 file changed, 1 insertion(+) diff --git a/test/elvis.coverspec b/test/elvis.coverspec index f68350a..0a697e7 100644 --- a/test/elvis.coverspec +++ b/test/elvis.coverspec @@ -5,6 +5,7 @@ elvis, elvis_result, elvis_utils, + elvis_file, elvis_style, elvis_project, elvis_config,