Skip to content

Commit

Permalink
Merge pull request #179 from paulo-ferraz-oliveira/fix/analysis-over-…
Browse files Browse the repository at this point in the history
…umbrella-apps

Fix analysis over umbrella apps
  • Loading branch information
Brujo Benavides authored Mar 10, 2021
2 parents c4ffeb0 + 111ef39 commit 97f8e02
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 43 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ on:
- master
jobs:
ci:
name: Run checks and tests over ${{matrix.otp_vsn}} and ${{matrix.os}}
name: >
Run Linux-based checks and tests over ${{matrix.otp_vsn}} and ${{matrix.os}}
runs-on: ${{matrix.os}}
strategy:
matrix:
otp_vsn: [19.3.6.13, 20.3.8.26, 21.3.8.17, 22.3.4.9, 23.1.5]
os: [ubuntu-latest]
os: [ubuntu-18.04]
steps:
- uses: actions/checkout@v2
- uses: gleam-lang/setup-erlang@v1.0.0
- uses: gleam-lang/setup-erlang@v1.1.1
with:
otp-version: ${{matrix.otp_vsn}}
- run: |
Expand Down
16 changes: 16 additions & 0 deletions config/elvis-umbrella.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{elvis, [
{config, [
#{ dirs => [
"../../_build/test/lib/elvis_core/test/dirs/src/**"
, "../../_build/test/lib/elvis_core/test/dirs/test/**"
, "../../_build/test/lib/elvis_core/test/dirs/apps/**/src"
, "../../_build/test/lib/elvis_core/test/dirs/apps/**/test"
]
, filter => "*.erl"
, ruleset => erl_files
}
]
}
]}
].
71 changes: 31 additions & 40 deletions src/elvis_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,43 @@ find_files(Dirs, Pattern) ->
end,
[#{path => Path} || Path <- lists:usort(lists:flatmap(Fun, Dirs))].

dir_to(Filter, _Dir = ".") ->
Filter;
dir_to(Filter, Dir) ->
filename:join(Dir, Filter).

file_in(ExpandedFilter, Files) ->
lists:filter(fun (_File = #{ path := Path }) ->
lists:member(Path, ExpandedFilter)
end,
Files).

%% @doc Filter files based on the glob provided.
-spec filter_files([file()], [string()], string(), [string()]) -> [file()].
filter_files(Files, Dirs, Filter, IgnoreList) ->
AppendFilter = fun(Dir) ->
case Dir of
"." -> Filter;
Dir -> reduce_stars(Dir ++ "/" ++ Filter)
end
end,
FullFilters = lists:map(AppendFilter, 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,
IgnoreFun =
fun(#{path := Path}) ->
IsIgnored =
ExpandedFilters
= lists:map(
fun (Dir) ->
filelib:wildcard(dir_to(Filter, Dir))
end,
Dirs),
Found
= lists:flatmap(
fun (ExpandedFilter) ->
file_in(ExpandedFilter, Files)
end,
ExpandedFilters),
% File src/sub/file.erl will match both src/ and src/sub/ folders. We can't have that!
FoundUnique = lists:usort(Found),
lists:filter(
fun(#{ path := Path }) ->
not lists:any(
fun(Regex) ->
match == re:run(Path, Regex, [{capture, none}])
match == re:run(Path, Regex, [{capture, none}])
end,
not lists:any(IsIgnored, IgnoreList)
IgnoreList)
end,
Found = lists:flatmap(FlatmapFun, Regexes),
% File src/sub/file.erl will match both src/ and src/sub/ folders. We can't have that!
FoundUnique = lists:usort(Found),
lists:filter(IgnoreFun, FoundUnique).
FoundUnique).

%% @doc Return module name corresponding to a given .erl/.beam file
-spec module(file()) -> module().
Expand Down Expand Up @@ -147,21 +153,6 @@ resolve_parse_tree(".erl", Content, Mod, Ignore) ->
resolve_parse_tree(_, _, _, _) ->
undefined.

-spec glob_to_regex(iodata()) -> iodata().
glob_to_regex(Glob) ->
add_delimiters(replace_questions(replace_stars(escape_all_chars(Glob)))).

add_delimiters(Glob) -> [$^, Glob, $$].

escape_all_chars(Glob) -> re:replace(Glob, ".", "[&]", [global]).

replace_stars(Glob) -> re:replace(Glob, "[[][*][]]", ".*", [global]).

replace_questions(Glob) -> re:replace(Glob, "[[][?][]]", ".", [global]).

reduce_stars(DirAndFilter) ->
re:replace(DirAndFilter, "/\\*+/", "/", [global, {return, list}]).

-spec find_encoding(Content::binary()) ->
atom().
find_encoding(Content) ->
Expand Down
3 changes: 3 additions & 0 deletions test/dirs/apps/app1/src/dirs_apps_app1_src.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_apps_app1_src).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
3 changes: 3 additions & 0 deletions test/dirs/apps/app1/test/dirs_apps_app1_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_apps_app1_test).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
3 changes: 3 additions & 0 deletions test/dirs/apps/app2/src/dirs_apps_app2_src.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_apps_app2_src).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
3 changes: 3 additions & 0 deletions test/dirs/apps/app2/test/dirs_apps_app2_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_apps_app2_test).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
3 changes: 3 additions & 0 deletions test/dirs/src/dirs_src.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_src).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
3 changes: 3 additions & 0 deletions test/dirs/test/dir_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-module(dirs_test).

% this line will issue and error: we're not testing the static analysis mechanism, but the folder structure walking mechanism
15 changes: 15 additions & 0 deletions test/elvis_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
rock_with_rule_groups/1,
rock_this_skipping_files/1,
rock_this_not_skipping_files/1,
rock_with_umbrella_apps/1,
%% Util & Config
custom_ruleset/1,
throw_configuration/1,
Expand Down Expand Up @@ -319,6 +320,20 @@ rock_this_not_skipping_files(_Config) ->
meck:unload(elvis_file),
ok.

-spec rock_with_umbrella_apps(config()) -> ok.
rock_with_umbrella_apps(_Config) ->
ElvisUmbrellaConfigFile = "../../config/elvis-umbrella.config",
ElvisConfig = elvis_config:from_file(ElvisUmbrellaConfigFile),
{fail, [
#{ file := "../../_build/test/lib/elvis_core/test/dirs/test/dir_test.erl" }
, #{ file := "../../_build/test/lib/elvis_core/test/dirs/src/dirs_src.erl" }
, #{ file := "../../_build/test/lib/elvis_core/test/dirs/apps/app2/test/dirs_apps_app2_test.erl" }
, #{ file := "../../_build/test/lib/elvis_core/test/dirs/apps/app2/src/dirs_apps_app2_src.erl" }
, #{ file := "../../_build/test/lib/elvis_core/test/dirs/apps/app1/test/dirs_apps_app1_test.erl" }
, #{ file := "../../_build/test/lib/elvis_core/test/dirs/apps/app1/src/dirs_apps_app1_src.erl" }
]} = elvis_core:rock(ElvisConfig),
ok.

%%%%%%%%%%%%%%%
%%% Utils

Expand Down

0 comments on commit 97f8e02

Please sign in to comment.