PageRenderTime 129ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 82ms app.codeStats 0ms

/support/run_tests.escript

http://github.com/basho/mochiweb
Unknown | 94 lines | 84 code | 10 blank | 0 comment | 0 complexity | 099880f95868227c84f7995cd5079f2a MD5 | raw file
 1#!/usr/bin/env escript
 2%% -*- erlang -*-
 3%%! -name mochiweb__test@127.0.0.1
 4main([Ebin]) ->
 5    code:add_path(Ebin),
 6    code:add_paths(filelib:wildcard("../deps/*/ebin", Ebin)),
 7    code:add_paths(filelib:wildcard("../deps/*/deps/*/ebin", Ebin)),
 8
 9    ModuleNames = [hd(string:tokens(M, "."))
10                   || "../src/" ++ M <- filelib:wildcard("../src/*.erl")],
11
12    {ok, NonTestRe} = re:compile("_tests$"),
13    Modules = [list_to_atom(M) ||
14                  M <- lists:filter(
15                         fun(M) ->
16                                 nomatch == re:run(M, NonTestRe)
17                         end,
18                         ModuleNames)],
19
20
21    crypto:start(),
22    start_cover(Modules),
23    eunit:test(Modules, [verbose,{report,{eunit_surefire,[{dir,"../_test"}]}}]),
24    analyze_cover(Modules);
25main(_) ->
26    io:format("usage: run_tests.escript EBIN_DIR~n"),
27    halt(1).
28
29start_cover(Modules) ->
30    {ok, _Cover} = cover:start(),
31    io:format("Cover compiling...~n"),
32    Compiled = [ M || {ok, M} <- [ cover:compile(
33                                     M,
34                                     [{i, "include"}
35                                     ])
36                                   || M <- Modules ] ],
37    case length(Modules) == length(Compiled) of
38        true -> ok;
39        false ->
40            io:format("Warning: the following modules were not"
41                      " cover-compiled:~n   ~p~n", [Compiled])
42    end.
43
44analyze_cover(Modules) ->
45    io:format("Analyzing cover...~n"),
46    CoverBase = filename:join(["..", "_test", "cover"]),
47    ok = filelib:ensure_dir(filename:join([CoverBase, "fake"])),
48    Coverages = lists:foldl(
49                  fun(M, Acc) ->
50                          [analyze_module(CoverBase, M)|Acc]
51                  end,
52                  [], Modules),
53    IndexFilename = filename:join([CoverBase, "index.html"]),
54    {ok, Index} = file:open(IndexFilename, [write]),
55    {LineTotal, CoverTotal} =
56        lists:foldl(fun({_,_,Lines,Covered}, {LineAcc, CovAcc}) ->
57                            {LineAcc+Lines, CovAcc+Covered}
58                    end, {0,0}, Coverages),
59    file:write(Index,
60               "<html><head><title>Coverage</title></head>\n"
61               "<body><h1>Coverage</h1><ul>\n"),
62    file:write(Index,
63               io_lib:format("<h2>Total: ~.2f%</h2>\n",
64                             [percentage(CoverTotal, LineTotal)])),
65    [ file:write(Index,
66                 io_lib:format(
67                   "<li><a href=\"~s\">~p</a>: ~.2f%</li>~n",
68                   [Filename, Module, percentage(Covered, Lines)]))
69      || {Filename, Module, Lines, Covered} <- Coverages ],
70    file:write(Index,"</ul></body></html>"),
71    file:close(Index),
72    io:format("Cover analysis in ~s~n", [IndexFilename]).
73
74analyze_module(CoverBase, Module) ->
75    {ok, Filename} =
76        cover:analyze_to_file(
77          Module,
78          filename:join(CoverBase, atom_to_list(Module)++".COVER.html"),
79          [html]),
80    Lines = count_lines(Filename, "[[:digit:]]\.\.|"),
81    Covered = count_lines(Filename, "[[:space:]]0\.\.|"),
82    {filename:basename(Filename), Module, Lines, Lines-Covered}.
83
84count_lines(Filename, Pattern) ->
85    {ok, [Lines],_} = io_lib:fread(
86                        "~d",
87                        os:cmd(io_lib:format("grep -e \"~s\" ~s | wc -l",
88                                             [Pattern, Filename]))),
89    Lines.
90
91percentage(_, 0) -> 1000.0;
92percentage(Part, Total) ->
93    (Part/Total)*100.
94