From 6716a102513f19c95b1ed32c327711ce59a21dfc Mon Sep 17 00:00:00 2001
From: Emmanuele Bassi <ebassi@gnome.org>
Date: Wed, 29 Mar 2017 16:58:57 +0100
Subject: [PATCH] Generate metadata for installed tests

When installing tests we also need to generate the appropriate metadata
files so that test harnesses, like GNOME Continuous, can find them and
run them appropriately.

Fixes: #99
https://github.com/ebassi/graphene/commit/6716a102513f
---
 meson.build                     |  1 +
 src/tests/gen-installed-test.py | 39 +++++++++++++++++++++++++++++++++++++++
 src/tests/meson.build           | 27 ++++++++++++++++++++++++---
 4 files changed, 65 insertions(+), 4 deletions(-)
 create mode 100644 src/tests/gen-installed-test.py

diff --git a/meson.build b/meson.build
index 6f4db89..613b55b 100644
--- a/meson.build
+++ b/meson.build
@@ -327,6 +327,7 @@ graphene_simd += [ 'scalar' ]
 
 conf.set('GRAPHENE_SIMD', ' '.join(graphene_simd))
 
+python3 = import('python3')
 gnome = import('gnome')
 
 subdir('src')
diff --git a/src/tests/gen-installed-test.py b/src/tests/gen-installed-test.py
new file mode 100644
index 0000000..91cb796
--- /dev/null
+++ b/src/tests/gen-installed-test.py
@@ -0,0 +1,39 @@
+# Copyright 2017  Emmanuele Bassi
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+import sys
+import os
+import argparse
+
+def write_template(filename, data):
+    with open(filename, 'w') as f:
+        f.write(data)
+
+def build_template(testdir, testname):
+    return "[Test]\nType=session\nExec={}\n".format(os.path.join(testdir, testname))
+
+argparser = argparse.ArgumentParser(description='Generate installed-test data.')
+argparser.add_argument('--testdir', metavar='dir', required=True, help='Installed test directory')
+argparser.add_argument('--testname', metavar='name', required=True, help='Installed test name')
+argparser.add_argument('--outfile', metavar='file', required=True, help='Output file')
+argparser.add_argument('--outdir', metavar='dir', required=True, help='Output directory')
+args = argparser.parse_args()
+
+write_template(os.path.join(args.outdir, args.outfile), build_template(args.testdir, args.testname))
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 5ab0218..6151c91 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -19,16 +19,37 @@ unit_tests = [
   'vec4'
 ]
 
+python = python3.find_python()
+gen_installed_test = join_paths(meson.current_source_dir(), 'gen-installed-test.py')
+
+installed_test_datadir = join_paths(get_option('prefix'), get_option('datadir'), 'installed-tests', 'graphene-1.0')
+installed_test_bindir = join_paths(get_option('prefix'), get_option('libexecdir'), 'installed-tests', 'graphene-1.0')
+
 foreach unit: unit_tests
+  wrapper = '@0@.test'.format(unit)
+  data = custom_target(wrapper,
+                       output: wrapper,
+                       command: [
+                         python,
+                         gen_installed_test,
+                         '--testdir=@0@'.format(installed_test_bindir),
+                         '--testname=@0@'.format(unit),
+                         '--outdir=@OUTDIR@',
+                         '--outfile=@0@'.format(wrapper),
+                       ],
+                       install: true,
+                       install_dir: installed_test_datadir)
+
   exe = executable(unit, unit + '.c',
-                   install: true,
-                   install_dir: join_paths(get_option('libexecdir'), 'installed-tests', 'graphene-1.0'),
                    dependencies: graphene_dep,
                    include_directories: graphene_inc,
                    c_args: [
                      '-DG_LOG_DOMAIN="Graphene-Test"',
                      '-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_30',
                      '-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32'
-                   ])
+                   ],
+                   install: true,
+                   install_dir: installed_test_bindir)
+
   test(unit, exe, args: [ '--tap', '-k' ])
 endforeach

