Fix getting test configuration information

This was especially wrong if the information is not available before
building the project. Now the information will be fetched after a
possible automatic build had been done.

Change-Id: If92bc714039733700885820648471e355199079d
Reviewed-by: David Schulz <david.schulz@theqtcompany.com>
This commit is contained in:
Christian Stenger
2015-04-27 16:11:28 +02:00
parent 0afd504748
commit 8260b79458
4 changed files with 155 additions and 133 deletions

View File

@@ -19,7 +19,17 @@
#include "testconfiguration.h"
#include <cpptools/cppmodelmanager.h>
#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/environmentaspect.h>
#include <projectexplorer/localapplicationrunconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
using namespace ProjectExplorer;
namespace Autotest {
namespace Internal {
@@ -34,9 +44,8 @@ TestConfiguration::TestConfiguration(const QString &testClass, const QStringList
m_project(0),
m_guessedConfiguration(false)
{
if (testCases.size() != 0) {
if (testCases.size() != 0)
m_testCaseCount = testCases.size();
}
}
TestConfiguration::~TestConfiguration()
@@ -44,6 +53,103 @@ TestConfiguration::~TestConfiguration()
m_testCases.clear();
}
void basicProjectInformation(Project *project, const QString &mainFilePath, QString *proFile,
QString *displayName, Project **targetProject)
{
CppTools::CppModelManager *cppMM = CppTools::CppModelManager::instance();
QList<CppTools::ProjectPart::Ptr> projParts = cppMM->projectInfo(project).projectParts();
foreach (const CppTools::ProjectPart::Ptr &part, projParts) {
foreach (const CppTools::ProjectFile currentFile, part->files) {
if (currentFile.path == mainFilePath) {
*proFile = part->projectFile;
*displayName = part->displayName;
*targetProject = part->project;
return;
}
}
}
}
void extractEnvironmentInformation(LocalApplicationRunConfiguration *localRunConfiguration,
QString *workDir, Utils::Environment *env)
{
*workDir = Utils::FileUtils::normalizePathName(localRunConfiguration->workingDirectory());
if (auto environmentAspect = localRunConfiguration->extraAspect<EnvironmentAspect>())
*env = environmentAspect->environment();
}
void TestConfiguration::completeTestInformation()
{
QTC_ASSERT(!m_mainFilePath.isEmpty(), return);
typedef LocalApplicationRunConfiguration LocalRunConfig;
Project *project = SessionManager::startupProject();
if (!project)
return;
QString targetFile;
QString targetName;
QString workDir;
QString proFile;
QString displayName;
Project *targetProject = 0;
Utils::Environment env;
bool hasDesktopTarget = false;
bool guessedRunConfiguration = false;
setProject(0);
basicProjectInformation(project, m_mainFilePath, &proFile, &displayName, &targetProject);
Target *target = project->activeTarget();
if (!target)
return;
BuildTargetInfoList appTargets = target->applicationTargets();
foreach (const BuildTargetInfo &bti, appTargets.list) {
// some project manager store line/column information as well inside ProjectPart
if (bti.isValid() && proFile.startsWith(bti.projectFilePath.toString())) {
targetFile = Utils::HostOsInfo::withExecutableSuffix(bti.targetFilePath.toString());
targetName = bti.targetName;
break;
}
}
QList<RunConfiguration *> rcs = target->runConfigurations();
foreach (RunConfiguration *rc, rcs) {
auto config = qobject_cast<LocalRunConfig *>(rc);
if (config && config->executable() == targetFile) {
extractEnvironmentInformation(config, &workDir, &env);
hasDesktopTarget = true;
break;
}
}
// if we could not figure out the run configuration
// try to use the run configuration of the parent project
if (!hasDesktopTarget && targetProject && !targetFile.isEmpty()) {
if (auto config = qobject_cast<LocalRunConfig *>(target->activeRunConfiguration())) {
extractEnvironmentInformation(config, &workDir, &env);
hasDesktopTarget = true;
guessedRunConfiguration = true;
}
}
setProFile(proFile);
setDisplayName(displayName);
if (hasDesktopTarget) {
setTargetFile(targetFile);
setTargetName(targetName);
setWorkingDirectory(workDir);
setEnvironment(env);
setProject(project);
setGuessedConfiguration(guessedRunConfiguration);
}
}
/**
* @brief sets the test cases for this test configuration.
*
@@ -64,6 +170,11 @@ void TestConfiguration::setTestCaseCount(int count)
m_testCaseCount = count;
}
void TestConfiguration::setMainFilePath(const QString &mainFile)
{
m_mainFilePath = mainFile;
}
void TestConfiguration::setTargetFile(const QString &targetFile)
{
m_targetFile = targetFile;
@@ -94,7 +205,7 @@ void TestConfiguration::setEnvironment(const Utils::Environment &env)
m_environment = env;
}
void TestConfiguration::setProject(ProjectExplorer::Project *project)
void TestConfiguration::setProject(Project *project)
{
m_project = project;
}

View File

@@ -41,8 +41,11 @@ public:
int testCaseCount = 0, QObject *parent = 0);
~TestConfiguration();
void completeTestInformation();
void setTestCases(const QStringList &testCases);
void setTestCaseCount(int count);
void setMainFilePath(const QString &mainFile);
void setTargetFile(const QString &targetFile);
void setTargetName(const QString &targetName);
void setProFile(const QString &proFile);
@@ -66,14 +69,11 @@ public:
bool unnamedOnly() const { return m_unnamedOnly; }
bool guessedConfiguration() const { return m_guessedConfiguration; }
signals:
public slots:
private:
QString m_testClass;
QStringList m_testCases;
int m_testCaseCount;
QString m_mainFilePath;
bool m_unnamedOnly;
QString m_proFile;
QString m_targetFile;

View File

@@ -110,8 +110,16 @@ void performTestRun(QFutureInterface<void> &futureInterface,
const QString metricsOption, TestRunner* testRunner)
{
int testCaseCount = 0;
foreach (const TestConfiguration *config, selectedTests)
testCaseCount += config->testCaseCount();
foreach (TestConfiguration *config, selectedTests) {
config->completeTestInformation();
if (config->project()) {
testCaseCount += config->testCaseCount();
} else {
emitTestResultCreated(FaultyTestResult(Result::MESSAGE_WARN,
QObject::tr("Project is null for \"%1\". Removing from test run.\n"
"Check the test environment.").arg(config->displayName())));
}
}
QProcess testProcess;
testProcess.setReadChannelMode(QProcess::MergedChannels);
@@ -135,11 +143,16 @@ void performTestRun(QFutureInterface<void> &futureInterface,
if (futureInterface.isCanceled())
break;
if (!testConfiguration->project())
continue;
QProcessEnvironment environment = testConfiguration->environment().toProcessEnvironment();
QString commandFilePath = executableFilePath(testConfiguration->targetFile(), environment);
if (commandFilePath.isEmpty()) {
emitTestResultCreated(FaultyTestResult(Result::MESSAGE_FATAL,
QObject::tr("Could not find command \"%1\".").arg(testConfiguration->targetFile())));
QObject::tr("Could not find command \"%1\". (%2)")
.arg(testConfiguration->targetFile())
.arg(testConfiguration->displayName())));
continue;
}
@@ -197,26 +210,13 @@ void TestRunner::runTests()
// clear old log and output pane
TestResultsPane::instance()->clearContents();
// handle faulty test configurations
QList<TestConfiguration *> toBeRemoved;
foreach (TestConfiguration *config, m_selectedTests) {
if (!config->project()) {
toBeRemoved.append(config);
TestResultsPane::instance()->addTestResult(FaultyTestResult(Result::MESSAGE_WARN,
tr("Project is null for \"%1\". Removing from test run.\n"
"Check the test environment."
).arg(config->displayName())));
}
if (displayRunConfigWarnings && config->guessedConfiguration()) {
TestResultsPane::instance()->addTestResult(FaultyTestResult(Result::MESSAGE_WARN,
tr("Project's run configuration was guessed for \"%1\".\n"
"This might cause trouble during execution.").arg(config->displayName())));
}
}
foreach (TestConfiguration *config, toBeRemoved) {
m_selectedTests.removeOne(config);
delete config;
}
if (m_selectedTests.empty()) {
TestResultsPane::instance()->addTestResult(FaultyTestResult(Result::MESSAGE_WARN,

View File

@@ -24,21 +24,14 @@
#include <cpptools/cppmodelmanager.h>
#include <projectexplorer/buildtargetinfo.h>
#include <projectexplorer/environmentaspect.h>
#include <projectexplorer/localapplicationrunconfiguration.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <texteditor/texteditor.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
#include <QIcon>
@@ -347,113 +340,22 @@ bool TestTreeModel::hasTests() const
return m_autoTestRootItem->childCount() > 0 || m_quickTestRootItem->childCount() > 0;
}
static void addProjectInformation(TestConfiguration *config, const QString &filePath)
{
const ProjectExplorer::SessionManager *session = ProjectExplorer::SessionManager::instance();
if (!session || !session->hasProjects())
return;
ProjectExplorer::Project *project = session->startupProject();
if (!project)
return;
QString targetFile;
QString targetName;
QString workDir;
QString proFile;
QString displayName;
ProjectExplorer::Project *targetProject = 0;
Utils::Environment env;
bool hasDesktopTarget = false;
bool guessedRunConfiguration = false;
CppTools::CppModelManager *cppMM = CppTools::CppModelManager::instance();
QList<CppTools::ProjectPart::Ptr> projParts = cppMM->projectInfo(project).projectParts();
if (!projParts.empty()) {
foreach (const CppTools::ProjectPart::Ptr &part, projParts) {
foreach (const CppTools::ProjectFile currentFile, part->files) {
if (currentFile.path == filePath) {
proFile = part->projectFile;
displayName = part->displayName;
targetProject = part->project;
break;
}
}
if (!proFile.isEmpty()) // maybe better use a goto instead of the break above??
break;
}
}
if (project) {
if (auto target = project->activeTarget()) {
ProjectExplorer::BuildTargetInfoList appTargets = target->applicationTargets();
foreach (const ProjectExplorer::BuildTargetInfo &bti, appTargets.list) {
if (bti.isValid() && bti.projectFilePath.toString() == proFile) {
targetFile = Utils::HostOsInfo::withExecutableSuffix(bti.targetFilePath.toString());
targetName = bti.targetName;
break;
}
}
QList<ProjectExplorer::RunConfiguration *> rcs = target->runConfigurations();
foreach (ProjectExplorer::RunConfiguration *rc, rcs) {
ProjectExplorer::LocalApplicationRunConfiguration *localRunConfiguration
= qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration *>(rc);
if (localRunConfiguration && localRunConfiguration->executable() == targetFile) {
hasDesktopTarget = true;
workDir = Utils::FileUtils::normalizePathName(
localRunConfiguration->workingDirectory());
ProjectExplorer::EnvironmentAspect *envAsp
= localRunConfiguration->extraAspect<ProjectExplorer::EnvironmentAspect>();
env = envAsp->environment();
break;
}
}
// if we could not figure out the run configuration
// try to use the run configuration of the parent project
if (!hasDesktopTarget && targetProject && !targetFile.isEmpty()) {
auto localRunConfiguration
= qobject_cast<ProjectExplorer::LocalApplicationRunConfiguration *>(target->activeRunConfiguration());
if (localRunConfiguration) {
hasDesktopTarget = true;
workDir = Utils::FileUtils::normalizePathName(
localRunConfiguration->workingDirectory());
ProjectExplorer::EnvironmentAspect *environmentAspect
= localRunConfiguration->extraAspect<ProjectExplorer::EnvironmentAspect>();
env = environmentAspect->environment();
guessedRunConfiguration = true;
}
}
}
}
if (hasDesktopTarget) {
config->setTargetFile(targetFile);
config->setTargetName(targetName);
config->setWorkingDirectory(workDir);
config->setProFile(proFile);
config->setEnvironment(env);
config->setProject(project);
config->setDisplayName(displayName);
config->setGuessedConfiguration(guessedRunConfiguration);
} else {
config->setProFile(proFile);
config->setDisplayName(displayName);
}
}
QList<TestConfiguration *> TestTreeModel::getAllTestCases() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return result;
// get all Auto Tests
for (int row = 0, count = m_autoTestRootItem->childCount(); row < count; ++row) {
const TestTreeItem *child = m_autoTestRootItem->child(row);
TestConfiguration *tc = new TestConfiguration(child->name(), QStringList(),
child->childCount());
addProjectInformation(tc, child->filePath());
tc->setMainFilePath(child->filePath());
tc->setProject(project);
result << tc;
}
@@ -481,7 +383,8 @@ QList<TestConfiguration *> TestTreeModel::getAllTestCases() const
foreach (const QString &mainFile, foundMains.keys()) {
TestConfiguration *tc = new TestConfiguration(QString(), QStringList(),
foundMains.value(mainFile));
addProjectInformation(tc, mainFile);
tc->setMainFilePath(mainFile);
tc->setProject(project);
result << tc;
}
@@ -491,6 +394,10 @@ QList<TestConfiguration *> TestTreeModel::getAllTestCases() const
QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
{
QList<TestConfiguration *> result;
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::startupProject();
if (!project)
return result;
TestConfiguration *testConfiguration = 0;
for (int row = 0, count = m_autoTestRootItem->childCount(); row < count; ++row) {
@@ -501,7 +408,8 @@ QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
continue;
case Qt::Checked:
testConfiguration = new TestConfiguration(child->name(), QStringList(), child->childCount());
addProjectInformation(testConfiguration, child->filePath());
testConfiguration->setMainFilePath(child->filePath());
testConfiguration->setProject(project);
result << testConfiguration;
continue;
case Qt::PartiallyChecked:
@@ -516,7 +424,8 @@ QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
}
testConfiguration = new TestConfiguration(childName, testCases);
addProjectInformation(testConfiguration, child->filePath());
testConfiguration->setMainFilePath(child->filePath());
testConfiguration->setProject(project);
result << testConfiguration;
}
}
@@ -540,7 +449,8 @@ QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
testConfiguration = new TestConfiguration(QString(), QStringList());
testConfiguration->setTestCaseCount(1);
testConfiguration->setUnnamedOnly(true);
addProjectInformation(testConfiguration, mainFile);
testConfiguration->setMainFilePath(mainFile);
testConfiguration->setProject(project);
foundMains.insert(mainFile, testConfiguration);
}
}
@@ -581,7 +491,8 @@ QList<TestConfiguration *> TestTreeModel::getSelectedTests() const
}
} else {
tc = new TestConfiguration(QString(), testFunctions);
addProjectInformation(tc, child->mainFile());
tc->setMainFilePath(child->mainFile());
tc->setProject(project);
foundMains.insert(child->mainFile(), tc);
}
break;