#!/usr/bin/env escript %% -*- erlang -*- %%! -name mochiweb__test@127.0.0.1 main([Ebin]) -> code:add_path(Ebin), code:add_paths(filelib:wildcard("../deps/*/ebin", Ebin)), code:add_paths(filelib:wildcard("../deps/*/deps/*/ebin", Ebin)), ModuleNames = [hd(string:tokens(M, ".")) || "../src/" ++ M <- filelib:wildcard("../src/*.erl")], {ok, NonTestRe} = re:compile("_tests$"), Modules = [list_to_atom(M) || M <- lists:filter( fun(M) -> nomatch == re:run(M, NonTestRe) end, ModuleNames)], crypto:start(), start_cover(Modules), eunit:test(Modules, [verbose,{report,{eunit_surefire,[{dir,"../_test"}]}}]), analyze_cover(Modules); main(_) -> io:format("usage: run_tests.escript EBIN_DIR~n"), halt(1). start_cover(Modules) -> {ok, _Cover} = cover:start(), io:format("Cover compiling...~n"), Compiled = [ M || {ok, M} <- [ cover:compile( M, [{i, "include"} ]) || M <- Modules ] ], case length(Modules) == length(Compiled) of true -> ok; false -> io:format("Warning: the following modules were not" " cover-compiled:~n ~p~n", [Compiled]) end. analyze_cover(Modules) -> io:format("Analyzing cover...~n"), CoverBase = filename:join(["..", "_test", "cover"]), ok = filelib:ensure_dir(filename:join([CoverBase, "fake"])), Coverages = lists:foldl( fun(M, Acc) -> [analyze_module(CoverBase, M)|Acc] end, [], Modules), IndexFilename = filename:join([CoverBase, "index.html"]), {ok, Index} = file:open(IndexFilename, [write]), {LineTotal, CoverTotal} = lists:foldl(fun({_,_,Lines,Covered}, {LineAcc, CovAcc}) -> {LineAcc+Lines, CovAcc+Covered} end, {0,0}, Coverages), file:write(Index, "