cover.mk 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (c) 2016, Loïc Hoguin <essen@ninenines.eu>
  2. # Copyright (c) 2015, Viktor Söderqvist <viktor@zuiderkwast.se>
  3. # This file is part of erlang.mk and subject to the terms of the ISC License.
  4. COVER_REPORT_DIR = cover
  5. # Hook in coverage to ct
  6. ifdef COVER
  7. ifdef CT_RUN
  8. # All modules in 'ebin'
  9. COVER_MODS = $(notdir $(basename $(call core_ls,ebin/*.beam)))
  10. test-build:: $(TEST_DIR)/ct.cover.spec
  11. $(TEST_DIR)/ct.cover.spec:
  12. $(verbose) echo Cover mods: $(COVER_MODS)
  13. $(gen_verbose) printf "%s\n" \
  14. '{incl_mods,[$(subst $(space),$(comma),$(COVER_MODS))]}.' \
  15. '{export,"$(CURDIR)/ct.coverdata"}.' > $@
  16. CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
  17. endif
  18. endif
  19. # Core targets
  20. ifdef COVER
  21. ifneq ($(COVER_REPORT_DIR),)
  22. tests::
  23. $(verbose) $(MAKE) --no-print-directory cover-report
  24. endif
  25. endif
  26. clean:: coverdata-clean
  27. ifneq ($(COVER_REPORT_DIR),)
  28. distclean:: cover-report-clean
  29. endif
  30. help::
  31. $(verbose) printf "%s\n" "" \
  32. "Cover targets:" \
  33. " cover-report Generate a HTML coverage report from previously collected" \
  34. " cover data." \
  35. " all.coverdata Merge {eunit,ct}.coverdata into one coverdata file." \
  36. "" \
  37. "If COVER=1 is set, coverage data is generated by the targets eunit and ct. The" \
  38. "target tests additionally generates a HTML coverage report from the combined" \
  39. "coverdata files from each of these testing tools. HTML reports can be disabled" \
  40. "by setting COVER_REPORT_DIR to empty."
  41. # Plugin specific targets
  42. COVERDATA = $(filter-out all.coverdata,$(wildcard *.coverdata))
  43. .PHONY: coverdata-clean
  44. coverdata-clean:
  45. $(gen_verbose) rm -f *.coverdata ct.cover.spec
  46. # Merge all coverdata files into one.
  47. all.coverdata: $(COVERDATA)
  48. $(gen_verbose) $(ERL) -eval ' \
  49. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),) \
  50. cover:export("$@"), halt(0).'
  51. # These are only defined if COVER_REPORT_DIR is non-empty. Set COVER_REPORT_DIR to
  52. # empty if you want the coverdata files but not the HTML report.
  53. ifneq ($(COVER_REPORT_DIR),)
  54. .PHONY: cover-report-clean cover-report
  55. cover-report-clean:
  56. $(gen_verbose) rm -rf $(COVER_REPORT_DIR)
  57. ifeq ($(COVERDATA),)
  58. cover-report:
  59. else
  60. # Modules which include eunit.hrl always contain one line without coverage
  61. # because eunit defines test/0 which is never called. We compensate for this.
  62. EUNIT_HRL_MODS = $(subst $(space),$(comma),$(shell \
  63. grep -e '^\s*-include.*include/eunit\.hrl"' src/*.erl \
  64. | sed "s/^src\/\(.*\)\.erl:.*/'\1'/" | uniq))
  65. define cover_report.erl
  66. $(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),)
  67. Ms = cover:imported_modules(),
  68. [cover:analyse_to_file(M, "$(COVER_REPORT_DIR)/" ++ atom_to_list(M)
  69. ++ ".COVER.html", [html]) || M <- Ms],
  70. Report = [begin {ok, R} = cover:analyse(M, module), R end || M <- Ms],
  71. EunitHrlMods = [$(EUNIT_HRL_MODS)],
  72. Report1 = [{M, {Y, case lists:member(M, EunitHrlMods) of
  73. true -> N - 1; false -> N end}} || {M, {Y, N}} <- Report],
  74. TotalY = lists:sum([Y || {_, {Y, _}} <- Report1]),
  75. TotalN = lists:sum([N || {_, {_, N}} <- Report1]),
  76. Perc = fun(Y, N) -> case Y + N of 0 -> 100; S -> round(100 * Y / S) end end,
  77. TotalPerc = Perc(TotalY, TotalN),
  78. {ok, F} = file:open("$(COVER_REPORT_DIR)/index.html", [write]),
  79. io:format(F, "<!DOCTYPE html><html>~n"
  80. "<head><meta charset=\"UTF-8\">~n"
  81. "<title>Coverage report</title></head>~n"
  82. "<body>~n", []),
  83. io:format(F, "<h1>Coverage</h1>~n<p>Total: ~p%</p>~n", [TotalPerc]),
  84. io:format(F, "<table><tr><th>Module</th><th>Coverage</th></tr>~n", []),
  85. [io:format(F, "<tr><td><a href=\"~p.COVER.html\">~p</a></td>"
  86. "<td>~p%</td></tr>~n",
  87. [M, M, Perc(Y, N)]) || {M, {Y, N}} <- Report1],
  88. How = "$(subst $(space),$(comma)$(space),$(basename $(COVERDATA)))",
  89. Date = "$(shell date -u "+%Y-%m-%dT%H:%M:%SZ")",
  90. io:format(F, "</table>~n"
  91. "<p>Generated using ~s and erlang.mk on ~s.</p>~n"
  92. "</body></html>", [How, Date]),
  93. halt().
  94. endef
  95. cover-report:
  96. $(gen_verbose) mkdir -p $(COVER_REPORT_DIR)
  97. $(gen_verbose) $(call erlang,$(cover_report.erl))
  98. endif
  99. endif # ifneq ($(COVER_REPORT_DIR),)