tst_QLibrary: add a cleanup() method to unload left-overs

QLibrary intentionally does not unload on destruction, so failing tests
may leave libraries already loaded and cause further tests to fail
because of that. So add a cleanup() method to unload everything we may
have loaded.

Note that QLibrary::unload() sets its state to NotLoaded after one
successful call, so we must recreate the object in case it had been
load()ed multiple times.

Change-Id: I12a088d1ae424825abd3fffd171d133c678f910a
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 636dbe604596b2860e4f6d9b159fc017dcd2f66b)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2022-10-11 10:16:27 -07:00 committed by Qt Cherry-pick Bot
parent c7c360cd51
commit 4866cac6f1

View File

@ -81,6 +81,7 @@ enum QLibraryOperation {
QString directory; QString directory;
private slots: private slots:
void initTestCase(); void initTestCase();
void cleanup();
void load(); void load();
void load_data(); void load_data();
@ -124,6 +125,38 @@ void tst_QLibrary::initTestCase()
#endif #endif
} }
void tst_QLibrary::cleanup()
{
// unload the libraries, if they are still loaded after the test ended
// (probably in a failure)
static struct {
QString name;
int version = -1;
} libs[] = {
{ directory + "/mylib" },
{ directory + "/mylib", 1 },
{ directory + "/mylib", 2 },
{ sys_qualifiedLibraryName("mylib") },
// stuff that load_data() succeeds with
{ directory + "/" PREFIX "mylib" },
{ directory + "/" PREFIX "mylib" SUFFIX },
#if defined(Q_OS_WIN32)
{ directory + "/mylib.dl2" },
{ directory + "/system.qt.test.mylib.dll" },
#elif !defined(Q_OS_ANDROID)
// .so even on macOS
{ directory + "/libmylib.so2" },
{ directory + "/system.qt.test.mylib.so" },
#endif
};
for (const auto &entry : libs) {
do {} while (QLibrary(entry.name, entry.version).unload());
}
}
void tst_QLibrary::version_data() void tst_QLibrary::version_data()
{ {
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID