From 8374be46d2da63fa75cf283fc13e5ab9a8133d9a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
Date: Thu, 4 Apr 2019 21:45:31 -0400
Subject: [PATCH] cogl/tests: Export and run unit tests if they are enabled

Cogl unit tests are just functions inside normal code files that needs to be
dload'ed by the test binary.

So in case unit-tests are enabled, we need to export those symbols.
Since map file can't be overridden, use a configure_file to generate the map
file when tests are enabled, in order to export the needed symbols.

Then goes through the source files to look unit tests checking for their macro
definition and load them with the runner script.

https://gitlab.gnome.org/GNOME/mutter/merge_requests/442
---
 ...bmutter-cogl.map => libmutter-cogl.map.in} |  3 +-
 cogl/cogl/meson.build                         | 14 ++++++--
 .../conform/meson/find-conform-unit-tests.sh  |  2 --
 cogl/tests/unit/meson.build                   | 32 +++++++++----------
 cogl/tests/unit/meson/find-unit-tests.sh      |  7 ++--
 5 files changed, 33 insertions(+), 25 deletions(-)
 rename cogl/cogl/{libmutter-cogl.map => libmutter-cogl.map.in} (98%)

diff --git a/cogl/cogl/libmutter-cogl.map b/cogl/cogl/libmutter-cogl.map.in
similarity index 98%
rename from cogl/cogl/libmutter-cogl.map
rename to cogl/cogl/libmutter-cogl.map.in
index 9f2f16ba0..4e31a157b 100644
--- a/cogl/cogl/libmutter-cogl.map
+++ b/cogl/cogl/libmutter-cogl.map.in
@@ -29,8 +29,6 @@ global:
   _cogl_texture_can_hardware_repeat;
   _cogl_pipeline_prune_to_n_layers;
   _cogl_primitive_draw;
-  #test_;
-  #unit_test_;
   _cogl_winsys_glx_get_vtable;
   _cogl_winsys_egl_xlib_get_vtable;
   _cogl_winsys_egl_get_vtable;
@@ -48,6 +46,7 @@ global:
   _cogl_pixel_format_get_bytes_per_pixel*;
   _cogl_system_error_quark;
   _cogl_util_next_p2;
+  @unit_tests_symbols@
 local:
   *;
 };
diff --git a/cogl/cogl/meson.build b/cogl/cogl/meson.build
index 38ac9a10c..527c50528 100644
--- a/cogl/cogl/meson.build
+++ b/cogl/cogl/meson.build
@@ -455,16 +455,24 @@ cogl_headers_all = [
 ]
 
 cogl_test_deps = []
+cogl_unit_tests_map_data = configuration_data()
 
 if have_cogl_tests
   cogl_test_deps += [libmutter_cogl_test_fixtures_dep]
+
+  cogl_unit_tests_map_data.set('unit_tests_symbols', 'test_*; unit_test_*;')
 endif
 
+libmutter_cogl_map = configure_file(
+  input: 'libmutter-cogl.map.in',
+  output: 'libmutter-cogl.map',
+  configuration: cogl_unit_tests_map_data,
+  install: false,
+)
+
 libmutter_cogl_name = 'mutter-cogl-' + libmutter_api_version
-libmutter_cogl_map = 'libmutter-cogl.map'
 libmutter_cogl_link_args = [
-  '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(),
-      libmutter_cogl_map),
+  '-Wl,--version-script,@0@'.format(libmutter_cogl_map),
 ]
 libmutter_cogl = shared_library(libmutter_cogl_name,
   sources: [cogl_sources, cogl_headers_all],
diff --git a/cogl/tests/conform/meson/find-conform-unit-tests.sh b/cogl/tests/conform/meson/find-conform-unit-tests.sh
index acbc40903..3c42b0a89 100755
--- a/cogl/tests/conform/meson/find-conform-unit-tests.sh
+++ b/cogl/tests/conform/meson/find-conform-unit-tests.sh
@@ -3,8 +3,6 @@
 inputfile="$1"
 outputfile="$2"
 
-echo > "$outputfile"
-
 sed -n -e 's/^ \{1,\}ADD_TEST *( *\([a-zA-Z0-9_]\{1,\}\).*/\1/p' "$inputfile" | while read -r test; do
   echo "$test" >> "$outputfile"
 done
diff --git a/cogl/tests/unit/meson.build b/cogl/tests/unit/meson.build
index 39693443d..2869764f3 100644
--- a/cogl/tests/unit/meson.build
+++ b/cogl/tests/unit/meson.build
@@ -23,20 +23,20 @@ libmutter_cogl_test_unit = executable('test-unit',
   install: false,
 )
 
-find_unit_tests = find_program('meson/find-unit-tests.sh')
-cogl_unit_unit_tests = custom_target('cogl-tests-unit-unit-tests',
-  output: 'unit-tests',
-  input: libmutter_cogl,
-  command: [find_unit_tests, '@INPUT@', '@OUTPUT@'],
-  install: false,
-)
+cogl_unit_tests = run_command(
+  find_program('meson/find-unit-tests.sh'), cogl_srcdir, '/dev/stdout',
+  check: true,
+).stdout().strip().split('\n')
 
-test('unit', cogl_run_tests,
-  suite: ['cogl'],
-  args: [
-    cogl_config_env,
-    libmutter_cogl_test_unit,
-    cogl_unit_unit_tests
-  ],
-  is_parallel: false,
-)
+foreach test_target: cogl_unit_tests
+  test_name = '-'.join(test_target.split('_'))
+  test(test_name, cogl_run_tests,
+    suite: ['cogl', 'cogl/unit'],
+    args: [
+      cogl_config_env,
+      libmutter_cogl_test_unit,
+      'unit_test_' + test_target
+    ],
+    is_parallel: false,
+  )
+endforeach
diff --git a/cogl/tests/unit/meson/find-unit-tests.sh b/cogl/tests/unit/meson/find-unit-tests.sh
index 14370fb73..dc8ceb2bc 100755
--- a/cogl/tests/unit/meson/find-unit-tests.sh
+++ b/cogl/tests/unit/meson/find-unit-tests.sh
@@ -1,6 +1,9 @@
 #!/bin/sh
 
-inputfile="$1"
+inputdir="$1"
 outputfile="$2"
 
-nm "$inputfile" | grep '[DR] _\?unit_test_'|sed 's/.\+ [DR] _\?//' > "$outputfile"
+echo > "$outputfile"
+
+grep -h -r --include \*.c UNIT_TEST "$inputdir" | \
+    sed -n -e 's/^UNIT_TEST *( *\([a-zA-Z0-9_]\{1,\}\).*/\1/p' > "$outputfile"