Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

442 righe
16 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. -module(rebar_prv_cover).
  4. -behaviour(provider).
  5. -export([init/1,
  6. do/1,
  7. maybe_cover_compile/1,
  8. maybe_cover_compile/2,
  9. maybe_write_coverdata/2,
  10. format_error/1]).
  11. -include_lib("providers/include/providers.hrl").
  12. -include("rebar.hrl").
  13. -define(PROVIDER, cover).
  14. -define(DEPS, [lock]).
  15. %% ===================================================================
  16. %% Public API
  17. %% ===================================================================
  18. -spec init(rebar_state:t()) -> {ok, rebar_state:t()}.
  19. init(State) ->
  20. State1 = rebar_state:add_provider(State, providers:create([{name, ?PROVIDER},
  21. {module, ?MODULE},
  22. {bare, true},
  23. {deps, ?DEPS},
  24. {example, "rebar3 cover"},
  25. {short_desc, "Perform coverage analysis."},
  26. {desc, "Perform coverage analysis."},
  27. {opts, cover_opts(State)},
  28. {profiles, [test]}])),
  29. {ok, State1}.
  30. -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
  31. do(State) ->
  32. {Opts, _} = rebar_state:command_parsed_args(State),
  33. case proplists:get_value(reset, Opts, false) of
  34. true -> reset(State);
  35. false -> analyze(State)
  36. end.
  37. -spec maybe_cover_compile(rebar_state:t()) -> ok.
  38. maybe_cover_compile(State) ->
  39. maybe_cover_compile(State, apps).
  40. -spec maybe_cover_compile(rebar_state:t(), [file:name()] | apps) -> ok.
  41. maybe_cover_compile(State, Dirs) ->
  42. case rebar_state:get(State, cover_enabled, false) of
  43. true -> cover_compile(State, Dirs);
  44. false -> ok
  45. end.
  46. -spec maybe_write_coverdata(rebar_state:t(), atom()) -> ok.
  47. maybe_write_coverdata(State, Task) ->
  48. case cover:modules() of
  49. %% no coverdata collected, skip writing anything out
  50. [] -> ok;
  51. _ -> write_coverdata(State, Task)
  52. end.
  53. -spec format_error(any()) -> iolist().
  54. format_error({min_coverage_failed, {PassRate, Total}}) ->
  55. io_lib:format("Requiring ~p% coverage to pass. Only ~p% obtained",
  56. [PassRate, Total]);
  57. format_error(Reason) ->
  58. io_lib:format("~p", [Reason]).
  59. %% ===================================================================
  60. %% Internal functions
  61. %% ===================================================================
  62. reset(State) ->
  63. ?INFO("Resetting collected cover data...", []),
  64. CoverDir = cover_dir(State),
  65. CoverFiles = get_all_coverdata(CoverDir),
  66. F = fun(File) ->
  67. case file:delete(File) of
  68. {error, Reason} ->
  69. ?WARN("Error deleting ~p: ~p", [Reason, File]);
  70. _ -> ok
  71. end
  72. end,
  73. ok = lists:foreach(F, CoverFiles),
  74. {ok, State}.
  75. analyze(State) ->
  76. %% modules have to be compiled and then cover compiled
  77. %% in order for cover data to be reloaded
  78. %% this maybe breaks if modules have been deleted
  79. %% since code coverage was collected?
  80. {ok, S} = rebar_prv_compile:do(State),
  81. ok = cover_compile(S, apps),
  82. do_analyze(State).
  83. do_analyze(State) ->
  84. ?INFO("Performing cover analysis...", []),
  85. %% figure out what coverdata we have
  86. CoverDir = cover_dir(State),
  87. CoverFiles = get_all_coverdata(CoverDir),
  88. %% start the cover server if necessary
  89. {ok, CoverPid} = start_cover(),
  90. %% redirect cover output
  91. true = redirect_cover_output(State, CoverPid),
  92. %% analyze!
  93. case analyze(State, CoverFiles) of
  94. [] -> {ok, State};
  95. Analysis ->
  96. print_analysis(Analysis, verbose(State)),
  97. write_index(State, Analysis),
  98. maybe_fail_coverage(Analysis, State)
  99. end.
  100. get_all_coverdata(CoverDir) ->
  101. ok = filelib:ensure_dir(filename:join([CoverDir, "dummy.log"])),
  102. {ok, Files} = rebar_utils:list_dir(CoverDir),
  103. rebar_utils:filtermap(fun(FileName) ->
  104. case filename:extension(FileName) == ".coverdata" of
  105. true -> {true, filename:join([CoverDir, FileName])};
  106. false -> false
  107. end
  108. end, Files).
  109. analyze(_State, []) ->
  110. ?WARN("No coverdata found", []),
  111. [];
  112. analyze(State, CoverFiles) ->
  113. %% reset any existing cover data
  114. ok = cover:reset(),
  115. %% import all coverdata files
  116. ok = lists:foreach(fun(M) -> import(M) end, CoverFiles),
  117. [{"aggregate", CoverFiles, analysis(State, "aggregate")}] ++
  118. analyze(State, CoverFiles, []).
  119. analyze(_State, [], Acc) -> lists:reverse(Acc);
  120. analyze(State, [F|Rest], Acc) ->
  121. %% reset any existing cover data
  122. ok = cover:reset(),
  123. %% extract taskname from the CoverData file
  124. Task = filename:basename(F, ".coverdata"),
  125. %% import task cover data and process it
  126. ok = import(F),
  127. analyze(State, Rest, [{Task, [F], analysis(State, Task)}] ++ Acc).
  128. import(CoverData) ->
  129. case cover:import(CoverData) of
  130. {error, {cant_open_file, F, _Reason}} ->
  131. ?WARN("Can't import cover data from ~ts.", [F]),
  132. error;
  133. ok -> ok
  134. end.
  135. analysis(State, Task) ->
  136. OldPath = code:get_path(),
  137. ok = restore_cover_paths(State),
  138. Mods = cover:imported_modules(),
  139. Analysis = lists:map(fun(Mod) ->
  140. {ok, Answer} = cover:analyze(Mod, coverage, line),
  141. {ok, File} = analyze_to_file(Mod, State, Task),
  142. {Mod, process(Answer), File}
  143. end,
  144. Mods),
  145. true = rebar_utils:cleanup_code_path(OldPath),
  146. lists:sort(Analysis).
  147. restore_cover_paths(State) ->
  148. lists:foreach(fun(App) ->
  149. AppDir = rebar_app_info:out_dir(App),
  150. _ = code:add_path(filename:join([AppDir, "ebin"])),
  151. _ = code:add_path(filename:join([AppDir, "test"]))
  152. end, rebar_state:project_apps(State)),
  153. _ = code:add_path(filename:join([rebar_dir:base_dir(State), "test"])),
  154. ok.
  155. analyze_to_file(Mod, State, Task) ->
  156. CoverDir = cover_dir(State),
  157. TaskDir = filename:join([CoverDir, Task]),
  158. ok = filelib:ensure_dir(filename:join([TaskDir, "dummy.html"])),
  159. case code:ensure_loaded(Mod) of
  160. {module, _} ->
  161. write_file(Mod, mod_to_filename(TaskDir, Mod));
  162. {error, _} ->
  163. ?WARN("Can't load module ~ts.", [Mod]),
  164. {ok, []}
  165. end.
  166. write_file(Mod, FileName) ->
  167. case cover:analyze_to_file(Mod, FileName, [html]) of
  168. {ok, File} -> {ok, File};
  169. {error, Reason} ->
  170. ?WARN("Couldn't write annotated file for module ~p for reason ~p", [Mod, Reason]),
  171. {ok, []}
  172. end.
  173. mod_to_filename(TaskDir, M) ->
  174. filename:join([TaskDir, atom_to_list(M) ++ ".html"]).
  175. process(Coverage) -> process(Coverage, {0, 0}).
  176. process([], Acc) -> Acc;
  177. %% line 0 is a line added by eunit and never executed so ignore it
  178. process([{{_, 0}, _}|Rest], Acc) -> process(Rest, Acc);
  179. process([{_, {Cov, Not}}|Rest], {Covered, NotCovered}) ->
  180. process(Rest, {Covered + Cov, NotCovered + Not}).
  181. print_analysis(_, false) -> ok;
  182. print_analysis(Analysis, true) ->
  183. {_, CoverFiles, Stats} = lists:keyfind("aggregate", 1, Analysis),
  184. Table = format_table(Stats, CoverFiles),
  185. io:format("~ts", [Table]).
  186. format_table(Stats, CoverFiles) ->
  187. MaxLength = lists:max([20 | lists:map(fun({M, _, _}) -> mod_length(M) end, Stats)]),
  188. Header = header(MaxLength),
  189. Separator = separator(MaxLength),
  190. TotalLabel = format("total", MaxLength),
  191. TotalCov = format(calculate_total_string(Stats), 8),
  192. [io_lib:format("~ts~n~ts~n~ts~n", [Separator, Header, Separator]),
  193. lists:map(fun({Mod, Coverage, _}) ->
  194. Name = format(Mod, MaxLength),
  195. Cov = format(percentage_string(Coverage), 8),
  196. io_lib:format(" | ~ts | ~ts |~n", [Name, Cov])
  197. end, Stats),
  198. io_lib:format("~ts~n", [Separator]),
  199. io_lib:format(" | ~ts | ~ts |~n", [TotalLabel, TotalCov]),
  200. io_lib:format("~ts~n", [Separator]),
  201. io_lib:format(" coverage calculated from:~n", []),
  202. lists:map(fun(File) ->
  203. io_lib:format(" ~ts~n", [File])
  204. end, CoverFiles)].
  205. mod_length(Mod) when is_atom(Mod) -> mod_length(atom_to_list(Mod));
  206. mod_length(Mod) -> length(Mod).
  207. header(Width) ->
  208. [" | ", format("module", Width), " | ", format("coverage", 8), " |"].
  209. separator(Width) ->
  210. [" |--", io_lib:format("~*c", [Width, $-]), "--|------------|"].
  211. format(String, Width) -> io_lib:format("~*.ts", [Width, String]).
  212. calculate_total_string(Stats) ->
  213. integer_to_list(calculate_total(Stats))++"%".
  214. calculate_total(Stats) ->
  215. percentage(lists:foldl(
  216. fun({_Mod, {Cov, Not}, _File}, {CovAcc, NotAcc}) ->
  217. {CovAcc + Cov, NotAcc + Not}
  218. end,
  219. {0, 0},
  220. Stats
  221. )).
  222. percentage_string(Data) -> integer_to_list(percentage(Data))++"%".
  223. percentage({_, 0}) -> 100;
  224. percentage({Cov, Not}) -> trunc((Cov / (Cov + Not)) * 100).
  225. write_index(State, Coverage) ->
  226. CoverDir = cover_dir(State),
  227. FileName = filename:join([CoverDir, "index.html"]),
  228. {ok, F} = file:open(FileName, [write]),
  229. ok = file:write(F, "<!DOCTYPE HTML><html>\n"
  230. "<head><meta charset=\"utf-8\">"
  231. "<title>Coverage Summary</title></head>\n"
  232. "<body>\n"),
  233. {Aggregate, Rest} = lists:partition(fun({"aggregate", _, _}) -> true; (_) -> false end,
  234. Coverage),
  235. ok = write_index_section(F, Aggregate),
  236. ok = write_index_section(F, Rest),
  237. ok = file:write(F, "</body></html>"),
  238. ok = file:close(F),
  239. io:format(" cover summary written to: ~ts~n", [filename:absname(FileName)]).
  240. write_index_section(_F, []) -> ok;
  241. write_index_section(F, [{Section, DataFile, Mods}|Rest]) ->
  242. %% Write the report
  243. ok = file:write(F, ?FMT("<h1>~ts summary</h1>\n", [Section])),
  244. ok = file:write(F, "coverage calculated from:\n<ul>"),
  245. ok = lists:foreach(fun(D) -> ok = file:write(F, io_lib:format("<li>~ts</li>", [D])) end,
  246. DataFile),
  247. ok = file:write(F, "</ul>\n"),
  248. ok = file:write(F, "<table><tr><th>module</th><th>coverage %</th></tr>\n"),
  249. FmtLink =
  250. fun({Mod, Cov, Report}) ->
  251. ?FMT("<tr><td><a href='~ts'>~ts</a></td><td>~ts</td>\n",
  252. [strip_coverdir(Report), Mod, percentage_string(Cov)])
  253. end,
  254. lists:foreach(fun(M) -> ok = file:write(F, FmtLink(M)) end, Mods),
  255. ok = file:write(F, ?FMT("<tr><td><strong>Total</strong></td><td>~ts</td>\n",
  256. [calculate_total_string(Mods)])),
  257. ok = file:write(F, "</table>\n"),
  258. write_index_section(F, Rest).
  259. maybe_fail_coverage(Analysis, State) ->
  260. {_, _CoverFiles, Stats} = lists:keyfind("aggregate", 1, Analysis),
  261. Total = calculate_total(Stats),
  262. PassRate = min_coverage(State),
  263. ?DEBUG("Comparing ~p to pass rate ~p", [Total, PassRate]),
  264. if Total >= PassRate ->
  265. {ok, State}
  266. ; Total < PassRate ->
  267. ?PRV_ERROR({min_coverage_failed, {PassRate, Total}})
  268. end.
  269. %% fix for r15b which doesn't put the correct path in the `source` section
  270. %% of `module_info(compile)`
  271. strip_coverdir([]) -> "";
  272. strip_coverdir(File) ->
  273. filename:join(lists:reverse(lists:sublist(lists:reverse(filename:split(File)),
  274. 2))).
  275. cover_compile(State, apps) ->
  276. ExclApps = [rebar_utils:to_binary(A) || A <- rebar_state:get(State, cover_excl_apps, [])],
  277. Apps = filter_checkouts_and_excluded(rebar_state:project_apps(State), ExclApps),
  278. AppDirs = app_dirs(Apps),
  279. cover_compile(State, lists:filter(fun(D) -> ec_file:is_dir(D) end, AppDirs));
  280. cover_compile(State, Dirs) ->
  281. rebar_paths:set_paths([deps], State),
  282. %% start the cover server if necessary
  283. {ok, CoverPid} = start_cover(),
  284. %% redirect cover output
  285. true = redirect_cover_output(State, CoverPid),
  286. ExclMods = rebar_state:get(State, cover_excl_mods, []),
  287. lists:foreach(fun(Dir) ->
  288. case file:list_dir(Dir) of
  289. {ok, Files} ->
  290. ?DEBUG("cover compiling ~p", [Dir]),
  291. [cover_compile_file(filename:join(Dir, File))
  292. || File <- Files,
  293. filename:extension(File) == ".beam",
  294. not is_ignored(Dir, File, ExclMods)],
  295. ok;
  296. {error, eacces} ->
  297. ?WARN("Directory ~p not readable, modules will not be included in coverage", [Dir]);
  298. {error, enoent} ->
  299. ?WARN("Directory ~p not found", [Dir]);
  300. {error, Reason} ->
  301. ?WARN("Directory ~p error ~p", [Dir, Reason])
  302. end
  303. end, Dirs),
  304. ok.
  305. is_ignored(Dir, File, ExclMods) ->
  306. Ignored = lists:any(fun(Excl) ->
  307. File =:= atom_to_list(Excl) ++ ".beam"
  308. end,
  309. ExclMods),
  310. Ignored andalso ?DEBUG("cover ignoring ~p ~p", [Dir, File]),
  311. Ignored.
  312. cover_compile_file(FileName) ->
  313. case catch(cover:compile_beam(FileName)) of
  314. {error, Reason} ->
  315. ?WARN("Cover compilation failed: ~p", [Reason]);
  316. {ok, _} ->
  317. ok
  318. end.
  319. app_dirs(Apps) ->
  320. lists:foldl(fun app_ebin_dirs/2, [], Apps).
  321. app_ebin_dirs(App, Acc) ->
  322. [rebar_app_info:ebin_dir(App)|Acc].
  323. filter_checkouts_and_excluded(Apps, ExclApps) ->
  324. filter_checkouts_and_excluded(Apps, ExclApps, []).
  325. filter_checkouts_and_excluded([], _ExclApps, Acc) -> lists:reverse(Acc);
  326. filter_checkouts_and_excluded([App|Rest], ExclApps, Acc) ->
  327. case rebar_app_info:is_checkout(App) orelse lists:member(rebar_app_info:name(App), ExclApps) of
  328. true -> filter_checkouts_and_excluded(Rest, ExclApps, Acc);
  329. false -> filter_checkouts_and_excluded(Rest, ExclApps, [App|Acc])
  330. end.
  331. start_cover() ->
  332. case cover:start() of
  333. {ok, Pid} -> {ok, Pid};
  334. {error, {already_started, Pid}} -> {ok, Pid}
  335. end.
  336. redirect_cover_output(State, CoverPid) ->
  337. %% redirect cover console output to file
  338. DataDir = cover_dir(State),
  339. ok = filelib:ensure_dir(filename:join([DataDir, "dummy.log"])),
  340. {ok, F} = file:open(filename:join([DataDir, "cover.log"]),
  341. [append]),
  342. group_leader(F, CoverPid).
  343. write_coverdata(State, Name) ->
  344. DataDir = cover_dir(State),
  345. ok = filelib:ensure_dir(filename:join([DataDir, "dummy.log"])),
  346. ExportFile = filename:join([DataDir, rebar_utils:to_list(Name) ++ ".coverdata"]),
  347. case cover:export(ExportFile) of
  348. ok ->
  349. %% dump accumulated coverdata after writing
  350. ok = cover:reset(),
  351. ?DEBUG("Cover data written to ~p.", [ExportFile]);
  352. {error, Reason} ->
  353. ?WARN("Cover data export failed: ~p", [Reason])
  354. end.
  355. command_line_opts(State) ->
  356. {Opts, _} = rebar_state:command_parsed_args(State),
  357. Opts.
  358. config_opts(State) ->
  359. rebar_state:get(State, cover_opts, []).
  360. verbose(State) ->
  361. Command = proplists:get_value(verbose, command_line_opts(State), undefined),
  362. Config = proplists:get_value(verbose, config_opts(State), undefined),
  363. case {Command, Config} of
  364. {undefined, undefined} -> false;
  365. {undefined, Verbose} -> Verbose;
  366. {Verbose, _} -> Verbose
  367. end.
  368. min_coverage(State) ->
  369. Command = proplists:get_value(min_coverage, command_line_opts(State), undefined),
  370. Config = proplists:get_value(min_coverage, config_opts(State), undefined),
  371. case {Command, Config} of
  372. {undefined, undefined} -> 0;
  373. {undefined, Rate} -> Rate;
  374. {Rate, _} -> Rate
  375. end.
  376. cover_dir(State) ->
  377. filename:join([rebar_dir:base_dir(State), "cover"]).
  378. cover_opts(_State) ->
  379. [{reset, $r, "reset", boolean, help(reset)},
  380. {verbose, $v, "verbose", boolean, help(verbose)},
  381. {min_coverage, $m, "min_coverage", integer, help(min_coverage)}].
  382. help(reset) -> "Reset all coverdata.";
  383. help(verbose) -> "Print coverage analysis.";
  384. help(min_coverage) -> "Mandate a coverage percentage required to succeed (0..100)".