Browse Source

Convert legacy cover tests and fix a few issues

There are no more legacy tests. Enjoy!
Loïc Hoguin 8 years ago
parent
commit
68bb586e60
3 changed files with 120 additions and 88 deletions
  1. 12 7
      plugins/cover.mk
  2. 0 81
      test/Makefile
  3. 108 0
      test/plugin_cover.mk

+ 12 - 7
plugins/cover.mk

@@ -8,6 +8,7 @@ COVER_REPORT_DIR = cover
 
 ifdef COVER
 ifdef CT_RUN
+ifneq ($(wildcard $(TEST_DIR)),)
 # All modules in 'ebin'
 COVER_MODS = $(notdir $(basename $(call core_ls,ebin/*.beam)))
 
@@ -22,6 +23,7 @@ $(TEST_DIR)/ct.cover.spec:
 CT_RUN += -cover $(TEST_DIR)/ct.cover.spec
 endif
 endif
+endif
 
 # Core targets
 
@@ -43,7 +45,7 @@ help::
 		"Cover targets:" \
 		"  cover-report  Generate a HTML coverage report from previously collected" \
 		"                cover data." \
-		"  all.coverdata Merge {eunit,ct}.coverdata into one coverdata file." \
+		"  all.coverdata Merge all coverdata files into all.coverdata." \
 		"" \
 		"If COVER=1 is set, coverage data is generated by the targets eunit and ct. The" \
 		"target tests additionally generates a HTML coverage report from the combined" \
@@ -56,13 +58,16 @@ COVERDATA = $(filter-out all.coverdata,$(wildcard *.coverdata))
 
 .PHONY: coverdata-clean
 coverdata-clean:
-	$(gen_verbose) rm -f *.coverdata ct.cover.spec
+	$(gen_verbose) rm -f *.coverdata $(TEST_DIR)/ct.cover.spec
 
 # Merge all coverdata files into one.
+define cover_export.erl
+	$(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),)
+	cover:export("$@"), halt(0).
+endef
+
 all.coverdata: $(COVERDATA)
-	$(gen_verbose) $(ERL) -eval ' \
-		$(foreach f,$(COVERDATA),cover:import("$(f)") == ok orelse halt(1),) \
-		cover:export("$@"), halt(0).'
+	$(gen_verbose) $(call erlang,$(cover_export.erl))
 
 # These are only defined if COVER_REPORT_DIR is non-empty. Set COVER_REPORT_DIR to
 # empty if you want the coverdata files but not the HTML report.
@@ -80,7 +85,7 @@ else
 # Modules which include eunit.hrl always contain one line without coverage
 # because eunit defines test/0 which is never called. We compensate for this.
 EUNIT_HRL_MODS = $(subst $(space),$(comma),$(shell \
-	grep -e '^\s*-include.*include/eunit\.hrl"' src/*.erl \
+	grep -H -e '^\s*-include.*include/eunit\.hrl"' src/*.erl \
 	| sed "s/^src\/\(.*\)\.erl:.*/'\1'/" | uniq))
 
 define cover_report.erl
@@ -115,7 +120,7 @@ define cover_report.erl
 endef
 
 cover-report:
-	$(gen_verbose) mkdir -p $(COVER_REPORT_DIR)
+	$(verbose) mkdir -p $(COVER_REPORT_DIR)
 	$(gen_verbose) $(call erlang,$(cover_report.erl))
 
 endif

+ 0 - 81
test/Makefile

@@ -276,84 +276,3 @@ pkg-$1: build clean
 endef
 
 $(foreach pkg,$(PACKAGES),$(eval $(call pkg_target,$(pkg))))
-
-##################
-
-# Test application used for testing.
-app1:
-	$(call app1_setup)
-
-# Extra module in app1 used for testing eunit
-define create-module-t
-printf '%s\n' \
-	'-module(t).' \
-	'-export([succ/1]).' \
-	'succ(N) -> N + 1.' \
-	'-ifdef(TEST).' \
-	'-include_lib("eunit/include/eunit.hrl").' \
-	'succ_test() ->' \
-	'	?assertEqual(2, succ(1)),' \
-	'	os:cmd("echo t >> test-eunit.log").' \
-	'-endif.' \
-	> app1/src/t.erl
-endef
-
-# Legacy tests.
-#
-# The following tests are slowly being converted.
-# Do NOT use -j with legacy tests.
-
-.PHONY: legacy clean-legacy tests-cover
-
-legacy: clean-legacy tests-cover
-
-clean-legacy:
-	$t rm -rf app1
-
-# TODO: do coverage for 'tests' instead of 'eunit ct' when triq is fixed
-tests-cover: app1
-	$i "tests-cover: Testing 'eunit' and 'ct' with COVER=1"
-	$i "Setting up eunit and ct suites."
-	$t $(call create-module-t)
-	$t mkdir -p app1/test
-	$t printf "%s\n" \
-		"-module(m_SUITE)." \
-		"-export([all/0, testcase1/1])." \
-		"all() -> [testcase1]." \
-		"testcase1(_) -> 2 = m:succ(1)." \
-	 > app1/test/m_SUITE.erl
-	$i "Running tests with coverage analysis."
-	$t $(MAKE) -C app1 eunit ct COVER=1 $v
-	$t [ -e app1/test-eunit.log ]
-	$t [ -e app1/eunit.coverdata ]
-	$t [ -e app1/ct.coverdata ]
-	$i "Generating coverage report."
-	$t $(MAKE) -C app1 cover-report COVER=1 $v
-	$t [ -e app1/cover/m.COVER.html ]
-	$t [ -e app1/cover/t.COVER.html ]
-	$t [ -e app1/cover/index.html ]
-	$i "Checking combined coverage from eunit and ct."
-	$t [ `grep 'Total: 100%' app1/cover/index.html | wc -l` -eq 1 ]
-	$i "Checking that cover-report-clean removes cover report."
-	$t $(MAKE) -C app1 cover-report-clean $v
-	$t [ ! -e app1/cover ]
-	$i "Checking that coverdata-clean removes cover data."
-	$t $(MAKE) -C app1 coverdata-clean $v
-	$t [ ! -e app1/eunit.coverdata ]
-	@# clean up
-	$t rm -rf app1/src/t.erl app1/test app1/test-eunit.log
-	$t $(MAKE) -C app1 clean $v
-	$i "Test 'tests-cover' passed."
-
-define app1_setup
-	$i "Setting up app."
-	$t mkdir -p app1
-	$t cd .. && $(MAKE)
-	$t cp ../erlang.mk app1/
-	$t $(MAKE) -C app1 -f erlang.mk bootstrap-lib
-	$t printf "%s\n" \
-		"-module(m)." \
-		"-export([succ/1])." \
-		"succ(N) -> N + 1." \
-		> app1/src/m.erl
-endef

+ 108 - 0
test/plugin_cover.mk

@@ -0,0 +1,108 @@
+# Common Test plugin.
+
+COVER_CASES = ct eunit report-and-merge
+COVER_TARGETS = $(addprefix cover-,$(COVER_CASES))
+
+.PHONY: cover $(COVER_TARGETS)
+
+cover: $(COVER_TARGETS)
+
+cover-ct: build clean
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$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(_) -> ok." > $(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)/ct.coverdata
+	$t test -f $(APP)/test/ct.cover.spec
+
+	$i "Check that the generated files are removed on clean"
+	$t $(MAKE) -C $(APP) clean $v
+	$t test ! -e $(APP)/ct.coverdata
+	$t test ! -e $(APP)/test/ct.cover.spec
+
+cover-eunit: build clean
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$i "Generate a module containing EUnit tests"
+	$t printf "%s\n" \
+		"-module($(APP))." \
+		"-ifdef(TEST)." \
+		"-include_lib(\"eunit/include/eunit.hrl\")." \
+		"ok_test() -> ok." \
+		"-endif." > $(APP)/src/$(APP).erl
+
+	$i "Run EUnit with code coverage enabled"
+	$t $(MAKE) -C $(APP) eunit COVER=1 $v
+
+	$i "Check that the generated file exists"
+	$t test -f $(APP)/eunit.coverdata
+
+	$i "Check that the generated file is removed on clean"
+	$t $(MAKE) -C $(APP) clean $v
+	$t test ! -e $(APP)/eunit.coverdata
+
+cover-report-and-merge: build clean
+
+	$i "Bootstrap a new OTP application named $(APP)"
+	$t mkdir $(APP)/
+	$t cp ../erlang.mk $(APP)/
+	$t $(MAKE) -C $(APP) -f erlang.mk bootstrap-lib $v
+
+	$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(_) -> ok." > $(APP)/test/$(APP)_SUITE.erl
+
+	$i "Generate a module containing EUnit tests"
+	$t printf "%s\n" \
+		"-module($(APP))." \
+		"-ifdef(TEST)." \
+		"-include_lib(\"eunit/include/eunit.hrl\")." \
+		"ok_test() -> ok." \
+		"-endif." > $(APP)/src/$(APP).erl
+
+	$i "Run tests with code coverage enabled"
+	$t $(MAKE) -C $(APP) tests COVER=1 $v
+
+	$i "Check that the generated files exist"
+	$t test -f $(APP)/cover/$(APP).COVER.html
+	$t test -f $(APP)/cover/index.html
+	$t test -f $(APP)/ct.coverdata
+	$t test -f $(APP)/eunit.coverdata
+	$t test -f $(APP)/test/ct.cover.spec
+
+	$i "Merge coverdata files into all.coverdata"
+	$t $(MAKE) -C $(APP) all.coverdata $v
+	$t test -f $(APP)/all.coverdata
+
+	$i "Check that the generated files are removed on clean"
+	$t $(MAKE) -C $(APP) clean $v
+	$t test ! -e $(APP)/all.coverdata
+	$t test ! -e $(APP)/ct.coverdata
+	$t test ! -e $(APP)/eunit.coverdata
+	$t test ! -e $(APP)/test/ct.cover.spec
+
+	$i "Check that the cover report is removed on distclean"
+	$t $(MAKE) -C $(APP) distclean $v
+	$t test ! -e $(APP)/cover/