cover.mk 3.8 KB

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