From ebeae438dbaa3e9f1cda33a17c4a6530feac80d9 Mon Sep 17 00:00:00 2001
From: Michael Stahl <mstahl@redhat.com>
Date: Fri, 5 Apr 2013 23:07:18 +0200
Subject: move Python tests in-process

This is nice to make them more easily debuggable.
A series of crude hacks are employed to bootstrap enough services from
python so the current tests run.
This is only tested with system python3 on Fedora.

Change-Id: I5e06741e55ead7fddec41ff776ff8ca5d2399469
Reviewed-on: https://gerrit.libreoffice.org/3215
Reviewed-by: David Ostrovsky <David.Ostrovsky@gmx.de>
Tested-by: David Ostrovsky <David.Ostrovsky@gmx.de>
---
 pyuno/source/module/pyuno_module.cxx | 42 ++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

(limited to 'pyuno')

diff --git a/pyuno/source/module/pyuno_module.cxx b/pyuno/source/module/pyuno_module.cxx
index 768b6d735a8a..62851c4741c0 100644
--- a/pyuno/source/module/pyuno_module.cxx
+++ b/pyuno/source/module/pyuno_module.cxx
@@ -320,6 +320,47 @@ static PyObject* getComponentContext(
     return ret.getAcquired();
 }
 
+static PyObject* initPoniesMode(
+    SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
+{
+    // this tries to bootstrap enough of the soffice from python to run
+    // unit tests, which is only possible indirectly because pyuno is URE
+    // so load "test" library and invoke a function there to do the work
+    try
+    {
+        PyObject *const ctx(getComponentContext(0, 0));
+        if (!ctx) { abort(); }
+        Runtime const runtime;
+        Any const a(runtime.pyObject2Any(ctx));
+        Reference<XComponentContext> xContext;
+        a >>= xContext;
+        if (!xContext.is()) { abort(); }
+        using com::sun::star::lang::XMultiServiceFactory;
+        Reference<XMultiServiceFactory> const xMSF(
+            xContext->getServiceManager(),
+            com::sun::star::uno::UNO_QUERY_THROW);
+        if (!xMSF.is()) { abort(); }
+        char *const outdir = getenv("OUTDIR");
+        if (!outdir) { abort(); }
+        OStringBuffer libname(outdir);
+        libname.append("/lib/");
+        libname.append(SAL_DLLPREFIX "test" SAL_DLLEXTENSION);
+        oslModule const mod( osl_loadModuleAscii(libname.getStr(),
+                                SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL) );
+        if (!mod) { abort(); }
+        oslGenericFunction const pFunc(
+                osl_getAsciiFunctionSymbol(mod, "test_init"));
+        if (!pFunc) { abort(); }
+        // guess casting pFunc is undefined behavior but don't see a better way
+        ((void (SAL_CALL *)(XMultiServiceFactory*)) pFunc) (xMSF.get());
+    }
+    catch (const com::sun::star::uno::Exception & e)
+    {
+        abort();
+    }
+    return Py_None;
+}
+
 PyObject * extractOneStringArg( PyObject *args, char const *funcName )
 {
     if( !PyTuple_Check( args ) || PyTuple_Size( args) != 1 )
@@ -797,6 +838,7 @@ static PyObject *setCurrentContext(
 
 struct PyMethodDef PyUNOModule_methods [] =
 {
+    {"experimentalExtraMagic", initPoniesMode, METH_VARARGS, NULL},
     {"getComponentContext", getComponentContext, METH_VARARGS, NULL},
     {"_createUnoStructHelper", reinterpret_cast<PyCFunction>(createUnoStructHelper), METH_VARARGS | METH_KEYWORDS, NULL},
     {"getTypeByName", getTypeByName, METH_VARARGS, NULL},
-- 
cgit