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

0001 /*
0002  *
0003  * SPDX-FileCopyrightText: 2008 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  */
0007 
0008 #include "akonaditest_debug.h"
0009 #include "config.h" //krazy:exclude=includes
0010 #include "setup.h"
0011 #include "shellscript.h"
0012 #include "testrunner.h"
0013 
0014 #include <KAboutData>
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QApplication>
0019 #include <QCommandLineParser>
0020 #include <QSessionManager>
0021 #include <signal.h>
0022 
0023 static SetupTest *setup = nullptr;
0024 static TestRunner *runner = nullptr;
0025 
0026 void sigHandler(int signal)
0027 {
0028     qCCritical(AKONADITEST_LOG, "Received signal %d", signal);
0029     static int sigCounter = 0;
0030     if (sigCounter == 0) { // try clean shutdown
0031         if (runner) {
0032             runner->terminate();
0033         }
0034         if (setup) {
0035             setup->shutdown();
0036         }
0037     } else if (sigCounter == 1) { // force shutdown
0038         if (setup) {
0039             setup->shutdownHarder();
0040         }
0041     } else { // give up and just exit
0042         exit(255);
0043     }
0044     ++sigCounter;
0045 }
0046 
0047 int main(int argc, char **argv)
0048 {
0049     QApplication app(argc, argv);
0050     app.setQuitLockEnabled(false);
0051 
0052     KAboutData aboutdata(QStringLiteral("akonadi-TES"),
0053                          i18n("Akonadi Testing Environment Setup"),
0054                          QStringLiteral("1.0"),
0055                          i18n("Setup Environment"),
0056                          KAboutLicense::GPL,
0057                          i18n("(c) 2008 Igor Trindade Oliveira"));
0058     KAboutData::setApplicationData(aboutdata);
0059 
0060     QCommandLineParser parser;
0061     parser.addOption(
0062         {{QStringLiteral("c"), QStringLiteral("config")}, i18n("Configuration file to open"), QStringLiteral("configfile"), QStringLiteral("config.xml")});
0063     parser.addOption({{QStringLiteral("b"), QStringLiteral("backend")}, i18n("Database backend"), QStringLiteral("backend"), QStringLiteral("sqlite")});
0064     parser.addOption({QStringList{QStringLiteral("!+[test]")}, i18n("Test to run automatically, interactive if none specified"), QString()});
0065     parser.addOption({QStringList{QStringLiteral("testenv")}, i18n("Path where testenvironment would be saved"), QStringLiteral("path")});
0066 
0067     aboutdata.setupCommandLine(&parser);
0068     parser.process(app);
0069     aboutdata.processCommandLine(&parser);
0070 
0071     auto disableSessionManagement = [](QSessionManager &sm) {
0072         sm.setRestartHint(QSessionManager::RestartNever);
0073     };
0074     QObject::connect(qApp, &QGuiApplication::commitDataRequest, qApp, disableSessionManagement);
0075     QObject::connect(qApp, &QGuiApplication::saveStateRequest, qApp, disableSessionManagement);
0076 
0077     if (parser.isSet(QStringLiteral("config"))) {
0078         const auto backend = parser.value(QStringLiteral("backend"));
0079         if (backend != QLatin1StringView("sqlite") && backend != QLatin1StringView("mysql") && backend != QLatin1StringView("pgsql")) {
0080             qCritical("Invalid backend specified. Supported values are: sqlite,mysql,pgsql");
0081             return 1;
0082         }
0083 
0084         Config::instance(parser.value(QStringLiteral("config")));
0085 
0086         if (!Config::instance()->setDbBackend(backend)) {
0087             qCritical("Current configuration does not support the selected backend");
0088             return 1;
0089         }
0090     }
0091 
0092 #ifdef Q_OS_UNIX
0093     signal(SIGINT, sigHandler);
0094     signal(SIGQUIT, sigHandler);
0095 #endif
0096 
0097     setup = new SetupTest();
0098 
0099     if (!setup->startAkonadiDaemon()) {
0100         delete setup;
0101         qCCritical(AKONADITEST_LOG, "Failed to start Akonadi server!");
0102         return 1;
0103     }
0104 
0105     ShellScript sh;
0106     sh.setEnvironmentVariables(setup->environmentVariables());
0107 
0108     if (parser.isSet(QStringLiteral("testenv"))) {
0109         sh.makeShellScript(parser.value(QStringLiteral("testenv")));
0110     } else {
0111 #ifdef Q_OS_WIN
0112         sh.makeShellScript(setup->basePath() + QLatin1StringView("testenvironment.ps1"));
0113 #else
0114         sh.makeShellScript(setup->basePath() + QLatin1StringView("testenvironment.sh"));
0115 #endif
0116     }
0117 
0118     if (!parser.positionalArguments().isEmpty()) {
0119         QStringList testArgs;
0120         for (int i = 0; i < parser.positionalArguments().count(); ++i) {
0121             testArgs << parser.positionalArguments().at(i);
0122         }
0123         runner = new TestRunner(testArgs);
0124         QObject::connect(setup, &SetupTest::setupDone, runner, &TestRunner::run);
0125         QObject::connect(setup, &SetupTest::serverExited, runner, &TestRunner::triggerTermination);
0126         QObject::connect(runner, &TestRunner::finished, setup, &SetupTest::shutdown);
0127     }
0128 
0129     int exitCode = app.exec();
0130     if (runner) {
0131         exitCode += runner->exitCode();
0132         delete runner;
0133     }
0134 
0135     delete setup;
0136     setup = nullptr;
0137 
0138     return exitCode;
0139 }