Joseph Dunne 4 лет назад
Родитель
Сommit
57798d7dc3
3 измененных файлов с 82 добавлено и 9 удалено
  1. 11 0
      doc/src/guide/coverage.asciidoc
  2. 15 9
      plugins/cover.mk
  3. 56 0
      test/plugin_cover.mk

+ 11 - 0
doc/src/guide/coverage.asciidoc

@@ -46,6 +46,17 @@ some applications by defining the `COVER_APPS` variable:
 [source,make]
 COVER_APPS = presence backend
 
+=== Removing modules from the cover report
+
+By default Erlang.mk will include all modules in the
+cover report.
+
+To exclude some modules from the report, you can
+define the `COVER_EXCLUDE_MODS` variable:
+
+[source,make]
+COVER_EXCLUDE_MODS = cowboy_app cowboy_sup
+
 === Configuring paths
 
 By default Erlang.mk will store 'coverdata' files and

+ 15 - 9
plugins/cover.mk

@@ -8,6 +8,7 @@ COVER_DATA_DIR ?= $(COVER_REPORT_DIR)
 ifdef COVER
 COVER_APPS ?= $(notdir $(ALL_APPS_DIRS))
 COVER_DEPS ?=
+COVER_EXCLUDE_MODS ?=
 endif
 
 # Code coverage for Common Test.
@@ -23,7 +24,8 @@ $(TEST_DIR)/ct.cover.spec: cover-data-dir
 		"{incl_dirs, '$(PROJECT)', [\"$(call core_native_path,$(CURDIR)/ebin)\" \
 			$(foreach a,$(COVER_APPS),$(comma) \"$(call core_native_path,$(APPS_DIR)/$a/ebin)\") \
 			$(foreach d,$(COVER_DEPS),$(comma) \"$(call core_native_path,$(DEPS_DIR)/$d/ebin)\")]}." \
-		'{export,"$(call core_native_path,$(abspath $(COVER_DATA_DIR))/ct.coverdata)"}.' > $@
+		'{export,"$(call core_native_path,$(abspath $(COVER_DATA_DIR))/ct.coverdata)"}.' \
+		"{excl_mods, '$(PROJECT)', [$(call comma_list,$(COVER_EXCLUDE_MODS))]}." > $@
 
 CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
 endif
@@ -38,14 +40,18 @@ define cover.erl
 		Dirs = ["$(call core_native_path,$(CURDIR)/ebin)"
 			$(foreach a,$(COVER_APPS),$(comma) "$(call core_native_path,$(APPS_DIR)/$a/ebin)")
 			$(foreach d,$(COVER_DEPS),$(comma) "$(call core_native_path,$(DEPS_DIR)/$d/ebin)")],
-		[begin
-			case filelib:is_dir(Dir) of
-				false -> false;
-				true ->
-					case cover:compile_beam_directory(Dir) of
-						{error, _} -> halt(1);
-						_ -> true
-					end
+		Excludes = [$(call comma_list,$(foreach e,$(COVER_EXCLUDE_MODS),"$e"))],
+		[case file:list_dir(Dir) of
+			{error, enotdir} -> false;
+			{error, _} ->	halt(2);
+			{ok, Files} ->
+			BeamFiles =  [filename:join(Dir, File) ||
+				File <- Files,
+				not lists:member(filename:basename(File, ".beam"), Excludes),
+				filename:extension(File) =:= ".beam"],
+			case cover:compile_beam(BeamFiles) of
+				{error, _} -> halt(1);
+				_ -> true
 			end
 		end || Dir <- Dirs]
 	end,

+ 56 - 0
test/plugin_cover.mk

@@ -33,6 +33,34 @@ cover-ct: init
 	$t test ! -e $(APP)/cover/ct.coverdata
 	$t test ! -e $(APP)/test/ct.cover.spec
 
+cover-ct-excl-mods: init
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
+
+	$i "Add supervisor module to the cover exclude module list "
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "COVER_EXCLUDE_MODS = $(APP)_sup  \n"}' $(APP)/Makefile
+
+	$i "Generate a Common Test suite"
+	$t mkdir $(APP)/test
+	$t printf "%s\n" \
+		"-module($(APP)_SUITE)." \
+		"-export([all/0, ok/1])." \
+		"all() -> [ok]." \
+		"ok(_) -> application:start($(APP))." > $(APP)/test/$(APP)_SUITE.erl
+
+	$i "Run Common Test with code coverage enabled"
+	$t $(MAKE) -C $(APP) ct COVER=1 $v
+
+	$i "Check that the generated files exist"
+	$t test -f $(APP)/cover/ct.coverdata
+	$t test -f $(APP)/test/ct.cover.spec
+
+	$i "Check that the supervisor module is not included in the cover report"
+	$t ! test -e $(APP)/logs/ct_run.*/$(APP)_sup.COVER.html
+
 cover-ct-incl-apps: init
 
 	$i "Bootstrap a new OTP application named $(APP)"
@@ -285,6 +313,34 @@ cover-eunit-apps-only: init
 	$i "Check that the generated file exists"
 	$t test -f $(APP)/apps/my_app/cover/eunit.coverdata
 
+cover-eunit-excl-mods: init
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap $v
+
+	$i "Add supervisor module to the cover exclude module list "
+	$t perl -ni.bak -e 'print;if ($$.==1) {print "COVER_EXCLUDE_MODS = $(APP)_sup  \n"}' $(APP)/Makefile
+
+	$i "Generate a module containing EUnit tests"
+	$t printf "%s\n" \
+		"-module($(APP))." \
+		"-ifdef(TEST)." \
+		"-include_lib(\"eunit/include/eunit.hrl\")." \
+		"ok_test() -> application:ensure_all_started($(APP))." \
+		"-endif." > $(APP)/src/$(APP).erl
+
+	$i "Run EUnit with code coverage enabled"
+	$t $(MAKE) -C $(APP) eunit COVER=1 $v
+
+	$i "Build the cover report"
+	$t $(MAKE) -C $(APP) cover-report $v
+
+	$i "Check that app was covered, but supervisor wasn't"
+	$t test -f $(APP)/cover/$(APP)_app.COVER.html
+	$t ! test -e $(APP)/cover/$(APP)_sup.COVER.html
+
 cover-eunit-incl-apps: init
 
 	$i "Bootstrap a new OTP application named $(APP)"