diff --git a/src/libs/extensionsystem/CMakeLists.txt b/src/libs/extensionsystem/CMakeLists.txt index c8e37668060..a1768577b01 100644 --- a/src/libs/extensionsystem/CMakeLists.txt +++ b/src/libs/extensionsystem/CMakeLists.txt @@ -20,10 +20,16 @@ add_qtc_library(ExtensionSystem find_package(Qt6 COMPONENTS Test QUIET) +# If ExtensionSystem was compiled with QtTest, it should provide the test options +# and the test related API, regardless if Qt Creator was compiled with tests or not, +# and regardless if an external plugin is compiled with tests or not. +# ExtensionSystem may not require QtTest in public headers though. +# API with EXTENSIONSYSTEM_TEST_EXPORT (like CppPluginSpec) should only be exported +# if Qt Creator is compiled with tests. extend_qtc_library(ExtensionSystem CONDITION TARGET Qt::Test DEPENDS Qt::Test - DEFINES WITH_TESTS + PUBLIC_DEFINES EXTENSIONSYSTEM_WITH_TESTOPTION ) extend_qtc_library(ExtensionSystem diff --git a/src/libs/extensionsystem/extensionsystem.qbs b/src/libs/extensionsystem/extensionsystem.qbs index 72fe2f4c3cf..c34e97e7544 100644 --- a/src/libs/extensionsystem/extensionsystem.qbs +++ b/src/libs/extensionsystem/extensionsystem.qbs @@ -2,6 +2,7 @@ QtcLibrary { name: "ExtensionSystem" cpp.defines: base.concat(["EXTENSIONSYSTEM_LIBRARY", "IDE_TEST_DIR=\".\""]) + .concat(qtc.withPluginTests ? ["EXTENSIONSYSTEM_WITH_TESTOPTION"] : []) Depends { name: "Qt"; submodules: ["core", "widgets"] } Depends { name: "Qt.testlib"; condition: qtc.withPluginTests } @@ -35,5 +36,7 @@ QtcLibrary { Export { Depends { name: "Qt.core" } + Depends { name: "qtc" } + cpp.defines: qtc.withPluginTests ? ["EXTENSIONSYSTEM_WITH_TESTOPTION"] : [] } } diff --git a/src/libs/extensionsystem/optionsparser.cpp b/src/libs/extensionsystem/optionsparser.cpp index dd9d03944a2..21dfba16ecb 100644 --- a/src/libs/extensionsystem/optionsparser.cpp +++ b/src/libs/extensionsystem/optionsparser.cpp @@ -64,7 +64,7 @@ bool OptionsParser::parse() continue; if (checkForNoCrashcheckOption()) continue; -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION if (checkForTestOptions()) continue; if (checkForScenarioOption()) diff --git a/src/libs/extensionsystem/pluginmanager.cpp b/src/libs/extensionsystem/pluginmanager.cpp index 78bf778b31c..87ea5914b83 100644 --- a/src/libs/extensionsystem/pluginmanager.cpp +++ b/src/libs/extensionsystem/pluginmanager.cpp @@ -42,7 +42,7 @@ #include #include -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION #include #include #include @@ -745,7 +745,7 @@ void PluginManager::formatOptions(QTextStream &str, int optionIndentation, int d QLatin1String("Disable startup check for previously crashed instance"), optionIndentation, descriptionIndentation); -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION formatOption(str, QString::fromLatin1(OptionsParser::TEST_OPTION) + QLatin1String(" [,testfunction[:testdata]]..."), QString(), QLatin1String("Run plugin's tests (by default a separate settings path is used)"), @@ -798,7 +798,7 @@ bool PluginManager::testRunRequested() return !d->testSpecs.empty(); } -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION // Called in plugin initialization, the scenario function will be called later, from main bool PluginManager::registerScenario(const QString &scenarioId, std::function scenarioStarter) { @@ -956,17 +956,18 @@ void PluginManagerPrivate::startDelayedInitialize() } NANOTRACE_SHUTDOWN(); emit q->initializationDone(); -#ifdef WITH_TESTS - if (PluginManager::testRunRequested()) - startTests(); - else if (PluginManager::isScenarioRequested()) { - if (PluginManager::runScenario()) { - const QString info = QString("Successfully started scenario \"%1\"...").arg(d->m_requestedScenario); - qInfo("%s", qPrintable(info)); - } else { - QMetaObject::invokeMethod(this, [] { emit m_instance->scenarioFinished(1); }); - } +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION + if (PluginManager::testRunRequested()) + startTests(); + else if (PluginManager::isScenarioRequested()) { + if (PluginManager::runScenario()) { + const QString info + = QString("Successfully started scenario \"%1\"...").arg(d->m_requestedScenario); + qInfo("%s", qPrintable(info)); + } else { + QMetaObject::invokeMethod(this, [] { emit m_instance->scenarioFinished(1); }); } + } #endif } @@ -1068,14 +1069,15 @@ void PluginManagerPrivate::checkForDuplicatePlugins() static QHash> g_testCreators; -void PluginManagerPrivate::addTestCreator(IPlugin *plugin, const TestCreator &testCreator) +void PluginManagerPrivate::addTestCreator( + [[maybe_unused]] IPlugin *plugin, [[maybe_unused]] const TestCreator &testCreator) { -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION g_testCreators[plugin].append(testCreator); #endif } -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION using TestPlan = QHash; // Object -> selected test functions @@ -1456,7 +1458,7 @@ void PluginManagerPrivate::shutdown() shutdownEventLoop->exec(); } deleteAll(); -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION if (PluginManager::isScenarioRunning("TestModelManagerInterface")) { qDebug() << "Point 2: Expect the next call to Point 3 triggers a crash"; QThread::sleep(5); diff --git a/src/libs/extensionsystem/pluginmanager.h b/src/libs/extensionsystem/pluginmanager.h index 74237db5b34..b35df535b79 100644 --- a/src/libs/extensionsystem/pluginmanager.h +++ b/src/libs/extensionsystem/pluginmanager.h @@ -106,7 +106,7 @@ public: static bool testRunRequested(); -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION static bool registerScenario(const QString &scenarioId, std::function scenarioStarter); static bool isScenarioRequested(); static bool runScenario(); diff --git a/src/libs/extensionsystem/pluginmanager_p.h b/src/libs/extensionsystem/pluginmanager_p.h index 2404a8b49e9..074400bd6d9 100644 --- a/src/libs/extensionsystem/pluginmanager_p.h +++ b/src/libs/extensionsystem/pluginmanager_p.h @@ -151,7 +151,7 @@ private: void deleteAll(); void checkForDuplicatePlugins(); -#ifdef WITH_TESTS +#ifdef EXTENSIONSYSTEM_WITH_TESTOPTION void startTests(); #endif };