cover.mk 4.2 KB

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