From 24beec0a19f9e31ff186c7dbb0f95b3675ff4384 Mon Sep 17 00:00:00 2001
From: Stanislav Ovchar <ovcharsa@elcomplus.ru>
Date: Wed, 15 Mar 2017 13:02:12 +0300
Subject: Accumulate eunit failures in multi-apps

---
 plugins/eunit.mk     |  4 +++-
 test/plugin_eunit.mk | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/plugins/eunit.mk b/plugins/eunit.mk
index 892e01e..9739e0e 100644
--- a/plugins/eunit.mk
+++ b/plugins/eunit.mk
@@ -63,6 +63,8 @@ eunit: test-build $(if $(IS_APP),,apps-eunit)
 
 ifneq ($(ALL_APPS_DIRS),)
 apps-eunit:
-	$(verbose) for app in $(ALL_APPS_DIRS); do $(MAKE) -C $$app eunit IS_APP=1; done
+	$(verbose) eunit_retcode=0 ; for app in $(ALL_APPS_DIRS); do $(MAKE) -C $$app eunit IS_APP=1; \
+		[ $$? -ne 0 ] && eunit_retcode=1 ; done ; \
+		exit $$eunit_retcode
 endif
 endif
diff --git a/test/plugin_eunit.mk b/test/plugin_eunit.mk
index ed002a5..900b45e 100644
--- a/test/plugin_eunit.mk
+++ b/test/plugin_eunit.mk
@@ -1,6 +1,6 @@
 # EUnit plugin.
 
-EUNIT_CASES = all apps-only check erl-opts fun mod priv test-dir tests
+EUNIT_CASES = all apps-only check erl-opts fun mod priv test-dir tests multiapps-no-root-check-exit-code
 EUNIT_TARGETS = $(addprefix eunit-,$(EUNIT_CASES))
 
 .PHONY: eunit $(EUNIT_TARGETS)
@@ -79,6 +79,69 @@ eunit-apps-only: build clean
 	$i "Check that EUnit runs tests"
 	$t $(MAKE) -C $(APP) eunit | grep -q "Test passed."
 
+	$i "Check that EUnit runs tests"
+
+eunit-multiapps-no-root-check-exit-code: build clean
+
+		$i "Create a multi application repository with no root application"
+		$t mkdir $(APP)/
+		$t cp ../erlang.mk $(APP)/
+		$t echo "include erlang.mk" > $(APP)/Makefile
+
+		$i "Create a new application named my_app1"
+		$t $(MAKE) -C $(APP) new-app in=my_app1 $v
+
+		$i "Create a new application named my_app2"
+		$t $(MAKE) -C $(APP) new-app in=my_app2 $v
+
+		$i "Create a new library named my_lib"
+		$t $(MAKE) -C $(APP) new-lib in=my_lib $v
+
+		$i "Check that EUnit detects no tests"
+		$t $(MAKE) -C $(APP) eunit | grep -q "There were no tests to run."
+
+		$i "Generate a module containing broken EUnit tests in my_app1"
+		$t printf "%s\n" \
+			"-module(my_app1)." \
+			"-ifdef(TEST)." \
+			"-include_lib(\"eunit/include/eunit.hrl\")." \
+			"bad_test() -> ?assert(0 =:= 1)." \
+			"-endif." > $(APP)/apps/my_app1/src/my_app1.erl
+
+		$i "Generate a module containing EUnit good tests in my_app2"
+		$t printf "%s\n" \
+			"-module(my_app2)." \
+			"-ifdef(TEST)." \
+			"-include_lib(\"eunit/include/eunit.hrl\")." \
+			"ok_test() -> ok." \
+			"-endif." > $(APP)/apps/my_app2/src/my_app2.erl
+
+		$i "Generate a module containing EUnit tests in my_lib"
+		$t printf "%s\n" \
+			"-module(my_lib)." \
+			"-ifdef(TEST)." \
+			"-include_lib(\"eunit/include/eunit.hrl\")." \
+			"ok_test() -> ok." \
+			"-endif." > $(APP)/apps/my_lib/src/my_lib.erl
+
+		$i "Check exit code of EUnit for the module with broken test should be non-zero"
+		$t ( set +e; $(MAKE) -C $(APP)/apps/my_app1 eunit ; if [ $$? -ne 0 ] ; then \
+			echo "Test passed exit-code is non-zero" ; \
+		else \
+			echo "Test failed exit-code is zero" ; \
+			exit 100; \
+		fi )
+
+		$i "Check exit code of EUnit for the whole project with broken test should be non-zero"
+		$t ( set +e; $(MAKE) -C $(APP) eunit ; if [ $$? -ne 0 ] ; then \
+			echo "Test passed exit-code is non-zero" ; \
+		else \
+			echo "Test failed exit-code is zero" ; \
+			exit 100; \
+		fi )
+
+
+
 eunit-check: build clean
 
 	$i "Bootstrap a new OTP application named $(APP)"
-- 
cgit v1.2.3