aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/squish/squishxmloutputhandler.cpp
blob: 8a0f011c527b71caa4607d6eaf81323828920f8d (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
// Copyright (C) 2022 The Qt Company Ltd
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "squishxmloutputhandler.h"

#include "squishoutputpane.h"
#include "squishresultmodel.h"
#include "squishtr.h"

#include <utils/qtcassert.h>

#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QXmlStreamWriter>

namespace Squish {
namespace Internal {

SquishXmlOutputHandler::SquishXmlOutputHandler(QObject *parent)
    : QObject(parent)
{
    connect(this,
            &SquishXmlOutputHandler::resultItemCreated,
            SquishOutputPane::instance(),
            &SquishOutputPane::addResultItem,
            Qt::QueuedConnection);
}

void SquishXmlOutputHandler::clearForNextRun()
{
    m_xmlReader.clear();
}

void SquishXmlOutputHandler::mergeResultFiles(const QStringList &reportFiles,
                                              const QString &resultsDirectory,
                                              const QString &suiteName,
                                              QString *error)
{
    QFile resultsXML(QString::fromLatin1("%1/results.xml").arg(resultsDirectory));
    if (resultsXML.exists()) {
        if (error)
            *error = Tr::tr("Could not merge results into single results.xml.\n"
                            "Destination file \"%1\" already exists.")
                         .arg(resultsXML.fileName());
        return;
    }

    if (!resultsXML.open(QFile::WriteOnly)) {
        if (error)
            *error = Tr::tr("Could not merge results into single results.xml.\n"
                            "Failed to open file \"%1\"")
                         .arg(resultsXML.fileName());
        return;
    }

    QXmlStreamWriter xmlWriter(&resultsXML);
    xmlWriter.writeStartDocument("1.0");
    bool isFirstReport = true;
    bool isFirstTest = true;
    QString lastEpilogTime;
    for (const QString &caseResult : reportFiles) {
        QFile currentResultsFile(caseResult);
        if (!currentResultsFile.exists())
            continue;
        if (!currentResultsFile.open(QFile::ReadOnly))
            continue;
        QXmlStreamReader reader(&currentResultsFile);
        while (!reader.atEnd()) {
            QXmlStreamReader::TokenType type = reader.readNext();
            switch (type) {
            case QXmlStreamReader::StartElement: {
                const QString tagName = reader.name().toString();
                // SquishReport of the first results.xml will be taken as is and as this is a
                // merged results.xml we add another test tag holding the suite's name
                if (tagName == "SquishReport") {
                    if (isFirstReport) {
                        xmlWriter.writeStartElement(tagName);
                        xmlWriter.writeAttributes(reader.attributes());
                        xmlWriter.writeStartElement("test");
                        xmlWriter.writeAttribute("name", suiteName);
                        isFirstReport = false;
                    }
                    break;
                }
                if (isFirstTest && tagName == "test") {
                    // the prolog tag of the first results.xml holds the start time of the suite
                    // we already wrote the test tag for the suite, but haven't added the start
                    // time as we didn't know about it, so store information of the current test
                    // tag (case name), read ahead (prolog tag), write prolog (suite's test tag)
                    // and finally write test tag (case name) - the prolog tag (for test case)
                    // will be written outside the if
                    const QXmlStreamAttributes testAttributes = reader.attributes();
                    QXmlStreamReader::TokenType token;
                    while (!reader.atEnd()) {
                        token = reader.readNext();
                        if (token != QXmlStreamReader::Characters)
                            break;
                    }
                    const QString prolog = reader.name().toString();
                    QTC_ASSERT(token == QXmlStreamReader::StartElement
                                   && prolog == "prolog",
                               if (error) *error = Tr::tr("Error while parsing first test result.");
                               return );
                    xmlWriter.writeStartElement(prolog);
                    xmlWriter.writeAttributes(reader.attributes());
                    xmlWriter.writeEndElement();
                    xmlWriter.writeStartElement("test");
                    xmlWriter.writeAttributes(testAttributes);
                    isFirstTest = false;
                } else if (tagName == "epilog") {
                    lastEpilogTime = reader.attributes().value("time").toString();
                }
                xmlWriter.writeCurrentToken(reader);
                break;
            }
            case QXmlStreamReader::EndElement:
                if (reader.name() != QLatin1String("SquishReport"))
                    xmlWriter.writeCurrentToken(reader);
                break;
            case QXmlStreamReader::Characters:
                xmlWriter.writeCurrentToken(reader);
                break;
            // ignore the rest
            default:
                break;
            }
        }
        currentResultsFile.close();
    }
    if (!lastEpilogTime.isEmpty()) {
        xmlWriter.writeStartElement("epilog");
        xmlWriter.writeAttribute("time", lastEpilogTime);
        xmlWriter.writeEndElement();
    }
    xmlWriter.writeEndDocument();
}

Result::Type resultFromString(const QString &type)
{
    if (type == "DETAILED")
        return Result::Detail;
    if (type == "LOG")
        return Result::Log;
    if (type == "PASS")
        return Result::Pass;
    if (type == "FAIL")
        return Result::Fail;
    if (type == "WARNING")
        return Result::Warn;
    if (type == "XFAIL")
        return Result::ExpectedFail;
    if (type == "XPASS")
        return Result::UnexpectedPass;
    if (type == "FATAL")
        return Result::Fatal;
    if (type == "ERROR")
        return Result::Error;
    return Result::Log;
}

// this method uses the XML reader to parse output of the Squish results.xml and put it into an
// item that can be used to display inside the test results pane
// TODO: support Squish report 3.x as well
void SquishXmlOutputHandler::outputAvailable(const QByteArray &output)
{
    static SquishResultItem *testCaseRootItem;
    static QString name;
    static QString details;
    static QString logDetails;
    static QStringList logDetailsList;
    static QString time;
    static QString file;
    static Result::Type type;
    static int line = 0;
    static bool prepend = false;

    m_xmlReader.addData(output);

    while (!m_xmlReader.atEnd()) {
        QXmlStreamReader::TokenType tokenType = m_xmlReader.readNext();
        switch (tokenType) {
        case QXmlStreamReader::StartDocument:
        case QXmlStreamReader::EndDocument:
            break;
        case QXmlStreamReader::StartElement: {
            const QString currentName = m_xmlReader.name().toString();
            // tags verification, message, epilog and test will start a new entry, so, reset values
            if (currentName == "verification"
                || currentName == "message"
                || currentName == "epilog"
                || currentName == "test") {
                name = currentName;
                details.clear();
                logDetails.clear();
                logDetailsList.clear();
                time.clear();
                file.clear();
                line = 0;
                type = Result::Log;
                if (currentName == "test")
                    testCaseRootItem = nullptr;
            } else if (currentName == "result") {
                // result tag won't add another entry, but gives more information on enclosing tag
                name = currentName;
            }

            // description tag could provide information that must be prepended to the former entry
            if (currentName == "description") {
                prepend = (name == "result" && m_xmlReader.attributes().isEmpty());
            } else {
                const QXmlStreamAttributes attributes = m_xmlReader.attributes();
                for (const QXmlStreamAttribute &att : attributes) {
                    const QString attributeName = att.name().toString();
                    if (attributeName == "time")
                        time = QDateTime::fromString(att.value().toString(), Qt::ISODate)
                                   .toString("MMM dd, yyyy h:mm:ss AP");
                    else if (attributeName == "file")
                        file = att.value().toString();
                    else if (attributeName == "line")
                        line = att.value().toInt();
                    else if (attributeName == "type")
                        type = resultFromString(att.value().toString());
                    else if (attributeName == "name")
                        logDetails = att.value().toString();
                }
            }
            // handle prolog (test) elements already within the start tag
            if (currentName == "prolog") {
                TestResult result(Result::Start, logDetails, time);
                result.setFile(file);
                result.setLine(line);
                testCaseRootItem = new SquishResultItem(result);
                emit resultItemCreated(testCaseRootItem);
            }
            break;
        }
        case QXmlStreamReader::EndElement: {
            const QString currentName = m_xmlReader.name().toString();
            // description and result tags are handled differently, test tags are handled by
            // prolog tag (which is handled in QXmlStreamReader::StartElement already),
            // SquishReport tags will be ignored completely
            if (currentName == "epilog") {
                QTC_ASSERT(testCaseRootItem, break);
                TestResult result(Result::End, QString(), time);
                SquishResultItem *item = new SquishResultItem(result);
                testCaseRootItem->appendChild(item);
            } else if (currentName == "description") {
                if (!prepend && !details.trimmed().isEmpty()) {
                    logDetailsList.append(details.trimmed());
                    details.clear();
                }
            } else if (currentName != "prolog"
                       && currentName != "test"
                       && currentName != "result"
                       && currentName != "SquishReport") {
                TestResult result(type, logDetails, time);
                if (logDetails.isEmpty() && !logDetailsList.isEmpty())
                    result.setText(logDetailsList.takeFirst());
                result.setFile(file);
                result.setLine(line);
                SquishResultItem *item = new SquishResultItem(result);
                if (!logDetailsList.isEmpty()) {
                    for (const QString &detail : qAsConst(logDetailsList)) {
                        TestResult childResult(Result::Detail, detail);
                        SquishResultItem *childItem = new SquishResultItem(childResult);
                        item->appendChild(childItem);
                    }
                }
                testCaseRootItem->appendChild(item);
            }
            break;
        }
        case QXmlStreamReader::Characters: {
            QString text = m_xmlReader.text().toString();
            if (m_xmlReader.isCDATA() || !text.trimmed().isEmpty()) {
                if (!m_xmlReader.isCDATA())
                    text = text.trimmed();
                if (prepend) {
                    if (!logDetails.isEmpty() && (text == "Verified" || text == "Not Verified"))
                        logDetails.prepend(text + ": ");
                    else
                        logDetails = text;
                } else {
                    details.append(text).append('\n');
                }
            }
            break;
        }
        default:
            break;
        }
    }

    if (m_xmlReader.hasError()) {
        // kind of expected as we get the output piece by piece
        if (m_xmlReader.error() != QXmlStreamReader::PrematureEndOfDocumentError)
            qWarning() << m_xmlReader.error() << m_xmlReader.errorString();
    }
}

} // namespace Internal
} // namespace Squish