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