File indexing completed on 2024-05-12 04:39:28

0001 /*
0002     SPDX-FileCopyrightText: 2012 Miha Čančula <miha@noughmad.eu>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ctestfindjob.h"
0008 #include "ctestsuite.h"
0009 #include <debug_testing.h>
0010 
0011 #include <interfaces/icore.h>
0012 #include <interfaces/ilanguagecontroller.h>
0013 #include <language/duchain/duchain.h>
0014 #include <language/backgroundparser/backgroundparser.h>
0015 
0016 #include <KLocalizedString>
0017 
0018 CTestFindJob::CTestFindJob(CTestSuite* suite, QObject* parent)
0019 : KJob(parent)
0020 , m_suite(suite)
0021 {
0022     qCDebug(CMAKE_TESTING) << "Created a CTestFindJob";
0023     setObjectName(i18n("Parse test suite %1", suite->name()));
0024     setCapabilities(Killable);
0025 }
0026 
0027 void CTestFindJob::start()
0028 {
0029     qCDebug(CMAKE_TESTING) << "Finding test cases";
0030     QMetaObject::invokeMethod(this, &CTestFindJob::findTestCases, Qt::QueuedConnection);
0031 }
0032 
0033 void CTestFindJob::findTestCases()
0034 {
0035     if (!m_suite)
0036         return;
0037 
0038     m_pendingFiles.clear();
0039     const auto& sourceFiles = m_suite->sourceFiles();
0040     for (const auto& file : sourceFiles) {
0041         if (!file.isEmpty())
0042         {
0043             m_pendingFiles << file;
0044         }
0045     }
0046     qCDebug(CMAKE_TESTING) << "Source files to update:" << m_pendingFiles;
0047 
0048     if (m_pendingFiles.isEmpty()) {
0049         m_suite = nullptr;
0050         emitResult();
0051         return;
0052     }
0053 
0054     const auto currentPendingFiles = m_pendingFiles;
0055     for (const KDevelop::Path& file : currentPendingFiles) {
0056         KDevelop::DUChain::self()->updateContextForUrl(KDevelop::IndexedString(file.toUrl()), KDevelop::TopDUContext::AllDeclarationsAndContexts, this);
0057     }
0058 }
0059 
0060 void CTestFindJob::updateReady(const KDevelop::IndexedString& document, const KDevelop::ReferencedTopDUContext& context)
0061 {
0062     if (!m_suite) {
0063         // killed or done already
0064         return;
0065     }
0066 
0067     if (Q_UNLIKELY(!m_suite->project())) {
0068         qCDebug(CMAKE_TESTING) << "Cannot add test suite" << m_suite->name()
0069                                << "because its project is already destroyed (probably closed by the user).";
0070         kill();
0071         return;
0072     }
0073 
0074     qCDebug(CMAKE_TESTING) << "context update ready" << m_pendingFiles << document.str();
0075     m_suite->loadDeclarations(document, context);
0076     m_pendingFiles.removeAll(KDevelop::Path(document.toUrl()));
0077 
0078     if (m_pendingFiles.isEmpty()) {
0079         m_suite = nullptr;
0080         emitResult();
0081     }
0082 }
0083 
0084 bool CTestFindJob::doKill()
0085 {
0086     m_suite = nullptr;
0087     KDevelop::ICore::self()->languageController()->backgroundParser()->revertAllRequests(this);
0088     return true;
0089 }
0090 
0091 #include "moc_ctestfindjob.cpp"