From: Mark Syms <mark.syms@citrix.com>
Enable coverage for mockatests

Need build system changes to control creation of coverage data for production
code.

Signed-off-by: Mark Syms <mark.syms@citrix.com>
Reviewed-by: Chandrika Srinivasan <chandrika.srinivasan@citrix.com>

diff --git a/cbt/Makefile.am b/cbt/Makefile.am
index bbc4d50..e0b7833 100644
--- a/cbt/Makefile.am
+++ b/cbt/Makefile.am
@@ -1,5 +1,8 @@
 AM_CFLAGS  = -Wall
 AM_CFLAGS += -Werror
+# Need this to be conditionalised
+#AM_CFLAGS += -fprofile-arcs
+#AM_CFLAGS += -ftest-coverage
 
 AM_CPPFLAGS = -I$(top_srcdir)/include
 
diff --git a/mockatests/cbt/Makefile.am b/mockatests/cbt/Makefile.am
index 6b1e52d..fa8b3a8 100644
--- a/mockatests/cbt/Makefile.am
+++ b/mockatests/cbt/Makefile.am
@@ -1,8 +1,7 @@
 AM_CFLAGS  = -Wall
 AM_CFLAGS += -Werror
-# Arc coverage and wrap don't seem to be compatible
-#AM_CFLAGS += -fprofile-arcs
-#AM_CFLAGS += -ftest-coverage
+AM_CFLAGS += -fprofile-arcs
+AM_CFLAGS += -ftest-coverage
 
 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/cbt
 
diff --git a/mockatests/cbt/test-cbt-util.c b/mockatests/cbt/test-cbt-util.c
index 8d385aa..4140dd6 100644
--- a/mockatests/cbt/test-cbt-util.c
+++ b/mockatests/cbt/test-cbt-util.c
@@ -20,8 +20,10 @@ struct cbt_log_metadata {
 	int    consistent;
 };
 
+static int tests_running = 1;
+
 FILE *
-__wrap_fopen(void)
+__wrap_fopen(const char *path, const char *mode)
 {
 	return (FILE*) mock();
 }
@@ -31,7 +33,9 @@ void __real_fclose(FILE *fp);
 void
 __wrap_fclose(FILE *fp)
 {
-	check_expected_ptr(fp);
+	if (tests_running) {
+		check_expected_ptr(fp);
+	}
 	__real_fclose(fp);
 }
 
@@ -142,7 +146,12 @@ const struct CMUnitTest cbt_get_tests[] = {
 
 int main(void)
 {
-	return
+	int result =
 		cmocka_run_group_tests_name("Command tests", cbt_command_tests, NULL, NULL) +
 		cmocka_run_group_tests_name("Get tests", cbt_get_tests, NULL, NULL);
+
+	/* Need to flag that the tests are done so that the fclose mock goes quiescent */
+	tests_running = 0;
+
+	return result;
 }
