scripts/unit-test: Only export coverage data for coverage checking

Running binaries built with b_coverage=true will always output coverage
data so it can be collected later. We don't want our memcheck and
sanitizer code to run over these code paths. Only enable them for the
final coverage run.

Change-Id: I192089623f2b01ba17beea6ba14bc433a13a6497
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index 81ad017..6468bf1 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -405,7 +405,6 @@
         meson_options = parse_meson_options("meson_options.txt")
         meson_flags = [
             '-Db_colorout=never',
-            '-Db_coverage=' + str(build_for_testing).lower(),
         ]
         if build_for_testing:
             meson_flags.append('--buildtype=debug')
@@ -716,11 +715,18 @@
         # Run package unit tests
         build_and_install(UNIT_TEST_PKG, True)
         if os.path.isfile(CODE_SCAN_DIR + '/meson.build'):
-            check_call_cmd(top_dir, 'meson', 'test', '-C', 'build')
-            check_call_cmd(top_dir, 'ninja', '-C', 'build', 'coverage-html')
+            # Run valgrind if it is supported
             if is_valgrind_safe():
                 check_call_cmd(top_dir, 'meson', 'test', '-C', 'build',
                                '--wrap', 'valgrind')
+
+            # Run coverage checks
+            check_call_cmd(top_dir, 'meson', 'configure', 'build',
+                           '-Db_coverage=true')
+            check_call_cmd(top_dir, 'meson', 'test', '-C', 'build')
+            check_call_cmd(top_dir, 'ninja', '-C', 'build', 'coverage-html')
+            check_call_cmd(top_dir, 'meson', 'configure', 'build',
+                           '-Db_coverage=false')
         else:
             run_unit_tests(top_dir)
             maybe_run_valgrind(top_dir)