From 4866cac6f17cafe152d0de9d4c64072b01b0724b Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 11 Oct 2022 10:16:27 -0700 Subject: [PATCH] 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 (cherry picked from commit 636dbe604596b2860e4f6d9b159fc017dcd2f66b) Reviewed-by: Qt Cherry-pick Bot --- .../corelib/plugin/qlibrary/tst_qlibrary.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/auto/corelib/plugin/qlibrary/tst_qlibrary.cpp b/tests/auto/corelib/plugin/qlibrary/tst_qlibrary.cpp index d36ca0f8d89..651dd34c018 100644 --- a/tests/auto/corelib/plugin/qlibrary/tst_qlibrary.cpp +++ b/tests/auto/corelib/plugin/qlibrary/tst_qlibrary.cpp @@ -81,6 +81,7 @@ enum QLibraryOperation { QString directory; private slots: void initTestCase(); + void cleanup(); void load(); void load_data(); @@ -124,6 +125,38 @@ void tst_QLibrary::initTestCase() #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() { #ifdef Q_OS_ANDROID