forked from bagwanpankaj/erlang_learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.erl
55 lines (46 loc) · 1.81 KB
/
tester.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-module(tester).
-export([dir/0, dir/2]).
-define(EXT, ".erl"). % file extension to look for
-define(MODS, "./").
-define(TESTS, "./tests/").
%% scans both a module directory and a test directory, compiles the
%% modules inside and then call for tests to be ran.
%%
%% usage:
%% tester:dir("./","./tests/").
dir() -> dir(?MODS, ?TESTS).
dir(ModulePath, TestPath) ->
ModuleList = module_list(ModulePath),
TestList = module_list(TestPath),
[compile(ModulePath++X) || X <- ModuleList],
[compile(TestPath++X) || X <- TestList],
test_all(TestList),
warnings(),
cleanup(ModuleList),
cleanup(TestList),
ok.
%% assumes pre-compiled modules
test_all(FileList) ->
Split = [lists:nth(1, string:tokens(File, ".")) || File <- FileList],
[eunit:test(list_to_existing_atom(F), [verbose]) || F <- Split].
% [(list_to_existing_atom(F)):test() || F <- Split].
cleanup(Files) ->
[file:delete(lists:nth(1, string:tokens(F, "."))++".beam") || F <- Files].
%% get module .erl file names from a directory
module_list(Path) ->
SameExt = fun(File) -> get_ext(File) =:= ?EXT end,
{ok, Files} = file:list_dir(Path),
lists:filter(SameExt, Files).
%% find the extension of a file (length is taken from the ?EXT macro).
get_ext(Str) ->
lists:reverse(string:sub_string(lists:reverse(Str), 1, length(?EXT))).
compile(FileName) ->
compile:file(FileName, [report, verbose, export_all]).
warnings() ->
Warns = [{Mod, get_warnings(Mod)} || {Mod,_Path} <- code:all_loaded(),
has_warnings(Mod)],
io:format("These need to be tested better: ~n\t~p~n", [Warns]).
has_warnings(Mod) ->
is_list(get_warnings(Mod)).
get_warnings(Mod) ->
proplists:get_value(test_warnings, Mod:module_info(attributes)).