tdf#169387 Add a unit test to check the Python home

Adds a unit test that loads a Python module through the Python loader.
The module contains an XJob that checks whether sys.prefix is a
subdirectory of the LibreOffice install directory.

Change-Id: I095ac0dfb835ce47c5ad28807d47fc96b9f10266
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/211360
Tested-by: Jenkins
Reviewed-by: Ilmari Lauhakangas <ilmari.lauhakangas@libreoffice.org>
This commit is contained in:
Neil Roberts
2026-09-24 08:37:49 +02:00
committed by Ilmari Lauhakangas
parent af745380b6
commit 1f917206a1
4 changed files with 157 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; fill-column: 100 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_CppunitTest_CppunitTest,pyuno_pythonhome))
$(eval $(call gb_CppunitTest_add_exception_objects,pyuno_pythonhome, \
pyuno/qa/cppunit/testpythonhome \
))
$(eval $(call gb_CppunitTest_use_libraries,pyuno_pythonhome, \
cppu \
cppuhelper \
sal \
subsequenttest \
test \
tl \
unotest \
utl \
))
$(eval $(call gb_CppunitTest_use_externals,pyuno_pythonhome,\
libxml2 \
))
$(eval $(call gb_CppunitTest_use_api,pyuno_pythonhome,\
udkapi \
offapi \
))
$(eval $(call gb_CppunitTest_use_components,pyuno_pythonhome,\
pyuno/source/loader/pythonloader \
))
$(eval $(call gb_CppunitTest_use_ure,pyuno_pythonhome))
$(eval $(call gb_CppunitTest_use_vcl,pyuno_pythonhome))
$(eval $(call gb_CppunitTest_use_rdb,pyuno_pythonhome,services))
$(eval $(call gb_CppunitTest_use_configuration,pyuno_pythonhome))
# vim: set noet sw=4 ts=4:
+1
View File
@@ -46,6 +46,7 @@ endif
$(eval $(call gb_Module_add_check_targets,pyuno, \
PythonTest_pyuno_pytests_testimports \
$(if $(SYSTEM_PYTHON),,CppunitTest_pyuno_pythonhome) \
))
endif # !SYSTEM_PYTHON
+64
View File
@@ -0,0 +1,64 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <test/unoapi_test.hxx>
#include <com/sun/star/lang/XSingleComponentFactory.hpp>
#include <com/sun/star/loader/XImplementationLoader.hpp>
#include <com/sun/star/task/XJob.hpp>
namespace
{
class PythonHomeTest : public UnoApiTest
{
public:
PythonHomeTest()
: UnoApiTest(u"/pyuno/qa/extras"_ustr)
{
}
private:
void testHome();
CPPUNIT_TEST_SUITE(PythonHomeTest);
CPPUNIT_TEST(testHome);
CPPUNIT_TEST_SUITE_END();
};
void PythonHomeTest::testHome()
{
// Tests that the Python environment has the Python home correctly configured. See tdf#169387
css::uno::Reference<css::loader::XImplementationLoader> xLoader(
m_xFactory->createInstanceWithContext(u"com.sun.star.loader.Python"_ustr, m_xContext),
css::uno::UNO_QUERY_THROW);
css::uno::Reference<css::lang::XSingleComponentFactory> xFactory(
xLoader->activate(u"org.libreoffice.pyuno.check_python_home"_ustr, u""_ustr,
createFileURL(u"check_python_home.py"),
css::uno::Reference<css::registry::XRegistryKey>()),
css::uno::UNO_QUERY_THROW);
css::uno::Reference<css::task::XJob> xJob(xFactory->createInstanceWithContext(m_xContext),
css::uno::UNO_QUERY_THROW);
// Execute the job contained in the test service. This should throw an exception if the test
// fails.
xJob->execute(css::uno::Sequence<css::beans::NamedValue>());
}
CPPUNIT_TEST_SUITE_REGISTRATION(PythonHomeTest);
} // namespace
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
+47
View File
@@ -0,0 +1,47 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from com.sun.star.task import XJob
from com.sun.star.uri import UriReferenceFactory
from com.sun.star.util import theMacroExpander
import os.path
import sys
import uno
import unohelper
g_ImplementationHelper = unohelper.ImplementationHelper()
g_implName = "org.libreoffice.pyuno.check_python_home"
class CheckPythonHome(unohelper.Base, XJob):
def __init__(self, ctx):
self.ctx = ctx
def execute(self, args):
# Make sure that the Python prefix is a subdirectory of $BRAND_BASE_DIR
xServiceManager = self.ctx.ServiceManager
xUriFactory = UriReferenceFactory.create(self.ctx)
xUriReference = xUriFactory.parse("vnd.sun.star.expand:$BRAND_BASE_DIR")
uri = xUriReference.expand(theMacroExpander.get(self.ctx))
brand_path = os.path.realpath(uno.fileUrlToSystemPath(uri))
prefix_path = os.path.realpath(sys.prefix)
assert prefix_path.startswith(brand_path)
g_ImplementationHelper.addImplementation(
CheckPythonHome,
g_implName,
(
),
)
# vim: set shiftwidth=4 softtabstop=4 expandtab: