cover.mk 4.0 KB

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