forked from 2600hz/kazoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eunit_run.escript
executable file
·109 lines (96 loc) · 3.46 KB
/
eunit_run.escript
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env escript
%%! +A0
%%! -sasl sasl_error_logger {file, "sasl.log"}
%%! -noshell -noinput
%% -*- coding: utf-8 -*-
-mode(compile).
-export([main/1]).
main(Args) ->
_ = io:setopts('user', [{'encoding', 'unicode'}]),
maybe_add_ebin(),
run_eunit(parse_args(Args, #{modules => []})).
maybe_add_ebin() ->
{'ok', CWD} = file:get_cwd(),
Ebin = filename:join([CWD, "ebin"]),
case filelib:is_dir(Ebin) of
'true' -> code:add_patha(Ebin);
'false' -> 'ok'
end.
parse_args([], Opts) ->
Opts;
parse_args(["--with-cover" | Rest], Opts) ->
parse_args(Rest, Opts#{cover => 'true'});
parse_args(["--cover-project-name", ProjectName | Rest], Opts) ->
parse_args(Rest, Opts#{coverdata => ProjectName ++ ".coverdata"});
parse_args(["--cover-report-dir", Dir | Rest], Opts) ->
parse_args(Rest, Opts#{report_dir => Dir});
parse_args([Mod | Rest], #{modules := Modules}=Opts) ->
parse_args(Rest, Opts#{modules => [Mod | Modules]}).
run_eunit(#{modules := []}) ->
io:format('user', "No modules are specified.\n", []),
usage();
run_eunit(#{modules := Modules}=Opts) ->
_Cover = maybe_start_cover(Opts),
TestMods = filter_same_name_test_modules(Modules),
case eunit:test(TestMods, eunit_options(Opts)) of
'ok' ->
maybe_stop_cover(Opts),
erlang:halt();
'error' ->
erlang:halt(1)
end.
eunit_options(#{report_dir := Dir}) ->
['verbose'
,{'report', {'eunit_surefire', [{'dir', Dir}]}}
];
eunit_options(_Options) ->
['verbose'].
is_loaded(M) ->
case 'true' =:= code:is_loaded(M)
orelse code:ensure_loaded(M)
of
'true' -> 'true';
{'module', M} -> 'true';
{'error', _E} ->
io:format('user', "failed to find ~p: ~p~n", [M, _E]),
'false'
end.
filter_same_name_test_modules(Modules) ->
Fun = fun(Mod, Acc) ->
Module = filename:rootname(filename:basename(Mod)),
case lists:reverse(Module) of
"stset_"++M ->
case not lists:member(lists:reverse(M), Modules) of
'false' -> Acc;
'true' ->
'true' = is_loaded(list_to_atom(Module)),
[list_to_atom(Module) | Acc]
end;
_ ->
'true' = is_loaded(list_to_atom(Module)),
[list_to_atom(Module) | Acc]
end
end,
lists:usort(lists:foldl(Fun, [], Modules)).
maybe_start_cover(#{cover := 'true'
,coverdata := _
}=Opts) ->
'ok' = filelib:ensure_dir(maps:get('report_dir', Opts, "cover") ++ "/dummy"),
_ = cover:start(),
cover:compile_beam_directory("ebin");
maybe_start_cover(#{cover := 'true'}) ->
io:format('user', "No project name is specified.\n", []),
usage();
maybe_start_cover(_) ->
'ok'.
maybe_stop_cover(#{cover := 'true'
,coverdata := Coverdata
}=Opts) ->
cover:export(Coverdata),
cover:analyse_to_file(['html', {'outdir', maps:get('report_dir', Opts, "cover")}]);
maybe_stop_cover(_) ->
'ok'.
usage() ->
Arg0 = escript:script_name(),
io:format('user', "Usage: ~s [--with-cover] [--cover-project-name <project name>] [--cover-report-dir <cover report dir>] <erlang module name/>+\n", [filename:basename(Arg0)]),
halt(1).