Skip to content

Commit 9b1793c

Browse files
committed
Fix issue with unloading single .py file and fix tests.
1 parent c658b39 commit 9b1793c

File tree

7 files changed

+59
-39
lines changed

7 files changed

+59
-39
lines changed

src/runner/pythonrunner.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,13 @@ namespace mo2::python {
246246
{
247247
py::gil_scoped_acquire lock;
248248

249-
// At this point, the identifier is the full path to the module.
249+
// at this point, the identifier is the full path to the module.
250250
QDir folder(modulePath);
251251

252252
// we want to "unload" (remove from sys.modules) modules that come
253253
// from this plugin (whose __path__ points under this module,
254254
// including the module of the plugin itself)
255+
//
255256
py::object sys = py::module_::import("sys");
256257
py::dict modules = sys.attr("modules");
257258
py::list keys = modules.attr("keys")();
@@ -269,6 +270,14 @@ namespace mo2::python {
269270
}
270271
}
271272
}
273+
274+
// for simple Python file - not really used anymore, but actually used in
275+
// testing - we need to remove using the module name
276+
//
277+
py::str pyModuleName(moduleName);
278+
if (modules.contains(pyModuleName)) {
279+
PyDict_DelItem(modules.ptr(), pyModuleName.ptr());
280+
}
272281
}
273282

274283
bool PythonRunner::isInitialized() const

tests/runner/test_diagnose.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@ using ::testing::ElementsAre;
1616

1717
TEST(IPluginDiagnose, Simple)
1818
{
19-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
19+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
2020

2121
auto runner = mo2::python::createPythonRunner();
2222
runner->initialize();
2323

2424
// load objects
25-
const auto objects = runner->load(plugins_folder + "/dummy-diagnose.py");
26-
EXPECT_EQ(objects.size(), 3);
25+
const auto objects =
26+
runner->load("dummy_diagnose", plugins_folder / "dummy-diagnose.py");
27+
ASSERT_EQ(objects.size(), 2);
2728

2829
// load the first IPluginDiagnose
2930
{
30-
IPluginDiagnose* plugin = qobject_cast<IPluginDiagnose*>(objects[0]);
31-
EXPECT_NE(plugin, nullptr);
31+
IPluginDiagnose* plugin = qobject_cast<IPluginDiagnose*>(objects[0][0]);
32+
ASSERT_NE(plugin, nullptr);
3233

3334
ASSERT_THAT(plugin->activeProblems(), ElementsAre(1, 2));
3435
EXPECT_EQ(plugin->shortDescription(1), "short-1");
@@ -41,8 +42,8 @@ TEST(IPluginDiagnose, Simple)
4142

4243
// load the second one (this is cast before IPluginGame so should be before)
4344
{
44-
IPluginDiagnose* plugin = qobject_cast<IPluginDiagnose*>(objects[1]);
45-
EXPECT_NE(plugin, nullptr);
45+
IPluginDiagnose* plugin = qobject_cast<IPluginDiagnose*>(objects[1][0]);
46+
ASSERT_NE(plugin, nullptr);
4647

4748
ASSERT_THAT(plugin->activeProblems(), ElementsAre(5, 7));
4849
EXPECT_EQ(plugin->shortDescription(5), "short-5");
@@ -55,7 +56,7 @@ TEST(IPluginDiagnose, Simple)
5556

5657
// load the game plugin
5758
{
58-
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[2]);
59-
EXPECT_NE(plugin, nullptr);
59+
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[1][1]);
60+
ASSERT_NE(plugin, nullptr);
6061
}
6162
}

tests/runner/test_filemapper.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ using namespace MOBase;
1414

1515
TEST(IPluginFileMapper, Simple)
1616
{
17-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
17+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1818

1919
auto runner = mo2::python::createPythonRunner();
2020
runner->initialize();
2121

2222
// load objects
23-
const auto objects = runner->load(plugins_folder + "/dummy-filemapper.py");
24-
EXPECT_EQ(objects.size(), 3);
23+
const auto objects =
24+
runner->load("dummy_filemapper", plugins_folder / "dummy-filemapper.py");
25+
EXPECT_EQ(objects.size(), 2);
2526

2627
// load the first IPluginFileMapper
2728
{
28-
IPluginFileMapper* plugin = qobject_cast<IPluginFileMapper*>(objects[0]);
29-
EXPECT_NE(plugin, nullptr);
29+
ASSERT_EQ(objects[0].size(), 1);
30+
IPluginFileMapper* plugin = qobject_cast<IPluginFileMapper*>(objects[0][0]);
31+
ASSERT_NE(plugin, nullptr);
3032

3133
const auto m = plugin->mappings();
32-
EXPECT_EQ(m.size(), 2);
34+
ASSERT_EQ(m.size(), 2);
3335

3436
EXPECT_EQ(m[0].source, "the source");
3537
EXPECT_EQ(m[0].destination, "the destination");
@@ -44,11 +46,12 @@ TEST(IPluginFileMapper, Simple)
4446

4547
// load the second one (this is cast before IPluginGame so should be before)
4648
{
47-
IPluginFileMapper* plugin = qobject_cast<IPluginFileMapper*>(objects[1]);
48-
EXPECT_NE(plugin, nullptr);
49+
ASSERT_EQ(objects[1].size(), 2);
50+
IPluginFileMapper* plugin = qobject_cast<IPluginFileMapper*>(objects[1][0]);
51+
ASSERT_NE(plugin, nullptr);
4952

5053
const auto m = plugin->mappings();
51-
EXPECT_EQ(m.size(), 1);
54+
ASSERT_EQ(m.size(), 1);
5255

5356
EXPECT_EQ(m[0].source, "the source");
5457
EXPECT_EQ(m[0].destination, "the destination");
@@ -58,7 +61,7 @@ TEST(IPluginFileMapper, Simple)
5861

5962
// load the game plugin
6063
{
61-
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[2]);
62-
EXPECT_NE(plugin, nullptr);
64+
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[1][1]);
65+
ASSERT_NE(plugin, nullptr);
6366
}
6467
}

tests/runner/test_game.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ using namespace MOBase;
1313

1414
TEST(IPluginGame, Simple)
1515
{
16-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
16+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1717

1818
auto runner = mo2::python::createPythonRunner();
1919
runner->initialize();
2020

2121
// load objects
22-
const auto objects = runner->load(plugins_folder + "/dummy-game.py");
22+
const auto objects = runner->load("dummy_game", plugins_folder / "dummy-game.py");
2323
EXPECT_EQ(objects.size(), 1);
2424

2525
// load the IPlugin
26-
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[0]);
26+
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[0][0]);
2727
EXPECT_NE(plugin, nullptr);
2828
}

tests/runner/test_installer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ using namespace MOBase;
1414

1515
TEST(IPluginInstaller, Simple)
1616
{
17-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
17+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1818

1919
auto runner = mo2::python::createPythonRunner();
2020
runner->initialize();
2121

2222
// load objects
23-
const auto objects = runner->load(plugins_folder + "/dummy-installer.py");
23+
const auto objects =
24+
runner->load("dummy_installer", plugins_folder / "dummy-installer.py");
2425
EXPECT_EQ(objects.size(), 1);
2526

2627
// load the IPlugin
27-
IPluginInstallerSimple* plugin = qobject_cast<IPluginInstallerSimple*>(objects[0]);
28+
IPluginInstallerSimple* plugin =
29+
qobject_cast<IPluginInstallerSimple*>(objects[0][0]);
2830
EXPECT_NE(plugin, nullptr);
2931

3032
// basic tests

tests/runner/test_iplugin.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ using namespace MOBase;
1212

1313
TEST(IPlugin, Basic)
1414
{
15-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
15+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1616

1717
auto runner = mo2::python::createPythonRunner();
1818
runner->initialize();
1919

2020
// load objects
21-
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
21+
const auto objects =
22+
runner->load("dummy_iplugin", plugins_folder / "dummy-iplugin.py");
2223
EXPECT_EQ(objects.size(), 1);
2324

2425
// load the IPlugin
25-
const IPlugin* plugin = qobject_cast<IPlugin*>(objects[0]);
26+
const IPlugin* plugin = qobject_cast<IPlugin*>(objects[0][0]);
2627
EXPECT_NE(plugin, nullptr);
2728

2829
EXPECT_EQ(plugin->author(), "The Author");

tests/runner/test_lifetime.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,42 @@
77

88
TEST(Lifetime, Plugins)
99
{
10-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
10+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1111

1212
auto runner = mo2::python::createPythonRunner();
1313
runner->initialize();
1414

1515
{
16-
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
16+
const auto objects =
17+
runner->load("dummy_iplugin", plugins_folder / "dummy-iplugin.py");
1718

1819
// we found one plugin
19-
EXPECT_EQ(objects.size(), 1);
20+
ASSERT_EQ(objects.size(), 1);
2021

2122
// check that deleting the object actually destroys it
2223
bool destroyed = false;
23-
QObject::connect(objects[0], &QObject::destroyed, [&destroyed]() {
24+
QObject::connect(objects[0][0], &QObject::destroyed, [&destroyed]() {
2425
destroyed = true;
2526
});
26-
delete objects[0];
27+
delete objects[0][0];
2728
EXPECT_EQ(destroyed, true);
29+
30+
runner->unload("dummy_iplugin", plugins_folder / "dummy-iplugin.py");
2831
}
2932

3033
// same things but with a parent
3134
{
3235
QObject* dummy_parent = new QObject();
33-
const auto objects = runner->load(plugins_folder + "/dummy-iplugin.py");
36+
const auto objects =
37+
runner->load("dummy_iplugin", plugins_folder / "dummy-iplugin.py");
3438

3539
// we found one plugin
36-
EXPECT_EQ(objects.size(), 1);
37-
objects[0]->setParent(dummy_parent);
40+
ASSERT_EQ(objects.size(), 1);
41+
objects[0][0]->setParent(dummy_parent);
3842

3943
// check that deleting the object actually destroys it
4044
bool destroyed = false;
41-
QObject::connect(objects[0], &QObject::destroyed, [&destroyed]() {
45+
QObject::connect(objects[0][0], &QObject::destroyed, [&destroyed]() {
4246
destroyed = true;
4347
});
4448
delete dummy_parent;

0 commit comments

Comments
 (0)