summaryrefslogtreecommitdiff
path: root/plugins/autotest/testrunner.cpp
blob: f3f7e37143945100b4778eed83e5fd11e3577a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Creator Enterprise Auto Test Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "autotestconstants.h"
#include "autotestplugin.h"
#include "testresultspane.h"
#include "testrunner.h"
#include "testsettings.h"

#include <QDebug> // REMOVE

#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>

#include <projectexplorer/buildmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorersettings.h>

#include <utils/multitask.h>

#include <QFuture>
#include <QFutureInterface>
#include <QTime>

namespace Autotest {
namespace Internal {

static TestRunner *m_instance = 0;
static QProcess *m_runner = 0;
static QFutureInterface<void> *m_currentFuture = 0;

TestRunner *TestRunner::instance()
{
    if (!m_instance)
        m_instance = new TestRunner;
    return m_instance;
}

TestRunner::TestRunner(QObject *parent) :
    QObject(parent),
    m_building(false),
    m_executingTests(false)
{
}

TestRunner::~TestRunner()
{
    qDeleteAll(m_selectedTests);
    m_selectedTests.clear();
    m_instance = 0;
    if (m_runner)
        delete m_runner;
}

void TestRunner::setSelectedTests(const QList<TestConfiguration *> &selected)
{
     qDeleteAll(m_selectedTests);
     m_selectedTests.clear();
     m_selectedTests = selected;
}

/******************** XML line parser helper ********************/

static bool xmlStartsWith(const QString &code, const QString &start, QString &result)
{
    if (code.startsWith(start)) {
        result = code.mid(start.length());
        result = result.left(result.indexOf(QLatin1Char('"')));
        result = result.left(result.indexOf(QLatin1String("</")));
        return !result.isEmpty();
    }
    return false;
}

static bool xmlCData(const QString &code, const QString &start, QString &result)
{
    if (code.startsWith(start)) {
        int index = code.indexOf(QLatin1String("<![CDATA[")) + 9;
        result = code.mid(index, code.indexOf(QLatin1String("]]>"), index) - index);
        return !result.isEmpty();
    }
    return false;
}

static bool xmlExtractTypeFileLine(const QString &code, const QString &tagStart,
                                   ResultType &result, QString &file, int &line)
{
    if (code.startsWith(tagStart)) {
        int start = code.indexOf(QLatin1String(" type=\"")) + 7;
        result = TestResult::resultFromString(
                    code.mid(start, code.indexOf(QLatin1Char('"'), start) - start));
        start = code.indexOf(QLatin1String(" file=\"")) + 7;
        file = code.mid(start, code.indexOf(QLatin1Char('"'), start) - start);
        start = code.indexOf(QLatin1String(" line=\"")) + 7;
        line = code.mid(start, code.indexOf(QLatin1Char('"'), start) - start).toInt();
        return true;
    }
    return false;
}

// adapted from qplaintestlogger.cpp
static QString formatResult(double value)
{
    if (value < 0 || value == NAN)
        return QLatin1String("NAN");
    if (value == 0)
        return QLatin1String("0");

    int significantDigits = 0;
    qreal divisor = 1;

    while (value / divisor >= 1) {
        divisor *= 10;
        ++significantDigits;
    }

    QString beforeDecimalPoint = QString::number(value, 'f', 0);
    QString afterDecimalPoint = QString::number(value, 'f', 20);
    afterDecimalPoint.remove(0, beforeDecimalPoint.count() + 1);

    const int beforeUse = qMin(beforeDecimalPoint.count(), significantDigits);
    const int beforeRemove = beforeDecimalPoint.count() - beforeUse;

    beforeDecimalPoint.chop(beforeRemove);
    for (int i = 0; i < beforeRemove; ++i)
        beforeDecimalPoint.append(QLatin1Char('0'));

    int afterUse = significantDigits - beforeUse;
    if (beforeDecimalPoint == QLatin1String("0") && !afterDecimalPoint.isEmpty()) {
        ++afterUse;
        int i = 0;
        while (i < afterDecimalPoint.count() && afterDecimalPoint.at(i) == QLatin1Char('0'))
            ++i;
        afterUse += i;
    }

    const int afterRemove = afterDecimalPoint.count() - afterUse;
    afterDecimalPoint.chop(afterRemove);

    QString result = beforeDecimalPoint;
    if (afterUse > 0)
        result.append(QLatin1Char('.'));
    result += afterDecimalPoint;

    return result;
}

static bool xmlExtractBenchmarkInformation(const QString &code, const QString &tagStart,
                                           QString &description)
{
    if (code.startsWith(tagStart)) {
        int start = code.indexOf(QLatin1String(" metric=\"")) + 9;
        const QString metric = code.mid(start, code.indexOf(QLatin1Char('"'), start) - start);
        start = code.indexOf(QLatin1String(" value=\"")) + 8;
        const double value = code.mid(start, code.indexOf(QLatin1Char('"'), start) - start).toDouble();
        start = code.indexOf(QLatin1String(" iterations=\"")) + 13;
        const int iterations = code.mid(start, code.indexOf(QLatin1Char('"'), start) - start).toInt();
        QString metricsTxt;
        if (metric == QLatin1String("WalltimeMilliseconds"))         // default
            metricsTxt = QLatin1String("msecs");
        else if (metric == QLatin1String("CPUTicks"))                // -tickcounter
            metricsTxt = QLatin1String("CPU ticks");
        else if (metric == QLatin1String("Events"))                  // -eventcounter
            metricsTxt = QLatin1String("events");
        else if (metric == QLatin1String("InstructionReads"))        // -callgrind
            metricsTxt = QLatin1String("instruction reads");
        else if (metric == QLatin1String("CPUCycles"))               // -perf
            metricsTxt = QLatin1String("CPU cycles");
        description = QObject::tr("%1 %2 per iteration (total: %3, iterations: %4)")
                .arg(formatResult(value))
                .arg(metricsTxt)
                .arg(formatResult(value * (double)iterations))
                .arg(iterations);
        return true;
    }
    return false;
}

/****************** XML line parser helper end ******************/

void processOutput()
{
    if (!m_runner)
        return;
    static QString className;
    static QString testCase;
    static QString dataTag;
    static ResultType result = ResultType::UNKNOWN;
    static QString description;
    static QString file;
    static int lineNumber = 0;
    static QString duration;
    static bool readingDescription = false;
    static QString qtVersion;
    static QString qtestVersion;
    static QString bmDescription;

    while (m_runner->canReadLine()) {
        // TODO Qt5 uses UTF-8 - while Qt4 uses ISO-8859-1 - could this be a problem?
        const QString line = QString::fromUtf8(m_runner->readLine()).trimmed();
        if (line.isEmpty() || line.startsWith(QLatin1String("<?xml version"))) {
            className = QString();
            continue;
        }
        if (xmlStartsWith(line, QLatin1String("<TestCase name=\""), className))
            continue;
        if (xmlStartsWith(line, QLatin1String("<TestFunction name=\""), testCase)) {
            dataTag = QString();
            description = QString();
            duration = QString();
            file = QString();
            result = ResultType::UNKNOWN;
            lineNumber = 0;
            readingDescription = false;
            TestResultsPane::instance()->addTestResult(
                        TestResult(className, testCase, QString(), ResultType::MESSAGE_CURRENT_TEST,
                                   QObject::tr("Entering Test Function %1::%2")
                                   .arg(className).arg(testCase)));
            continue;
        }
        if (xmlStartsWith(line, QLatin1String("<Duration msecs=\""), duration)) {
            continue;
        }
        if (xmlExtractTypeFileLine(line, QLatin1String("<Message"), result, file, lineNumber))
            continue;
        if (xmlCData(line, QLatin1String("<DataTag>"), dataTag))
            continue;
        if (xmlCData(line, QLatin1String("<Description>"), description)) {
            if (!line.endsWith(QLatin1String("</Description>")))
                readingDescription = true;
            continue;
        }
        if (xmlExtractTypeFileLine(line, QLatin1String("<Incident"), result, file, lineNumber)) {
            if (line.endsWith(QLatin1String("/>"))) {
                TestResult testResult(className, testCase, dataTag, result, description);
                if (!file.isEmpty())
                    file = QFileInfo(m_runner->workingDirectory(), file).canonicalFilePath();
                testResult.setFileName(file);
                testResult.setLine(lineNumber);
                TestResultsPane::instance()->addTestResult(testResult);
            }
            continue;
        }
        if (xmlExtractBenchmarkInformation(line, QLatin1String("<BenchmarkResult"), bmDescription)) {
            TestResult testResult(className, testCase, dataTag, ResultType::BENCHMARK, bmDescription);
            TestResultsPane::instance()->addTestResult(testResult);
            continue;
        }
        if (line == QLatin1String("</Message>") || line == QLatin1String("</Incident>")) {
            TestResult testResult(className, testCase, dataTag, result, description);
            if (!file.isEmpty())
                file = QFileInfo(m_runner->workingDirectory(), file).canonicalFilePath();
            testResult.setFileName(file);
            testResult.setLine(lineNumber);
            TestResultsPane::instance()->addTestResult(testResult);
            description = QString();
        } else if (line == QLatin1String("</TestFunction>") && !duration.isEmpty()) {
            TestResult testResult(className, testCase, QString(), ResultType::MESSAGE_INTERNAL,
                                  QObject::tr("execution took %1ms").arg(duration));
            TestResultsPane::instance()->addTestResult(testResult);
            m_currentFuture->setProgressValue(m_currentFuture->progressValue() + 1);
        } else if (line == QLatin1String("</TestCase>") && !duration.isEmpty()) {
            TestResult testResult(className, QString(), QString(), ResultType::MESSAGE_INTERNAL,
                                  QObject::tr("Test execution took %1ms").arg(duration));
            TestResultsPane::instance()->addTestResult(testResult);
        } else if (readingDescription) {
            if (line.endsWith(QLatin1String("]]></Description>"))) {
                description.append(QLatin1Char('\n'));
                description.append(line.left(line.indexOf(QLatin1String("]]></Description>"))));
                readingDescription = false;
            } else {
                description.append(QLatin1Char('\n'));
                description.append(line);
            }
        } else if (xmlStartsWith(line, QLatin1String("<QtVersion>"), qtVersion)) {
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_INTERNAL,
                                   QObject::tr("Qt Version: %1").arg(qtVersion)));
        } else if (xmlStartsWith(line, QLatin1String("<QTestVersion>"), qtestVersion)) {
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_INTERNAL,
                                   QObject::tr("QTest Version: %1").arg(qtestVersion)));
        } else {
//            qDebug() << "Unhandled line:" << line; // TODO remove
        }
    }
}

static QString which(const QString &path, const QString &cmd)
{
    if (path.isEmpty() || cmd.isEmpty())
        return QString();

    QStringList paths;
#ifdef Q_OS_WIN
    paths = path.split(QLatin1Char(';'));
#else
    paths = path.split(QLatin1Char(':'));
#endif

    foreach (const QString &p, paths) {
        const QString fName = p + QDir::separator() + cmd;
        QFileInfo fi(fName);
        if (fi.exists() && fi.isExecutable())
            return fName;
#ifdef Q_OS_WIN
        fi = QFileInfo(fName + QLatin1String(".exe"));
        if (fi.exists())
            return fi.absoluteFilePath();
        fi = QFileInfo(fName + QLatin1String(".bat"));
        if (fi.exists())
            return fi.absoluteFilePath();
        fi = QFileInfo(fName + QLatin1String(".cmd"));
        if (fi.exists())
            return fi.absoluteFilePath();
#endif
    }
    return QString();
}

bool performExec(const QString &cmd, const QStringList &args, const QString &workingDir,
                 const Utils::Environment &env, int timeout)
{
    QString runCmd;
    if (!QDir::toNativeSeparators(cmd).contains(QDir::separator())) {
        if (env.hasKey(QLatin1String("PATH")))
            runCmd = which(env.value(QLatin1String("PATH")), cmd);
    } else if (QFileInfo(cmd).exists()) {
        runCmd = cmd;
    }

    if (runCmd.isEmpty()) {
        TestResultsPane::instance()->addTestResult(
                    TestResult(QString(), QString(), QString(), ResultType::MESSAGE_FATAL,
                               QObject::tr("*** Could not find command '%1' ***").arg(cmd)));
        return false;
    }

    m_runner->setWorkingDirectory(workingDir);
    m_runner->setProcessEnvironment(env.toProcessEnvironment());
    QTime executionTimer;

    if (args.count()) {
        m_runner->start(runCmd, args);
    } else {
        m_runner->start(runCmd);
    }

    bool ok = m_runner->waitForStarted();
    executionTimer.start();
    if (ok) {
        while (m_runner->state() == QProcess::Running && executionTimer.elapsed() < timeout) {
            if (m_currentFuture->isCanceled()) {
                m_runner->kill();
                m_runner->waitForFinished();
                TestResultsPane::instance()->addTestResult(
                            TestResult(QString(), QString(), QString(), ResultType::MESSAGE_FATAL,
                                       QObject::tr("*** Test Run canceled by user ***")));
            }
            qApp->processEvents();
        }
    }
    if (ok && executionTimer.elapsed() < timeout) {
        return m_runner->exitCode() == 0;
    } else {
        if (m_runner->state() != QProcess::NotRunning) {
            m_runner->kill();
            m_runner->waitForFinished();
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_FATAL,
                                   QObject::tr("*** Test Case canceled due to timeout ***\n"
                                               "Maybe raise the timeout?")));
        }
        return false;
    }
}

void performTestRun(QFutureInterface<void> &future, const QList<TestConfiguration *> selectedTests)
{
    int testCaseCount = 0;
    foreach (const TestConfiguration *config, selectedTests)
        testCaseCount += config->testCaseCount();

    m_currentFuture = &future;
    m_runner = new QProcess;
    m_runner->setReadChannelMode(QProcess::MergedChannels);
    m_runner->setReadChannel(QProcess::StandardOutput);

    QObject::connect(m_runner, &QProcess::readyReadStandardOutput, &processOutput);

    future.setProgressRange(0, testCaseCount);
    future.setProgressValue(0);

    const QSharedPointer<TestSettings> settings = AutotestPlugin::instance()->settings();
    const int timeout = settings->timeout;
    const QString metricsOption = TestSettings::metricsTypeToOption(settings->metrics);

    foreach (const TestConfiguration *tc, selectedTests) {
        if (future.isCanceled())
            break;
        QString cmd = tc->targetFile();
        QString workDir = tc->workingDirectory();
        QStringList args;
        Utils::Environment env = tc->environment();

        args << QLatin1String("-xml");
        if (!metricsOption.isEmpty())
            args << metricsOption;
        if (tc->testCases().count())
            args << tc->testCases();

        performExec(cmd, args, workDir, env, timeout);
    }
    future.setProgressValue(testCaseCount);

    delete m_runner;
    m_runner = 0;
    m_currentFuture = 0;
}

void TestRunner::runTests()
{
    // clear old log and output pane
    TestResultsPane::instance()->clearContents();

    // handle faulty test configurations
    QList<TestConfiguration *> toBeRemoved;
    foreach (TestConfiguration *config, m_selectedTests)
        if (!config->project()) {
            toBeRemoved.append(config);
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_WARN,
                                   tr("*** Project is null for '%1' - removing from Test Run ***\n"
                                      "This might be the case for a faulty environment or similar."
                                      ).arg(config->displayName())));
        }
    foreach (TestConfiguration *config, toBeRemoved) {
        m_selectedTests.removeOne(config);
        delete config;
    }

    if (m_selectedTests.empty()) {
        TestResultsPane::instance()->addTestResult(
                    TestResult(QString(), QString(), QString(), ResultType::MESSAGE_WARN,
                               tr("*** No tests selected - canceling Test Run ***")));
        return;
    }

    ProjectExplorer::Project *project = m_selectedTests.at(0)->project();
    if (!project) {
        TestResultsPane::instance()->addTestResult(
                    TestResult(QString(), QString(), QString(), ResultType::MESSAGE_WARN,
                               tr("*** Project is null - canceling Test Run ***\n"
                                  "Actually only Desktop kits are supported - make sure the "
                                  "current active kit is a Desktop kit.")));
        return;
    }

    ProjectExplorer::ProjectExplorerPlugin *pep = ProjectExplorer::ProjectExplorerPlugin::instance();
    ProjectExplorer::Internal::ProjectExplorerSettings pes = pep->projectExplorerSettings();
    if (pes.buildBeforeDeploy) {
        if (!project->hasActiveBuildSettings()) {
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_FATAL,
                                   tr("*** Project is not configured - canceling Test Run ***")));
            return;
        }
        buildProject(project);
        while (m_building) {
            qApp->processEvents();
        }

        if (!m_buildSucceeded) {
            TestResultsPane::instance()->addTestResult(
                        TestResult(QString(), QString(), QString(), ResultType::MESSAGE_FATAL,
                                   tr("*** Build failed - canceling Test Run ***")));
            return;
        }
    }

    m_executingTests = true;
    emit testRunStarted();
    QFuture<void> future = QtConcurrent::run(&performTestRun , m_selectedTests);
    Core::FutureProgress *progress = Core::ProgressManager::addTask(future, tr("Running Tests"),
                                                                    Autotest::Constants::TASK_INDEX);
    connect(progress, &Core::FutureProgress::finished,
            TestRunner::instance(), &TestRunner::onFinished);
}

void TestRunner::buildProject(ProjectExplorer::Project *project)
{
    m_building = true;
    m_buildSucceeded = false;
    ProjectExplorer::BuildManager *mgr = static_cast<ProjectExplorer::BuildManager *>(
                ProjectExplorer::BuildManager::instance());
    ProjectExplorer::ProjectExplorerPlugin *pep = ProjectExplorer::ProjectExplorerPlugin::instance();
    connect(mgr, &ProjectExplorer::BuildManager::buildQueueFinished,
            this, &TestRunner::buildFinished);
    pep->buildProject(project);
}

void TestRunner::buildFinished(bool success)
{
    ProjectExplorer::BuildManager *mgr = static_cast<ProjectExplorer::BuildManager *>(
                ProjectExplorer::BuildManager::instance());
    disconnect(mgr, &ProjectExplorer::BuildManager::buildQueueFinished,
               this, &TestRunner::buildFinished);
    m_building = false;
    m_buildSucceeded = success;
}

void TestRunner::onFinished()
{
    m_executingTests = false;
    emit testRunFinished();
}

void TestRunner::stopTestRun()
{
    if (m_runner && m_runner->state() != QProcess::NotRunning && m_currentFuture)
        m_currentFuture->cancel();
}

} // namespace Internal
} // namespace Autotest