File indexing completed on 2024-05-19 05:42:15

0001 // ct_lvtplg_pythonlibrarydispatcher.t.cpp                           -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtplg_pythonlibrarydispatcher.h>
0021 
0022 #include <QDir>
0023 #include <catch2-local-includes.h>
0024 #include <ct_lvttst_tmpdir.h>
0025 #include <string>
0026 
0027 using namespace Codethink::lvtplg;
0028 namespace py = pybind11;
0029 
0030 static auto constexpr PLUGIN_CONTENTS =
0031     ""
0032     "def hookSetupPlugin(handler):"
0033     "    pass"
0034     "";
0035 
0036 TEST_CASE("Python dispatcher")
0037 {
0038     py::scoped_interpreter py;
0039     py::gil_scoped_release defaultReleaseGIL;
0040 
0041     auto pluginDir = TmpDir{std::string{"myplugin"}};
0042     auto plgDirAsQDir = QDir{QString::fromStdString(pluginDir.path().string())};
0043 
0044     REQUIRE_FALSE(PythonLibraryDispatcher::isValidPlugin(plgDirAsQDir));
0045     (void) pluginDir.createTextFile("myplugin.py", PLUGIN_CONTENTS);
0046     (void) pluginDir.createTextFile("README.md", "");
0047     (void) pluginDir.createTextFile("metadata.json", "");
0048     REQUIRE(PythonLibraryDispatcher::isValidPlugin(plgDirAsQDir));
0049     auto pluginLib = PythonLibraryDispatcher::loadSinglePlugin(plgDirAsQDir);
0050     {
0051         auto resolveCtx = pluginLib->resolve("hookSetupPlugin");
0052         (void) resolveCtx;
0053         auto *setupHook = reinterpret_cast<hookSetupPlugin_f>(resolveCtx->hook);
0054         auto handler = PluginSetupHandler{
0055             [](auto _1, auto _2) {},
0056             [](std::string const& id) {
0057                 return nullptr;
0058             },
0059             [](auto _1) {},
0060         };
0061 
0062         REQUIRE(setupHook != nullptr);
0063         setupHook(&handler);
0064     }
0065 
0066     {
0067         auto resolveCtx = pluginLib->resolve("hookTeardownPlugin");
0068         auto *notImplementedHook = reinterpret_cast<hookTeardownPlugin_f>(resolveCtx->hook);
0069         REQUIRE(notImplementedHook == nullptr);
0070     }
0071 }