2014-10-07 15:51:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** This file is part of Qt Creator.
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
2014-10-07 15:51:02 +02:00
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-22 10:37:55 +01:00
|
|
|
** 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.
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
2016-01-22 10:37:55 +01:00
|
|
|
** 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.
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
|
|
|
****************************************************************************/
|
2016-01-22 10:37:55 +01:00
|
|
|
|
2015-01-16 13:44:24 +01:00
|
|
|
#include "testrunner.h"
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
#include "autotestconstants.h"
|
2014-12-04 14:05:19 +01:00
|
|
|
#include "autotestplugin.h"
|
2014-10-07 15:51:02 +02:00
|
|
|
#include "testresultspane.h"
|
2014-12-04 14:05:19 +01:00
|
|
|
#include "testsettings.h"
|
2015-12-09 09:46:33 +01:00
|
|
|
#include "testoutputreader.h"
|
2014-10-07 15:51:02 +02:00
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
#include <coreplugin/progressmanager/futureprogress.h>
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
#include <projectexplorer/buildmanager.h>
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
#include <projectexplorer/projectexplorersettings.h>
|
|
|
|
|
2016-02-15 16:26:11 +01:00
|
|
|
#include <utils/runextensions.h>
|
2014-11-04 12:35:00 +01:00
|
|
|
|
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureInterface>
|
2014-10-07 15:51:02 +02:00
|
|
|
#include <QTime>
|
|
|
|
|
|
|
|
namespace Autotest {
|
|
|
|
namespace Internal {
|
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
static TestRunner *m_instance = 0;
|
2014-12-01 16:12:05 +01:00
|
|
|
|
2015-01-16 11:53:33 +01:00
|
|
|
static QString executableFilePath(const QString &command, const QProcessEnvironment &environment)
|
2014-10-07 15:51:02 +02:00
|
|
|
{
|
2015-01-16 11:53:33 +01:00
|
|
|
if (command.isEmpty())
|
2014-10-07 15:51:02 +02:00
|
|
|
return QString();
|
|
|
|
|
2015-01-16 11:53:33 +01:00
|
|
|
QFileInfo commandFileInfo(command);
|
|
|
|
if (commandFileInfo.isExecutable() && commandFileInfo.path() != QLatin1String(".")) {
|
|
|
|
return commandFileInfo.absoluteFilePath();
|
|
|
|
} else if (commandFileInfo.path() == QLatin1String(".")){
|
|
|
|
QString fullCommandFileName = command;
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (!command.endsWith(QLatin1String(".exe")))
|
|
|
|
fullCommandFileName = command + QLatin1String(".exe");
|
|
|
|
|
|
|
|
static const QString pathSeparator(QLatin1Char(';'));
|
|
|
|
#else
|
|
|
|
static const QString pathSeparator(QLatin1Char(':'));
|
|
|
|
#endif
|
|
|
|
QStringList pathList = environment.value(QLatin1String("PATH")).split(pathSeparator);
|
|
|
|
|
|
|
|
foreach (const QString &path, pathList) {
|
|
|
|
QString filePath(path + QDir::separator() + fullCommandFileName);
|
|
|
|
if (QFileInfo(filePath).isExecutable())
|
|
|
|
return commandFileInfo.absoluteFilePath();
|
|
|
|
}
|
2014-10-07 15:51:02 +02:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2015-01-16 13:44:24 +01:00
|
|
|
TestRunner *TestRunner::instance()
|
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new TestRunner;
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestRunner::TestRunner(QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
m_executingTests(false)
|
|
|
|
{
|
2016-02-25 13:02:31 +01:00
|
|
|
connect(&m_futureWatcher, &QFutureWatcher<TestResultPtr>::resultReadyAt,
|
2016-01-19 14:24:09 +01:00
|
|
|
this, [this](int index) { emit testResultReady(m_futureWatcher.resultAt(index)); });
|
2016-02-25 13:02:31 +01:00
|
|
|
connect(&m_futureWatcher, &QFutureWatcher<TestResultPtr>::finished,
|
2016-01-19 14:24:09 +01:00
|
|
|
this, &TestRunner::onFinished);
|
|
|
|
connect(this, &TestRunner::requestStopTestRun,
|
2016-02-25 13:02:31 +01:00
|
|
|
&m_futureWatcher, &QFutureWatcher<TestResultPtr>::cancel);
|
|
|
|
connect(&m_futureWatcher, &QFutureWatcher<TestResultPtr>::canceled,
|
|
|
|
this, [this]() { emit testResultReady(TestResultPtr(new FaultyTestResult(
|
2016-01-19 14:24:09 +01:00
|
|
|
Result::MessageFatal,
|
2016-02-25 13:02:31 +01:00
|
|
|
QObject::tr("Test run canceled by user."))));
|
2016-01-19 14:24:09 +01:00
|
|
|
});
|
2015-01-16 13:44:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TestRunner::~TestRunner()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_selectedTests);
|
|
|
|
m_selectedTests.clear();
|
|
|
|
m_instance = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::setSelectedTests(const QList<TestConfiguration *> &selected)
|
|
|
|
{
|
|
|
|
qDeleteAll(m_selectedTests);
|
|
|
|
m_selectedTests.clear();
|
|
|
|
m_selectedTests = selected;
|
|
|
|
}
|
|
|
|
|
2016-02-25 13:02:31 +01:00
|
|
|
static void performTestRun(QFutureInterface<TestResultPtr> &futureInterface,
|
2016-02-23 17:40:10 +01:00
|
|
|
const QList<TestConfiguration *> selectedTests,
|
|
|
|
const TestSettings &settings)
|
2014-11-04 12:35:00 +01:00
|
|
|
{
|
2016-02-23 17:40:10 +01:00
|
|
|
const int timeout = settings.timeout;
|
|
|
|
const QString &metricsOption = TestSettings::metricsTypeToOption(settings.metrics);
|
2016-01-19 12:02:07 +01:00
|
|
|
QEventLoop eventLoop;
|
2014-11-04 12:35:00 +01:00
|
|
|
int testCaseCount = 0;
|
2015-04-27 16:11:28 +02:00
|
|
|
foreach (TestConfiguration *config, selectedTests) {
|
|
|
|
config->completeTestInformation();
|
|
|
|
if (config->project()) {
|
|
|
|
testCaseCount += config->testCaseCount();
|
|
|
|
} else {
|
2016-02-25 13:02:31 +01:00
|
|
|
futureInterface.reportResult(TestResultPtr(new FaultyTestResult(Result::MessageWarn,
|
2015-04-27 16:11:28 +02:00
|
|
|
QObject::tr("Project is null for \"%1\". Removing from test run.\n"
|
2016-02-25 13:02:31 +01:00
|
|
|
"Check the test environment.").arg(config->displayName()))));
|
2015-04-27 16:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-13 11:24:41 +01:00
|
|
|
QProcess testProcess;
|
|
|
|
testProcess.setReadChannel(QProcess::StandardOutput);
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-16 13:44:24 +01:00
|
|
|
futureInterface.setProgressRange(0, testCaseCount);
|
|
|
|
futureInterface.setProgressValue(0);
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-13 11:26:13 +01:00
|
|
|
foreach (const TestConfiguration *testConfiguration, selectedTests) {
|
2016-01-20 08:26:10 +01:00
|
|
|
QScopedPointer<TestOutputReader> outputReader;
|
|
|
|
switch (testConfiguration->testType()) {
|
|
|
|
case TestTypeQt:
|
2016-01-26 17:24:11 +01:00
|
|
|
outputReader.reset(new QtTestOutputReader(futureInterface, &testProcess,
|
|
|
|
testConfiguration->buildDirectory()));
|
2016-01-20 08:26:10 +01:00
|
|
|
break;
|
|
|
|
case TestTypeGTest:
|
2016-01-26 17:24:11 +01:00
|
|
|
outputReader.reset(new GTestOutputReader(futureInterface, &testProcess,
|
|
|
|
testConfiguration->buildDirectory()));
|
2016-01-20 08:26:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-01-16 13:44:24 +01:00
|
|
|
if (futureInterface.isCanceled())
|
2014-11-04 12:35:00 +01:00
|
|
|
break;
|
2015-01-13 11:20:51 +01:00
|
|
|
|
2015-04-27 16:11:28 +02:00
|
|
|
if (!testConfiguration->project())
|
|
|
|
continue;
|
|
|
|
|
2015-01-16 11:53:33 +01:00
|
|
|
QProcessEnvironment environment = testConfiguration->environment().toProcessEnvironment();
|
|
|
|
QString commandFilePath = executableFilePath(testConfiguration->targetFile(), environment);
|
|
|
|
if (commandFilePath.isEmpty()) {
|
2016-02-25 13:02:31 +01:00
|
|
|
futureInterface.reportResult(TestResultPtr(new FaultyTestResult(Result::MessageFatal,
|
2015-04-27 16:11:28 +02:00
|
|
|
QObject::tr("Could not find command \"%1\". (%2)")
|
|
|
|
.arg(testConfiguration->targetFile())
|
2016-02-25 13:02:31 +01:00
|
|
|
.arg(testConfiguration->displayName()))));
|
2015-01-13 11:20:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:24:09 +01:00
|
|
|
if (testConfiguration->testType() == TestTypeQt) {
|
2015-12-09 09:46:33 +01:00
|
|
|
QStringList argumentList(QLatin1String("-xml"));
|
|
|
|
if (!metricsOption.isEmpty())
|
|
|
|
argumentList << metricsOption;
|
|
|
|
if (testConfiguration->testCases().count())
|
|
|
|
argumentList << testConfiguration->testCases();
|
|
|
|
testProcess.setArguments(argumentList);
|
2016-01-12 13:52:21 +01:00
|
|
|
} else { // TestTypeGTest
|
2016-02-23 17:40:10 +01:00
|
|
|
QStringList argumentList;
|
2015-12-09 13:21:23 +01:00
|
|
|
const QStringList &testSets = testConfiguration->testCases();
|
|
|
|
if (testSets.size()) {
|
|
|
|
argumentList << QLatin1String("--gtest_filter=")
|
|
|
|
+ testSets.join(QLatin1Char(':'));
|
|
|
|
}
|
2016-02-23 17:40:10 +01:00
|
|
|
if (settings.gtestRunDisabled)
|
|
|
|
argumentList << QLatin1String("--gtest_also_run_disabled_tests");
|
|
|
|
if (settings.gtestRepeat)
|
|
|
|
argumentList << QString::fromLatin1("--gtest_repeat=%1").arg(settings.gtestIterations);
|
|
|
|
if (settings.gtestShuffle) {
|
|
|
|
argumentList << QLatin1String("--gtest_shuffle");
|
|
|
|
argumentList << QString::fromLatin1("--gtest_random_seed=%1").arg(settings.gtestSeed);
|
|
|
|
}
|
|
|
|
testProcess.setArguments(argumentList);
|
2015-12-09 09:46:33 +01:00
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-16 11:53:33 +01:00
|
|
|
testProcess.setWorkingDirectory(testConfiguration->workingDirectory());
|
2015-01-27 13:29:53 +01:00
|
|
|
if (Utils::HostOsInfo::isWindowsHost())
|
|
|
|
environment.insert(QLatin1String("QT_LOGGING_TO_CONSOLE"), QLatin1String("1"));
|
2015-01-16 11:53:33 +01:00
|
|
|
testProcess.setProcessEnvironment(environment);
|
|
|
|
testProcess.setProgram(commandFilePath);
|
|
|
|
testProcess.start();
|
2015-01-13 11:20:51 +01:00
|
|
|
|
2015-01-13 11:24:41 +01:00
|
|
|
bool ok = testProcess.waitForStarted();
|
2015-01-16 11:53:33 +01:00
|
|
|
QTime executionTimer;
|
2015-01-13 11:20:51 +01:00
|
|
|
executionTimer.start();
|
2016-01-19 13:31:55 +01:00
|
|
|
bool canceledByTimeout = false;
|
2015-01-13 11:20:51 +01:00
|
|
|
if (ok) {
|
2016-01-19 13:31:55 +01:00
|
|
|
while (testProcess.state() == QProcess::Running) {
|
|
|
|
if (executionTimer.elapsed() >= timeout) {
|
|
|
|
canceledByTimeout = true;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-16 13:44:24 +01:00
|
|
|
if (futureInterface.isCanceled()) {
|
2015-01-13 11:24:41 +01:00
|
|
|
testProcess.kill();
|
|
|
|
testProcess.waitForFinished();
|
2016-01-19 14:24:09 +01:00
|
|
|
return;
|
2015-01-13 11:20:51 +01:00
|
|
|
}
|
2016-01-19 12:02:07 +01:00
|
|
|
eventLoop.processEvents();
|
2015-01-13 11:20:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 13:31:55 +01:00
|
|
|
if (canceledByTimeout) {
|
2015-01-13 11:24:41 +01:00
|
|
|
if (testProcess.state() != QProcess::NotRunning) {
|
|
|
|
testProcess.kill();
|
|
|
|
testProcess.waitForFinished();
|
2015-01-13 11:20:51 +01:00
|
|
|
}
|
2016-02-25 13:02:31 +01:00
|
|
|
futureInterface.reportResult(TestResultPtr(
|
|
|
|
new FaultyTestResult(Result::MessageFatal, QObject::tr(
|
|
|
|
"Test case canceled due to timeout. \nMaybe raise the timeout?"))));
|
2015-01-13 11:20:51 +01:00
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
2015-01-16 13:44:24 +01:00
|
|
|
futureInterface.setProgressValue(testCaseCount);
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
|
|
|
|
2015-06-29 09:22:58 +02:00
|
|
|
void TestRunner::prepareToRunTests()
|
2014-11-04 12:35:00 +01:00
|
|
|
{
|
2016-01-20 09:38:26 +01:00
|
|
|
ProjectExplorer::Internal::ProjectExplorerSettings projectExplorerSettings =
|
|
|
|
ProjectExplorer::ProjectExplorerPlugin::projectExplorerSettings();
|
|
|
|
if (projectExplorerSettings.buildBeforeDeploy && !projectExplorerSettings.saveBeforeBuild) {
|
|
|
|
if (!ProjectExplorer::ProjectExplorerPlugin::saveModifiedFiles())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-29 09:22:58 +02:00
|
|
|
const bool omitRunConfigWarnings = AutotestPlugin::instance()->settings()->omitRunConfigWarn;
|
2015-02-17 10:33:35 +01:00
|
|
|
|
2015-04-15 09:30:28 +02:00
|
|
|
m_executingTests = true;
|
|
|
|
emit testRunStarted();
|
|
|
|
|
2014-11-04 13:42:38 +01:00
|
|
|
// clear old log and output pane
|
|
|
|
TestResultsPane::instance()->clearContents();
|
|
|
|
|
2015-02-17 10:33:35 +01:00
|
|
|
foreach (TestConfiguration *config, m_selectedTests) {
|
2015-06-29 09:22:58 +02:00
|
|
|
if (!omitRunConfigWarnings && config->guessedConfiguration()) {
|
2016-02-25 13:02:31 +01:00
|
|
|
emit testResultReady(TestResultPtr(new FaultyTestResult(Result::MessageWarn,
|
2015-03-30 13:56:50 +02:00
|
|
|
tr("Project's run configuration was guessed for \"%1\".\n"
|
2016-02-25 13:02:31 +01:00
|
|
|
"This might cause trouble during execution.").arg(config->displayName()))));
|
2015-02-17 10:33:35 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-19 08:52:40 +01:00
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
if (m_selectedTests.empty()) {
|
2016-02-25 13:02:31 +01:00
|
|
|
emit testResultReady(TestResultPtr(new FaultyTestResult(Result::MessageWarn,
|
|
|
|
tr("No tests selected. Canceling test run."))));
|
2015-04-15 09:30:28 +02:00
|
|
|
onFinished();
|
2014-11-04 12:35:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectExplorer::Project *project = m_selectedTests.at(0)->project();
|
2014-11-13 12:31:58 +01:00
|
|
|
if (!project) {
|
2016-02-25 13:02:31 +01:00
|
|
|
emit testResultReady(TestResultPtr(new FaultyTestResult(Result::MessageWarn,
|
2015-03-30 13:56:50 +02:00
|
|
|
tr("Project is null. Canceling test run.\n"
|
|
|
|
"Only desktop kits are supported. Make sure the "
|
2016-02-25 13:02:31 +01:00
|
|
|
"currently active kit is a desktop kit."))));
|
2015-04-15 09:30:28 +02:00
|
|
|
onFinished();
|
2014-11-04 12:35:00 +01:00
|
|
|
return;
|
2014-11-13 12:31:58 +01:00
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-06-29 09:22:58 +02:00
|
|
|
if (!projectExplorerSettings.buildBeforeDeploy) {
|
|
|
|
runTests();
|
|
|
|
} else {
|
|
|
|
if (project->hasActiveBuildSettings()) {
|
|
|
|
buildProject(project);
|
|
|
|
} else {
|
2016-02-25 13:02:31 +01:00
|
|
|
emit testResultReady(TestResultPtr(new FaultyTestResult(Result::MessageFatal,
|
|
|
|
tr("Project is not configured. Canceling test run."))));
|
2015-04-15 09:30:28 +02:00
|
|
|
onFinished();
|
2014-11-04 12:35:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 09:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::runTests()
|
|
|
|
{
|
2016-02-25 13:02:31 +01:00
|
|
|
QFuture<TestResultPtr> future = Utils::runAsync(&performTestRun, m_selectedTests,
|
|
|
|
*AutotestPlugin::instance()->settings());
|
2016-01-19 14:24:09 +01:00
|
|
|
m_futureWatcher.setFuture(future);
|
|
|
|
Core::ProgressManager::addTask(future, tr("Running Tests"), Autotest::Constants::TASK_INDEX);
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::buildProject(ProjectExplorer::Project *project)
|
|
|
|
{
|
2015-04-15 09:30:28 +02:00
|
|
|
ProjectExplorer::BuildManager *buildManager = ProjectExplorer::BuildManager::instance();
|
2015-06-29 09:22:58 +02:00
|
|
|
m_buildConnect = connect(this, &TestRunner::requestStopTestRun,
|
|
|
|
buildManager, &ProjectExplorer::BuildManager::cancel);
|
2015-01-13 11:26:13 +01:00
|
|
|
connect(buildManager, &ProjectExplorer::BuildManager::buildQueueFinished,
|
2014-11-04 12:35:00 +01:00
|
|
|
this, &TestRunner::buildFinished);
|
2015-01-08 12:52:17 +01:00
|
|
|
ProjectExplorer::ProjectExplorerPlugin::buildProject(project);
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestRunner::buildFinished(bool success)
|
|
|
|
{
|
2015-06-29 09:22:58 +02:00
|
|
|
disconnect(m_buildConnect);
|
2015-04-15 09:30:28 +02:00
|
|
|
ProjectExplorer::BuildManager *buildManager = ProjectExplorer::BuildManager::instance();
|
2015-01-13 11:26:13 +01:00
|
|
|
disconnect(buildManager, &ProjectExplorer::BuildManager::buildQueueFinished,
|
2014-11-04 12:35:00 +01:00
|
|
|
this, &TestRunner::buildFinished);
|
2015-06-29 09:22:58 +02:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
runTests();
|
|
|
|
} else {
|
2016-02-25 13:02:31 +01:00
|
|
|
emit testResultReady(TestResultPtr(new FaultyTestResult(Result::MessageFatal,
|
|
|
|
tr("Build failed. Canceling test run."))));
|
2015-06-29 09:22:58 +02:00
|
|
|
onFinished();
|
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-06 10:48:17 +01:00
|
|
|
void TestRunner::onFinished()
|
|
|
|
{
|
|
|
|
m_executingTests = false;
|
|
|
|
emit testRunFinished();
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
} // namespace Internal
|
|
|
|
} // namespace Autotest
|