File indexing completed on 2024-11-10 04:40:08

0001 /*
0002  * SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "testrunner.h"
0008 #include "akonaditest_debug.h"
0009 
0010 #include <KProcess>
0011 
0012 TestRunner::TestRunner(const QStringList &args, QObject *parent)
0013     : QObject(parent)
0014     , mArguments(args)
0015     , mExitCode(0)
0016     , mProcess(nullptr)
0017 {
0018 }
0019 
0020 int TestRunner::exitCode() const
0021 {
0022     return mExitCode;
0023 }
0024 
0025 void TestRunner::run()
0026 {
0027     qCDebug(AKONADITEST_LOG) << "Starting test" << mArguments;
0028     mProcess = new KProcess(this);
0029     mProcess->setProgram(mArguments);
0030     connect(mProcess, &KProcess::finished, this, &TestRunner::processFinished);
0031     connect(mProcess, &KProcess::errorOccurred, this, &TestRunner::processError);
0032     // environment setup seems to have been done by setuptest globally already
0033     mProcess->start();
0034     if (!mProcess->waitForStarted()) {
0035         qCWarning(AKONADITEST_LOG) << mArguments << "failed to start!";
0036         mExitCode = 255;
0037         Q_EMIT finished();
0038     }
0039 }
0040 
0041 void TestRunner::triggerTermination(int exitCode)
0042 {
0043     processFinished(exitCode);
0044 }
0045 
0046 void TestRunner::processFinished(int exitCode)
0047 {
0048     // Only update the exit code when it is 0. This prevents overwriting a non-zero
0049     // value with 0. This can happen when multiple processes finish or triggerTermination
0050     // is called after a process has finished.
0051     if (mExitCode == 0) {
0052         mExitCode = exitCode;
0053         qCInfo(AKONADITEST_LOG) << "Test finished with exist code" << exitCode;
0054     }
0055     Q_EMIT finished();
0056 }
0057 
0058 void TestRunner::processError(QProcess::ProcessError error)
0059 {
0060     qCWarning(AKONADITEST_LOG) << mArguments << "exited with an error:" << error;
0061     mExitCode = 255;
0062     Q_EMIT finished();
0063 }
0064 
0065 void TestRunner::terminate()
0066 {
0067     if (mProcess) {
0068         mProcess->terminate();
0069     }
0070 }
0071 
0072 #include "moc_testrunner.cpp"