aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/gtest/gtestoutputreader.cpp
blob: 96382f5f89651ad2c4d59dd6666ec002994b67c7 (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
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "gtestoutputreader.h"
#include "gtestresult.h"

#include <QDir>
#include <QFileInfo>
#include <QRegExp>

namespace Autotest {
namespace Internal {

static QString constructSourceFilePath(const QString &path, const QString &filePath)
{
    return QFileInfo(path, filePath).canonicalFilePath();
}

GTestOutputReader::GTestOutputReader(const QFutureInterface<TestResultPtr> &futureInterface,
                                     QProcess *testApplication, const QString &buildDirectory)
    : TestOutputReader(futureInterface, testApplication, buildDirectory)
{
}

void GTestOutputReader::processOutput(const QByteArray &outputLine)
{
    static QRegExp newTestStarts("^\\[-{10}\\] \\d+ tests? from (.*)$");
    static QRegExp testEnds("^\\[-{10}\\] \\d+ tests? from (.*) \\((.*)\\)$");
    static QRegExp newTestSetStarts("^\\[ RUN      \\] (.*)$");
    static QRegExp testSetSuccess("^\\[       OK \\] (.*) \\((.*)\\)$");
    static QRegExp testSetFail("^\\[  FAILED  \\] (.*) \\((.*)\\)$");
    static QRegExp disabledTests("^  YOU HAVE (\\d+) DISABLED TESTS?$");
    static QRegExp failureLocation("^(.*):(\\d+): Failure$");
    static QRegExp errorLocation("^(.*)\\((\\d+)\\): error:.*$");
    static QRegExp iterations("^Repeating all tests \\(iteration (\\d+)\\) \\. \\. \\.$");

    const QString line = QString::fromLatin1(outputLine);
    if (line.trimmed().isEmpty())
        return;

    if (!line.startsWith('[')) {
        m_description.append(line).append('\n');
        if (iterations.exactMatch(line)) {
            m_iteration = iterations.cap(1).toInt();
            m_description.clear();
        } else if (line.startsWith(QStringLiteral("Note:"))) {
            m_description = line;
            if (m_iteration > 1)
                m_description.append(' ' + tr("(iteration %1)").arg(m_iteration));
            TestResultPtr testResult = TestResultPtr(new GTestResult);
            testResult->setResult(Result::MessageInternal);
            testResult->setDescription(m_description);
            m_futureInterface.reportResult(testResult);
            m_description.clear();
        } else if (disabledTests.exactMatch(line)) {
            TestResultPtr testResult = TestResultPtr(new GTestResult());
            testResult->setResult(Result::MessageDisabledTests);
            int disabled = disabledTests.cap(1).toInt();
            testResult->setDescription(tr("You have %n disabled test(s).", 0, disabled));
            testResult->setLine(disabled); // misuse line property to hold number of disabled
            m_futureInterface.reportResult(testResult);
            m_description.clear();
        }
        return;
    }

    if (testEnds.exactMatch(line)) {
        GTestResult *testResult = createDefaultResult();
        testResult->setResult(Result::MessageTestCaseEnd);
        testResult->setDescription(tr("Test execution took %1").arg(testEnds.cap(2)));
        m_futureInterface.reportResult(TestResultPtr(testResult));
        m_currentTestName.clear();
        m_currentTestSet.clear();
    } else if (newTestStarts.exactMatch(line)) {
        m_currentTestName = newTestStarts.cap(1);
        TestResultPtr testResult = TestResultPtr(createDefaultResult());
        testResult->setResult(Result::MessageTestCaseStart);
        if (m_iteration > 1) {
            testResult->setDescription(tr("Repeating test case %1 (iteration %2)")
                                       .arg(m_currentTestName).arg(m_iteration));
        } else {
            testResult->setDescription(tr("Executing test case %1").arg(m_currentTestName));
        }
        m_futureInterface.reportResult(testResult);
    } else if (newTestSetStarts.exactMatch(line)) {
        m_currentTestSet = newTestSetStarts.cap(1);
        TestResultPtr testResult = TestResultPtr(new GTestResult());
        testResult->setResult(Result::MessageCurrentTest);
        testResult->setDescription(tr("Entering test set %1").arg(m_currentTestSet));
        m_futureInterface.reportResult(testResult);
        m_description.clear();
    } else if (testSetSuccess.exactMatch(line)) {
        GTestResult *testResult = createDefaultResult();
        testResult->setResult(Result::Pass);
        testResult->setDescription(m_description);
        m_futureInterface.reportResult(TestResultPtr(testResult));
        m_description.clear();
        testResult = createDefaultResult();
        testResult->setResult(Result::MessageInternal);
        testResult->setDescription(tr("Execution took %1.").arg(testSetSuccess.cap(2)));
        m_futureInterface.reportResult(TestResultPtr(testResult));
        m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
    } else if (testSetFail.exactMatch(line)) {
        GTestResult *testResult = createDefaultResult();
        testResult->setResult(Result::Fail);
        m_description.chop(1);
        testResult->setDescription(m_description);

        for (const QString &output : m_description.split('\n')) {
            QRegExp *match = nullptr;
            if (failureLocation.exactMatch(output))
                match = &failureLocation;
            else if (errorLocation.exactMatch(output))
                match = &errorLocation;

            if (match) {
                QString file = constructSourceFilePath(m_buildDir, match->cap(1));
                if (!file.isEmpty()) {
                    testResult->setFileName(file);
                    testResult->setLine(match->cap(2).toInt());
                    break;
                }
            }
        }
        m_futureInterface.reportResult(TestResultPtr(testResult));
        m_description.clear();
        testResult = createDefaultResult();
        testResult->setResult(Result::MessageInternal);
        testResult->setDescription(tr("Execution took %1.").arg(testSetFail.cap(2)));
        m_futureInterface.reportResult(TestResultPtr(testResult));
        m_futureInterface.setProgressValue(m_futureInterface.progressValue() + 1);
    }
}

GTestResult *GTestOutputReader::createDefaultResult() const
{
    GTestResult *result = new GTestResult(m_currentTestName);
    result->setTestSetName(m_currentTestSet);
    result->setIteration(m_iteration);
    return result;
}

} // namespace Internal
} // namespace Autotest