Skip to content
Snippets Groups Projects
main.cpp 2.95 KiB
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QApplication>
#include <QTemporaryFile>
#include <QTest>

#include "tst_dsmanager.h"
#include "tst_projectmanager.h"
#include "tst_settings.h"

#define DEBUG_LINE qDebug() << "Debug (" << __LINE__ << "):"

int main(int argc, char *argv[])
{
    qputenv("QML_COMPAT_RESOLVE_URLS_ON_ASSIGNMENT", "1");
    QApplication app(argc, argv);

    QStringList initialArgs = app.arguments();
    DEBUG_LINE << "Arguments:" << initialArgs;

    QString outputFileName;
    QString appName = initialArgs.first();
    initialArgs.removeFirst();

    if (initialArgs.contains("-o")) {
        int index = initialArgs.indexOf("-o");
        if (index < initialArgs.size() - 1) {
            outputFileName = initialArgs.at(index + 1);
            outputFileName.remove(",junitxml"); // remove the junitxml extension if it's provided
            DEBUG_LINE << "Find output file name inside the args:" << outputFileName;

            initialArgs.removeAt(index); // remove the -o flag
            initialArgs.removeAt(index); // remove the output file name
        }
    }

    if (outputFileName.isEmpty()) {
        DEBUG_LINE << "Output file name not provided. Using the default name: output.junitxml";
        outputFileName = "output.junitxml";
    }

    QFile outputFile{outputFileName};
    if (!outputFile.open(QIODevice::Truncate | QIODevice::WriteOnly)) {
        DEBUG_LINE << "Failed to open the output file";
        return -1;
    }

    int status = 0;
    auto runTest = [&status, &outputFile, initialArgs, appName](QObject *obj) {
        // temporary file to store the individual test results.
        // we'll append the results to the output file.
        QTemporaryFile file;
        if (!file.open()) {
            DEBUG_LINE << "Failed to open the temporary file";
            status = -1;
            return;
        }

        QStringList args{"-o", QString(file.fileName()).append(",junitxml")};
        args.prepend(appName);
        args.append(initialArgs);

        DEBUG_LINE << "Running test" << obj->metaObject()->className() << "with arguments:" << args;
        status |= QTest::qExec(obj, args);

        // append the test results to the output file
        file.readLine(); // skip the first line because it's the xml header
        const int rV = outputFile.write(file.readAll());
        if (rV == -1) {
            DEBUG_LINE << "Failed to write the test results to the output file";
            status = -1;
            return;
        }
    };

    outputFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    outputFile.write("<testsuites>\n");

    runTest(new TestSettings);
    runTest(new TestDesignStudioManager);
    runTest(new TestProjectManager);

    outputFile.write("</testsuites>\n");
    outputFile.flush();
    outputFile.close();

    DEBUG_LINE << "Test suite finished with status:" << status;
    return status;
}