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
|
/****************************************************************************
**
** 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 "autotestconstants.h"
#include "autotest_utils.h"
#include "testcodeparser.h"
#include "testframeworkmanager.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/futureprogress.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <cpptools/cpptoolsconstants.h>
#include <cpptools/cppmodelmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/session.h>
#include <qmljstools/qmljsmodelmanager.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <QDirIterator>
#include <QFuture>
#include <QFutureInterface>
#include <QLoggingCategory>
#include <QTimer>
static Q_LOGGING_CATEGORY(LOG, "qtc.autotest.testcodeparser")
namespace Autotest {
namespace Internal {
TestCodeParser::TestCodeParser(TestTreeModel *parent)
: QObject(parent),
m_model(parent),
m_codeModelParsing(false),
m_fullUpdatePostponed(false),
m_partialUpdatePostponed(false),
m_dirty(false),
m_singleShotScheduled(false),
m_parserState(Disabled)
{
// connect to ProgressManager to postpone test parsing when CppModelManager is parsing
auto progressManager = qobject_cast<Core::ProgressManager *>(Core::ProgressManager::instance());
connect(progressManager, &Core::ProgressManager::taskStarted,
this, &TestCodeParser::onTaskStarted);
connect(progressManager, &Core::ProgressManager::allTasksFinished,
this, &TestCodeParser::onAllTasksFinished);
connect(&m_futureWatcher, &QFutureWatcher<TestParseResultPtr>::started,
this, &TestCodeParser::parsingStarted);
connect(&m_futureWatcher, &QFutureWatcher<TestParseResultPtr>::finished,
this, &TestCodeParser::onFinished);
connect(&m_futureWatcher, &QFutureWatcher<TestParseResultPtr>::resultReadyAt,
this, [this] (int index) {
emit testParseResultReady(m_futureWatcher.resultAt(index));
});
connect(this, &TestCodeParser::parsingFinished, this, &TestCodeParser::releaseParserInternals);
}
TestCodeParser::~TestCodeParser()
{
}
void TestCodeParser::setState(State state)
{
if (m_parserState == Shutdown)
return;
qCDebug(LOG) << "setState(" << state << "), currentState:" << m_parserState;
// avoid triggering parse before code model parsing has finished, but mark as dirty
if (m_codeModelParsing) {
m_dirty = true;
qCDebug(LOG) << "Not setting new state - code model parsing is running, just marking dirty";
return;
}
if ((state == Disabled || state == Idle)
&& (m_parserState == PartialParse || m_parserState == FullParse)) {
qCDebug(LOG) << "Not setting state, parse is running";
return;
}
m_parserState = state;
if (m_parserState == Disabled) {
m_fullUpdatePostponed = m_partialUpdatePostponed = false;
m_postponedFiles.clear();
} else if (m_parserState == Idle && ProjectExplorer::SessionManager::startupProject()) {
if (m_fullUpdatePostponed || m_dirty) {
emitUpdateTestTree();
} else if (m_partialUpdatePostponed) {
m_partialUpdatePostponed = false;
qCDebug(LOG) << "calling scanForTests with postponed files (setState)";
scanForTests(m_postponedFiles.toList());
}
}
}
void TestCodeParser::syncTestFrameworks(const QVector<Core::Id> &frameworkIds)
{
if (m_parserState != Disabled && m_parserState != Idle) {
// there's a running parse
m_fullUpdatePostponed = m_partialUpdatePostponed = false;
m_postponedFiles.clear();
Core::ProgressManager::instance()->cancelTasks(Constants::TASK_PARSE);
}
m_testCodeParsers.clear();
TestFrameworkManager *frameworkManager = TestFrameworkManager::instance();
qCDebug(LOG) << "Setting" << frameworkIds << "as current parsers";
foreach (const Core::Id &id, frameworkIds) {
ITestParser *testParser = frameworkManager->testParserForTestFramework(id);
QTC_ASSERT(testParser, continue);
m_testCodeParsers.append(testParser);
}
if (m_parserState != Disabled)
updateTestTree();
}
void TestCodeParser::emitUpdateTestTree()
{
if (m_singleShotScheduled) {
qCDebug(LOG) << "not scheduling another updateTestTree";
return;
}
qCDebug(LOG) << "adding singleShot";
m_singleShotScheduled = true;
QTimer::singleShot(1000, this, &TestCodeParser::updateTestTree);
}
void TestCodeParser::updateTestTree()
{
m_singleShotScheduled = false;
if (m_codeModelParsing) {
m_fullUpdatePostponed = true;
m_partialUpdatePostponed = false;
m_postponedFiles.clear();
return;
}
if (!ProjectExplorer::SessionManager::startupProject())
return;
m_fullUpdatePostponed = false;
emit aboutToPerformFullParse();
qCDebug(LOG) << "calling scanForTests (updateTestTree)";
scanForTests();
}
// used internally to indicate a parse that failed due to having triggered a parse for a file that
// is not (yet) part of the CppModelManager's snapshot
static bool parsingHasFailed;
static bool checkDocumentForTestCode(QFutureInterface<TestParseResultPtr> &futureInterface,
const QString &fileName,
const QVector<ITestParser *> &parsers)
{
foreach (ITestParser *currentParser, parsers) {
if (futureInterface.isCanceled())
return false;
if (currentParser->processDocument(futureInterface, fileName))
return true;
}
return false;
}
static void performParse(QFutureInterface<TestParseResultPtr> &futureInterface,
const QStringList &list, const QVector<ITestParser *> &parsers)
{
int progressValue = 0;
futureInterface.setProgressRange(0, list.size());
futureInterface.setProgressValue(progressValue);
foreach (const QString &file, list) {
if (futureInterface.isCanceled())
return;
futureInterface.setProgressValue(++progressValue);
if (!checkDocumentForTestCode(futureInterface, file, parsers)) {
parsingHasFailed |= !CppTools::CppModelManager::instance()->snapshot().contains(file)
&& (CppTools::ProjectFile::classify(file) != CppTools::ProjectFile::Unclassified);
}
}
futureInterface.setProgressValue(list.size());
}
/****** threaded parsing stuff *******/
void TestCodeParser::onDocumentUpdated(const QString &fileName)
{
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return;
if (!project->files(ProjectExplorer::Project::SourceFiles).contains(fileName))
return;
if (m_codeModelParsing) {
if (!m_fullUpdatePostponed) {
m_partialUpdatePostponed = true;
m_postponedFiles.insert(fileName);
}
return;
}
scanForTests(QStringList(fileName));
}
void TestCodeParser::onCppDocumentUpdated(const CPlusPlus::Document::Ptr &document)
{
onDocumentUpdated(document->fileName());
}
void TestCodeParser::onQmlDocumentUpdated(const QmlJS::Document::Ptr &document)
{
const QString fileName = document->fileName();
if (!fileName.endsWith(".qbs"))
onDocumentUpdated(fileName);
}
void TestCodeParser::onStartupProjectChanged(ProjectExplorer::Project *project)
{
if (m_parserState == FullParse || m_parserState == PartialParse) {
qCDebug(LOG) << "Canceling scanForTest (startup project changed)";
Core::ProgressManager::instance()->cancelTasks(Constants::TASK_PARSE);
}
if (project)
emitUpdateTestTree();
}
void TestCodeParser::onProjectPartsUpdated(ProjectExplorer::Project *project)
{
if (project != ProjectExplorer::SessionManager::startupProject())
return;
if (m_codeModelParsing || m_parserState == Disabled)
m_fullUpdatePostponed = true;
else
emitUpdateTestTree();
}
void TestCodeParser::aboutToShutdown()
{
qCDebug(LOG) << "Disabling (immediately) - shutting down";
State oldState = m_parserState;
m_parserState = Shutdown;
if (oldState == PartialParse || oldState == FullParse) {
m_futureWatcher.cancel();
m_futureWatcher.waitForFinished();
}
}
bool TestCodeParser::postponed(const QStringList &fileList)
{
switch (m_parserState) {
case Idle:
return false;
case PartialParse:
case FullParse:
// parse is running, postponing a full parse
if (fileList.isEmpty()) {
m_partialUpdatePostponed = false;
m_postponedFiles.clear();
m_fullUpdatePostponed = true;
qCDebug(LOG) << "Canceling scanForTest (full parse triggered while running a scan)";
Core::ProgressManager::instance()->cancelTasks(Constants::TASK_PARSE);
} else {
// partial parse triggered, but full parse is postponed already, ignoring this
if (m_fullUpdatePostponed)
return true;
// partial parse triggered, postpone or add current files to already postponed partial
foreach (const QString &file, fileList)
m_postponedFiles.insert(file);
m_partialUpdatePostponed = true;
}
return true;
case Disabled:
case Shutdown:
break;
}
QTC_ASSERT(false, return false); // should not happen at all
}
void TestCodeParser::scanForTests(const QStringList &fileList)
{
if (m_parserState == Disabled || m_parserState == Shutdown) {
m_dirty = true;
if (fileList.isEmpty()) {
m_fullUpdatePostponed = true;
m_partialUpdatePostponed = false;
m_postponedFiles.clear();
} else if (!m_fullUpdatePostponed) {
m_partialUpdatePostponed = true;
foreach (const QString &file, fileList)
m_postponedFiles.insert(file);
}
return;
}
if (postponed(fileList))
return;
m_postponedFiles.clear();
bool isFullParse = fileList.isEmpty();
QStringList list;
if (isFullParse) {
list = ProjectExplorer::SessionManager::startupProject()->files(ProjectExplorer::Project::SourceFiles);
if (list.isEmpty())
return;
qCDebug(LOG) << "setting state to FullParse (scanForTests)";
m_parserState = FullParse;
} else {
list << fileList;
qCDebug(LOG) << "setting state to PartialParse (scanForTests)";
m_parserState = PartialParse;
}
parsingHasFailed = false;
if (isFullParse) {
// remove qml files as they will be found automatically by the referencing cpp file
list = Utils::filtered(list, [] (const QString &fn) {
return !fn.endsWith(QLatin1String(".qml"));
});
m_model->markAllForRemoval();
} else {
foreach (const QString &filePath, list)
m_model->markForRemoval(filePath);
}
foreach (ITestParser *parser, m_testCodeParsers)
parser->init(list);
QFuture<TestParseResultPtr> future = Utils::runAsync(&performParse, list, m_testCodeParsers);
m_futureWatcher.setFuture(future);
if (list.size() > 5) {
Core::ProgressManager::addTask(future, tr("Scanning for Tests"),
Autotest::Constants::TASK_PARSE);
}
}
void TestCodeParser::onTaskStarted(Core::Id type)
{
if (type == CppTools::Constants::TASK_INDEX) {
m_codeModelParsing = true;
if (m_parserState == FullParse || m_parserState == PartialParse) {
m_fullUpdatePostponed = m_parserState == FullParse;
m_partialUpdatePostponed = !m_fullUpdatePostponed;
qCDebug(LOG) << "Canceling scan for test (CppModelParsing started)";
Core::ProgressManager::instance()->cancelTasks(Constants::TASK_PARSE);
}
}
}
void TestCodeParser::onAllTasksFinished(Core::Id type)
{
// only CPP parsing is relevant as we trigger Qml parsing internally anyway
if (type != CppTools::Constants::TASK_INDEX)
return;
m_codeModelParsing = false;
// avoid illegal parser state if respective widgets became hidden while parsing
setState(Idle);
}
void TestCodeParser::onFinished()
{
switch (m_parserState) {
case PartialParse:
qCDebug(LOG) << "setting state to Idle (onFinished, PartialParse)";
m_parserState = Idle;
onPartialParsingFinished();
break;
case FullParse:
qCDebug(LOG) << "setting state to Idle (onFinished, FullParse)";
m_parserState = Idle;
m_dirty = parsingHasFailed;
if (m_partialUpdatePostponed || m_fullUpdatePostponed || parsingHasFailed) {
onPartialParsingFinished();
} else {
qCDebug(LOG) << "emitting parsingFinished"
<< "(onFinished, FullParse, nothing postponed, parsing succeeded)";
emit parsingFinished();
}
m_dirty = false;
break;
case Disabled: // can happen if all Test related widgets become hidden while parsing
qCDebug(LOG) << "emitting parsingFinished (onFinished, Disabled)";
emit parsingFinished();
break;
case Shutdown:
qCDebug(LOG) << "Shutdown complete - not emitting parsingFinished (onFinished)";
break;
default:
qWarning("I should not be here... State: %d", m_parserState);
break;
}
}
void TestCodeParser::onPartialParsingFinished()
{
QTC_ASSERT(m_fullUpdatePostponed != m_partialUpdatePostponed
|| ((m_fullUpdatePostponed || m_partialUpdatePostponed) == false),
m_partialUpdatePostponed = false;m_postponedFiles.clear(););
if (m_fullUpdatePostponed) {
m_fullUpdatePostponed = false;
qCDebug(LOG) << "calling updateTestTree (onPartialParsingFinished)";
updateTestTree();
} else if (m_partialUpdatePostponed) {
m_partialUpdatePostponed = false;
qCDebug(LOG) << "calling scanForTests with postponed files (onPartialParsingFinished)";
scanForTests(m_postponedFiles.toList());
} else {
m_dirty |= m_codeModelParsing;
if (m_dirty) {
emit parsingFailed();
} else if (!m_singleShotScheduled) {
qCDebug(LOG) << "emitting parsingFinished"
<< "(onPartialParsingFinished, nothing postponed, not dirty)";
emit parsingFinished();
} else {
qCDebug(LOG) << "not emitting parsingFinished"
<< "(on PartialParsingFinished, singleshot scheduled)";
}
}
}
void TestCodeParser::releaseParserInternals()
{
for (ITestParser *parser : m_testCodeParsers)
parser->release();
}
} // namespace Internal
} // namespace Autotest
|