cover.mk 4.2 KB

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