From b843400284751968862751dfe93853f151551c64 Mon Sep 17 00:00:00 2001
From: Carlos Rafael Giani <crg7475@mailbox.org>
Date: Fri, 25 Oct 2019 00:06:26 +0200
Subject: [PATCH] meson: Add option for installed tests

This adds an option for producing installed versions of the unit tests.
These versions don't need meson to run (only a small shell script). This
makes it easier to run cross compiled tests on a target machine.

Upstream-Status: Pending

Signed-off-by: Carlos Rafael Giani <crg7475@mailbox.org>
---
 build-aux/gen-installed-test-desc.py     | 18 ++++++
 build-aux/gen-installed-test-shscript.py | 25 ++++++++
 meson_options.txt                        |  2 +
 tests/check/meson.build                  | 46 +++++++++++++-
 tests/files/testfile                     | 80 ++++++++++++++++++++++++
 5 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100644 build-aux/gen-installed-test-desc.py
 create mode 100644 build-aux/gen-installed-test-shscript.py
 create mode 100644 tests/files/testfile

diff --git a/build-aux/gen-installed-test-desc.py b/build-aux/gen-installed-test-desc.py
new file mode 100644
index 0000000..69e8a0f
--- /dev/null
+++ b/build-aux/gen-installed-test-desc.py
@@ -0,0 +1,18 @@
+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('--test-execdir', metavar='dir', required=True, help='Installed test directory')
+argparser.add_argument('--testname', metavar='name', required=True, help='Installed test name')
+argparser.add_argument('--output', metavar='file', required=True, help='Output file')
+args = argparser.parse_args()
+
+write_template(args.output, build_template(args.test_execdir, args.testname))
diff --git a/build-aux/gen-installed-test-shscript.py b/build-aux/gen-installed-test-shscript.py
new file mode 100644
index 0000000..5da86fb
--- /dev/null
+++ b/build-aux/gen-installed-test-shscript.py
@@ -0,0 +1,25 @@
+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 ''.join([
+        "#!/usr/bin/env sh\n",
+        "export GST_STATE_IGNORE_ELEMENTS=''\n",
+        "export CK_DEFAULT_TIMEOUT=20\n",
+        "export GST_PLUGIN_LOADING_WHITELIST='gstreamer'\n",
+        "{}\n".format(os.path.join(testdir, testname)),
+    ])
+
+argparser = argparse.ArgumentParser(description='Generate installed-test data.')
+argparser.add_argument('--test-execdir', metavar='dir', required=True, help='Installed test directory')
+argparser.add_argument('--testname', metavar='name', required=True, help='Installed test name')
+argparser.add_argument('--output', metavar='file', required=True, help='Output file')
+args = argparser.parse_args()
+
+write_template(args.output, build_template(args.test_execdir, args.testname))
+os.chmod(args.output, 0o755)
diff --git a/meson_options.txt b/meson_options.txt
index 8afde39..8884dcc 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -16,6 +16,8 @@ option('poisoning', type : 'boolean', value : false, description : 'Enable poiso
 option('memory-alignment', type: 'combo',
        choices : ['1', '2', '4', '8', '16', '32', '64', '128', '256', '512', '1024', '2048', '4096', '8192', 'malloc', 'pagesize'],
        value: 'malloc')
+option('installed-tests', type : 'boolean', value : false, description : 'enable installed tests')
+option('test-files-path', type : 'string', description : 'Path where to find test files')
 
 # Feature options
 option('check', type : 'feature', value : 'auto', description : 'Build unit test libraries')
diff --git a/tests/check/meson.build b/tests/check/meson.build
index 04da83f..2db7f76 100644
--- a/tests/check/meson.build
+++ b/tests/check/meson.build
@@ -118,11 +118,17 @@ if add_languages('cpp', required : false)
   ]
 endif
 
+test_files_path = get_option('test-files-path')
+if test_files_path == ''
+  test_files_path = meson.current_source_dir() + '/../files'
+endif
+message('Using path "@0@" as the path to read test files from'.format(test_files_path))
+
 test_defines = [
   '-UG_DISABLE_ASSERT',
   '-UG_DISABLE_CAST_CHECKS',
   '-DGST_CHECK_TEST_ENVIRONMENT_BEACON="GST_STATE_IGNORE_ELEMENTS"',
-  '-DTESTFILE="' + meson.current_source_dir() + '/meson.build"',
+  '-DTESTFILE="@0@"'.format(test_files_path + '/testfile'),
   '-DGST_USE_UNSTABLE_API',
   '-DGST_DISABLE_DEPRECATED',
 ]
@@ -137,6 +143,14 @@ endif
 glib_deps = [gio_dep, gobject_dep, gmodule_dep, glib_dep]
 gst_deps = [gst_dep, gst_base_dep, gst_check_dep, gst_net_dep, gst_controller_dep]
 
+installed_tests_datadir = join_paths(prefix, get_option('datadir'), 'installed-tests', 'gstreamer-1.0')
+installed_tests_execdir = join_paths(prefix, libexecdir, 'installed-tests', 'gstreamer-1.0')
+installed_tests_enabled = get_option('installed-tests')
+
+python = import('python').find_installation()
+gen_installed_test_desc = files('../../build-aux/gen-installed-test-desc.py')
+gen_installed_test_shscript = files('../../build-aux/gen-installed-test-shscript.py')
+
 foreach t : core_tests
   fname = t[0]
   test_name = fname.split('.')[0].underscorify()
@@ -150,8 +164,38 @@ foreach t : core_tests
         include_directories : [configinc],
         link_with : link_with_libs,
         dependencies : test_deps + glib_deps + gst_deps,
+        install_dir: installed_tests_execdir,
+        install: installed_tests_enabled
     )
 
+    if installed_tests_enabled
+      installed_test_shscript = test_name + '.sh'
+      shscript = custom_target (test_name + '_shscript',
+                                output: installed_test_shscript,
+                                command: [
+                                  python,
+                                  gen_installed_test_shscript,
+                                  '--test-execdir=@0@'.format(installed_tests_execdir),
+                                  '--testname=@0@'.format(test_name),
+                                  '--output=@0@'.format(join_paths('@OUTDIR@', installed_test_shscript)),
+                                ],
+                                install: true,
+                                install_dir: installed_tests_execdir)
+
+      installed_test_desc = test_name + '.test'
+      data = custom_target(test_name + '_desc',
+                           output: installed_test_desc,
+                           command: [
+                             python,
+                             gen_installed_test_desc,
+                             '--test-execdir=@0@'.format(installed_tests_execdir),
+                             '--testname=@0@'.format(installed_test_shscript),
+                             '--output=@0@'.format(join_paths('@OUTDIR@', installed_test_desc)),
+                           ],
+                           install: true,
+                           install_dir: installed_tests_datadir)
+    endif
+
     env = environment()
     env.set('GST_PLUGIN_PATH_1_0', meson.build_root())
     env.set('GST_PLUGIN_SYSTEM_PATH_1_0', '')
diff --git a/tests/files/testfile b/tests/files/testfile
new file mode 100644
index 0000000..89954e0
--- /dev/null
+++ b/tests/files/testfile
@@ -0,0 +1,80 @@
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
+................................................................................
-- 
2.17.1