Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* Add support of wildcards in xref.config paths. #30

Merged
merged 1 commit into from
Aug 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/xref_runner.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}.

-export_type([check/0, xref_default/0, config/0, warning/0]).
-export([check/0, check/1, check/2]).
-export([check/0, check/1, check/2, find_dirs/1]).

%% @doc Runs a list of checks.
%% To decide which checks to run and what options to use, it reads the
Expand Down Expand Up @@ -66,7 +66,8 @@ check(Path) ->
-spec check(check(), config()) -> [warning()].
check(Check, Config) ->
XrefDefaults = maps:get(xref_defaults, Config, []),
Dirs = maps:get(dirs, Config, [ebin()]),
ConfigDirs = maps:get(dirs, Config, [ebin()]),
Dirs = find_dirs(ConfigDirs),

lists:foreach(fun code:add_path/1, Dirs),

Expand Down Expand Up @@ -107,8 +108,15 @@ ebin() ->
end.

code_path(Config) ->
ExtraPaths = maps:get(extra_paths, Config, []),
[P || P <- code:get_path() ++ ExtraPaths, filelib:is_dir(P)].
ConfigExtraPaths = maps:get(extra_paths, Config, []),
ExtraPaths = find_dirs(ConfigExtraPaths),
[P || P <- code:get_path() ++ ExtraPaths].

%% @doc Returns all dirs under the specified wildcard
-spec find_dirs([file:name()]) -> [file:filename()].
find_dirs(Dirs) ->
ExtraPaths = lists:flatmap(fun filelib:wildcard/1, Dirs),
[Path || Path <- ExtraPaths, filelib:is_dir(Path)].

filter_xref_results(Check, Results) ->
SourceModules =
Expand Down Expand Up @@ -143,7 +151,7 @@ get_ignorelist(Mod, Check) ->

BehaviourCallbacks = get_behaviour_callbacks(Check, Mod, Attributes),

%% And create a flat {M,F,A} list
%% And create a flat {M, F, A} list
IgnoreXref ++ BehaviourCallbacks.

get_behaviour_callbacks(exports_not_used, Mod, Attributes) ->
Expand Down
88 changes: 88 additions & 0 deletions test/wildcard_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
-module(wildcard_SUITE).

-include_lib("common_test/include/ct.hrl").

-export([all/0, init_per_testcase/2, end_per_testcase/2]).

-export([
empty_list_dir/1,
not_exist_dir/1,
exist_dir/1,
match_one_character_dir/1,
match_one_character_dir_with_subdir/1,
match_any_characters_dir/1,
match_any_characters_dir_with_subdir/1,
match_all_directories_and_subdirectories/1,
match_alternatives_with_subdir/1
]).

all() ->
Exports = ?MODULE:module_info(exports),
[F || {F, 1} <- Exports, F /= module_info].

init_per_testcase(_Name, Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [
filename:join([PrivDir, X, Y]) ||
X <- ["a", "b", "c", "dd"],
Y <- ["", "ebin"]
],
[file:make_dir(Dir) || Dir <- Dirs],
[{dirs, Dirs} | Config].

end_per_testcase(_Name, Config) ->
Dirs = ?config(dirs, Config),
[file:del_dir(Dir) || Dir <- Dirs],
ok.

empty_list_dir(_Config) ->
[] = xref_runner:find_dirs([]).

not_exist_dir(_Config) ->
[] = xref_runner:find_dirs(["not_exist"]).

exist_dir(Config) ->
PrivDir = filename:join([?config(priv_dir, Config)]),
[PrivDir] = xref_runner:find_dirs([PrivDir]).

match_one_character_dir(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "?"])],
Found = xref_runner:find_dirs(Dirs),
["a", "b", "c"] = basenames(Found).

match_one_character_dir_with_subdir(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "?", "ebin"])],
Found = xref_runner:find_dirs(Dirs),
["a", "b", "c"] = basenames(dirnames(Found)).

match_any_characters_dir(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "*"])],
Found = xref_runner:find_dirs(Dirs),
["a", "b", "c", "dd"] = basenames(Found).

match_any_characters_dir_with_subdir(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "*", "ebin"])],
Found = xref_runner:find_dirs(Dirs),
["a", "b", "c", "dd"] = basenames(dirnames(Found)).

match_all_directories_and_subdirectories(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "**", "ebin"])],
Found = xref_runner:find_dirs(Dirs),
["a", "b", "c", "dd"] = basenames(dirnames(Found)).

match_alternatives_with_subdir(Config) ->
PrivDir = ?config(priv_dir, Config),
Dirs = [filename:join([PrivDir, "{a,dd}", "ebin"])],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space after "," on line 72

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space after "," on line 99

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space after "," on line 80

Found = xref_runner:find_dirs(Dirs),
["a", "dd"] = basenames(dirnames(Found)).

basenames(Dirs) ->
[filename:basename(Dir) || Dir <- Dirs].

dirnames(Dirs) ->
[filename:dirname(Dir) || Dir <- Dirs].