2014-10-07 15:51:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2015-02-19 08:09:02 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd
|
2014-10-07 15:51:02 +02:00
|
|
|
** All rights reserved.
|
2015-02-19 08:09:02 +01:00
|
|
|
** For any questions to The Qt Company, please use contact form at
|
|
|
|
** http://www.qt.io/contact-us
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
|
|
|
** 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
|
2015-02-19 08:09:02 +01:00
|
|
|
** a written agreement between you and The Qt Company.
|
2014-10-07 15:51:02 +02:00
|
|
|
**
|
|
|
|
** If you have questions regarding the use of this file, please use
|
2015-02-19 08:09:02 +01:00
|
|
|
** contact form at http://www.qt.io/contact-us
|
2014-10-07 15:51:02 +02: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-01-16 13:44:24 +01:00
|
|
|
#include "testxmloutputreader.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>
|
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
#include <utils/multitask.h>
|
|
|
|
|
|
|
|
#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-08-20 15:59:15 +02:00
|
|
|
void emitTestResultCreated(TestResult *testResult)
|
2015-01-08 12:51:01 +01:00
|
|
|
{
|
|
|
|
emit m_instance->testResultCreated(testResult);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-16 13:17:53 +01:00
|
|
|
void performTestRun(QFutureInterface<void> &futureInterface,
|
|
|
|
const QList<TestConfiguration *> selectedTests, const int timeout,
|
|
|
|
const QString metricsOption, TestRunner* testRunner)
|
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 {
|
2015-08-20 15:59:15 +02:00
|
|
|
emitTestResultCreated(new FaultyTestResult(Result::MESSAGE_WARN,
|
2015-04-27 16:11:28 +02:00
|
|
|
QObject::tr("Project is null for \"%1\". Removing from test run.\n"
|
|
|
|
"Check the test environment.").arg(config->displayName())));
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-13 11:24:41 +01:00
|
|
|
QProcess testProcess;
|
|
|
|
testProcess.setReadChannelMode(QProcess::MergedChannels);
|
|
|
|
testProcess.setReadChannel(QProcess::StandardOutput);
|
2015-01-16 11:53:33 +01:00
|
|
|
QObject::connect(testRunner, &TestRunner::requestStopTestRun, &testProcess, [&] () {
|
2015-01-16 13:44:24 +01:00
|
|
|
futureInterface.cancel(); // this kills the process if that is still in the running loop
|
2015-01-13 11:24:41 +01:00
|
|
|
});
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-16 13:44:24 +01:00
|
|
|
TestXmlOutputReader xmlReader(&testProcess);
|
|
|
|
QObject::connect(&xmlReader, &TestXmlOutputReader::increaseProgress, [&] () {
|
|
|
|
futureInterface.setProgressValue(futureInterface.progressValue() + 1);
|
2015-01-13 11:24:41 +01:00
|
|
|
});
|
2015-01-16 13:44:24 +01:00
|
|
|
QObject::connect(&xmlReader, &TestXmlOutputReader::testResultCreated, &emitTestResultCreated);
|
|
|
|
|
|
|
|
QObject::connect(&testProcess, &QProcess::readyRead, &xmlReader, &TestXmlOutputReader::processOutput);
|
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) {
|
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()) {
|
2015-08-20 15:59:15 +02:00
|
|
|
emitTestResultCreated(new FaultyTestResult(Result::MESSAGE_FATAL,
|
2015-04-27 16:11:28 +02:00
|
|
|
QObject::tr("Could not find command \"%1\". (%2)")
|
|
|
|
.arg(testConfiguration->targetFile())
|
|
|
|
.arg(testConfiguration->displayName())));
|
2015-01-13 11:20:51 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-16 11:53:33 +01:00
|
|
|
QStringList argumentList(QLatin1String("-xml"));
|
|
|
|
if (!metricsOption.isEmpty())
|
|
|
|
argumentList << metricsOption;
|
|
|
|
if (testConfiguration->testCases().count())
|
|
|
|
argumentList << testConfiguration->testCases();
|
|
|
|
testProcess.setArguments(argumentList);
|
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();
|
|
|
|
if (ok) {
|
2015-01-13 11:24:41 +01:00
|
|
|
while (testProcess.state() == QProcess::Running && executionTimer.elapsed() < timeout) {
|
2015-01-16 13:44:24 +01:00
|
|
|
if (futureInterface.isCanceled()) {
|
2015-01-13 11:24:41 +01:00
|
|
|
testProcess.kill();
|
|
|
|
testProcess.waitForFinished();
|
2015-08-20 15:59:15 +02:00
|
|
|
emitTestResultCreated(new FaultyTestResult(Result::MESSAGE_FATAL,
|
|
|
|
QObject::tr("Test run canceled by user.")));
|
2015-01-13 11:20:51 +01:00
|
|
|
}
|
|
|
|
qApp->processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (executionTimer.elapsed() >= timeout) {
|
2015-01-13 11:24:41 +01:00
|
|
|
if (testProcess.state() != QProcess::NotRunning) {
|
|
|
|
testProcess.kill();
|
|
|
|
testProcess.waitForFinished();
|
2015-08-20 15:59:15 +02:00
|
|
|
emitTestResultCreated(new FaultyTestResult(Result::MESSAGE_FATAL, QObject::tr(
|
2015-03-30 13:56:50 +02:00
|
|
|
"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
|
|
|
{
|
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()) {
|
2015-08-20 15:59:15 +02:00
|
|
|
TestResultsPane::instance()->addTestResult(new FaultyTestResult(Result::MESSAGE_WARN,
|
2015-03-30 13:56:50 +02:00
|
|
|
tr("Project's run configuration was guessed for \"%1\".\n"
|
2015-02-17 10:33:35 +01:00
|
|
|
"This might cause trouble during execution.").arg(config->displayName())));
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 08:52:40 +01:00
|
|
|
|
2014-11-04 12:35:00 +01:00
|
|
|
if (m_selectedTests.empty()) {
|
2015-08-20 15:59:15 +02:00
|
|
|
TestResultsPane::instance()->addTestResult(new FaultyTestResult(Result::MESSAGE_WARN,
|
2015-03-30 13:56:50 +02:00
|
|
|
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) {
|
2015-08-20 15:59:15 +02:00
|
|
|
TestResultsPane::instance()->addTestResult(new FaultyTestResult(Result::MESSAGE_WARN,
|
2015-03-30 13:56:50 +02:00
|
|
|
tr("Project is null. Canceling test run.\n"
|
|
|
|
"Only desktop kits are supported. Make sure the "
|
|
|
|
"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-01-13 11:26:13 +01:00
|
|
|
ProjectExplorer::Internal::ProjectExplorerSettings projectExplorerSettings =
|
2015-01-08 12:52:17 +01:00
|
|
|
ProjectExplorer::ProjectExplorerPlugin::projectExplorerSettings();
|
2015-06-29 09:22:58 +02:00
|
|
|
if (!projectExplorerSettings.buildBeforeDeploy) {
|
|
|
|
runTests();
|
|
|
|
} else {
|
|
|
|
if (project->hasActiveBuildSettings()) {
|
|
|
|
buildProject(project);
|
|
|
|
} else {
|
2015-08-20 15:59:15 +02:00
|
|
|
TestResultsPane::instance()->addTestResult(new FaultyTestResult(Result::MESSAGE_FATAL,
|
2015-03-30 13:56:50 +02:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
const QSharedPointer<TestSettings> settings = AutotestPlugin::instance()->settings();
|
|
|
|
const QString &metricsOption = TestSettings::metricsTypeToOption(settings->metrics);
|
2014-11-04 12:35:00 +01:00
|
|
|
|
2015-01-08 12:51:01 +01:00
|
|
|
connect(this, &TestRunner::testResultCreated,
|
|
|
|
TestResultsPane::instance(), &TestResultsPane::addTestResult,
|
|
|
|
Qt::QueuedConnection);
|
|
|
|
|
2015-06-29 09:22:58 +02:00
|
|
|
QFuture<void> future = QtConcurrent::run(&performTestRun, m_selectedTests, settings->timeout,
|
|
|
|
metricsOption, this);
|
2014-11-04 12:35:00 +01:00
|
|
|
Core::FutureProgress *progress = Core::ProgressManager::addTask(future, tr("Running Tests"),
|
|
|
|
Autotest::Constants::TASK_INDEX);
|
|
|
|
connect(progress, &Core::FutureProgress::finished,
|
2014-11-06 10:48:17 +01:00
|
|
|
TestRunner::instance(), &TestRunner::onFinished);
|
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 {
|
2015-08-20 15:59:15 +02:00
|
|
|
TestResultsPane::instance()->addTestResult(new FaultyTestResult(Result::MESSAGE_FATAL,
|
2015-06-29 09:22:58 +02:00
|
|
|
tr("Build failed. Canceling test run.")));
|
|
|
|
onFinished();
|
|
|
|
}
|
2014-11-04 12:35:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-06 10:48:17 +01:00
|
|
|
void TestRunner::onFinished()
|
|
|
|
{
|
2015-01-08 12:51:01 +01:00
|
|
|
disconnect(this, &TestRunner::testResultCreated,
|
|
|
|
TestResultsPane::instance(), &TestResultsPane::addTestResult);
|
|
|
|
|
2014-11-06 10:48:17 +01:00
|
|
|
m_executingTests = false;
|
|
|
|
emit testRunFinished();
|
|
|
|
}
|
|
|
|
|
2014-10-07 15:51:02 +02:00
|
|
|
} // namespace Internal
|
|
|
|
} // namespace Autotest
|