Skip to content

Commit b498f18

Browse files
committed
Fix issue with unloading single .py file and fix tests.
1 parent a421385 commit b498f18

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
@@ -15,19 +15,20 @@ using ::testing::ElementsAre;
1515

1616
TEST(IPluginDiagnose, Simple)
1717
{
18-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
18+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1919

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

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

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

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

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

4647
ASSERT_THAT(plugin->activeProblems(), ElementsAre(5, 7));
4748
EXPECT_EQ(plugin->shortDescription(5), "short-5");
@@ -54,7 +55,7 @@ TEST(IPluginDiagnose, Simple)
5455

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

tests/runner/test_filemapper.cpp

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

1414
TEST(IPluginFileMapper, 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-filemapper.py");
23-
EXPECT_EQ(objects.size(), 3);
22+
const auto objects =
23+
runner->load("dummy_filemapper", plugins_folder / "dummy-filemapper.py");
24+
EXPECT_EQ(objects.size(), 2);
2425

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

3032
const auto m = plugin->mappings();
31-
EXPECT_EQ(m.size(), 2);
33+
ASSERT_EQ(m.size(), 2);
3234

3335
EXPECT_EQ(m[0].source, "the source");
3436
EXPECT_EQ(m[0].destination, "the destination");
@@ -43,11 +45,12 @@ TEST(IPluginFileMapper, Simple)
4345

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

4952
const auto m = plugin->mappings();
50-
EXPECT_EQ(m.size(), 1);
53+
ASSERT_EQ(m.size(), 1);
5154

5255
EXPECT_EQ(m[0].source, "the source");
5356
EXPECT_EQ(m[0].destination, "the destination");
@@ -57,7 +60,7 @@ TEST(IPluginFileMapper, Simple)
5760

5861
// load the game plugin
5962
{
60-
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[2]);
61-
EXPECT_NE(plugin, nullptr);
63+
IPluginGame* plugin = qobject_cast<IPluginGame*>(objects[1][1]);
64+
ASSERT_NE(plugin, nullptr);
6265
}
6366
}

tests/runner/test_game.cpp

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

1313
TEST(IPluginGame, Simple)
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-game.py");
21+
const auto objects = runner->load("dummy_game", plugins_folder / "dummy-game.py");
2222
EXPECT_EQ(objects.size(), 1);
2323

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

tests/runner/test_installer.cpp

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

1414
TEST(IPluginInstaller, 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-installer.py");
22+
const auto objects =
23+
runner->load("dummy_installer", plugins_folder / "dummy-installer.py");
2324
EXPECT_EQ(objects.size(), 1);
2425

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

2931
// basic tests

tests/runner/test_iplugin.cpp

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

1212
TEST(IPlugin, Basic)
1313
{
14-
const auto plugins_folder = QString(std::getenv("PLUGIN_DIR"));
14+
const auto plugins_folder = std::filesystem::path(std::getenv("PLUGIN_DIR"));
1515

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

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

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

2728
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)