2016-07-08 12:07:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2018-05-08 17:33:29 +02:00
|
|
|
#include "clangtoolspreconfiguredsessiontests.h"
|
2016-07-08 12:07:53 +02:00
|
|
|
|
2018-01-17 15:08:30 +01:00
|
|
|
#include "clangtoolsdiagnostic.h"
|
2018-05-09 09:22:01 +02:00
|
|
|
#include "clangtidyclazytool.h"
|
2018-01-17 15:08:30 +01:00
|
|
|
#include "clangtoolsutils.h"
|
2016-07-08 12:07:53 +02:00
|
|
|
|
2018-04-30 15:26:36 +02:00
|
|
|
#include <cpptools/compileroptionsbuilder.h>
|
2016-07-08 12:07:53 +02:00
|
|
|
#include <cpptools/projectinfo.h>
|
|
|
|
|
#include <projectexplorer/kitinformation.h>
|
|
|
|
|
#include <projectexplorer/kitmanager.h>
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
#include <projectexplorer/toolchain.h>
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
|
|
|
|
#include <QSignalSpy>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QtTest>
|
2016-10-20 13:18:54 +02:00
|
|
|
#include <QVariant>
|
2016-07-08 12:07:53 +02:00
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
|
|
|
|
static bool processEventsUntil(const std::function<bool()> condition, int timeOutInMs = 30000)
|
|
|
|
|
{
|
|
|
|
|
QTime t;
|
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
|
|
forever {
|
|
|
|
|
if (t.elapsed() > timeOutInMs)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (condition())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 13:18:54 +02:00
|
|
|
class WaitForParsedProjects : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
2017-02-22 11:53:09 +01:00
|
|
|
WaitForParsedProjects(const QStringList &projects)
|
|
|
|
|
: m_projectsToWaitFor(projects)
|
2016-10-20 13:18:54 +02:00
|
|
|
{
|
2017-02-22 11:53:09 +01:00
|
|
|
connect(SessionManager::instance(),
|
|
|
|
|
&ProjectExplorer::SessionManager::projectFinishedParsing,
|
2016-10-20 13:18:54 +02:00
|
|
|
this, &WaitForParsedProjects::onProjectFinishedParsing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onProjectFinishedParsing(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
m_projectsToWaitFor.removeOne(project->projectFilePath().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wait()
|
|
|
|
|
{
|
|
|
|
|
return processEventsUntil([this]() {
|
|
|
|
|
return m_projectsToWaitFor.isEmpty();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QStringList m_projectsToWaitFor;
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-14 12:58:12 +01:00
|
|
|
namespace ClangTools {
|
2016-07-08 12:07:53 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2018-05-08 17:33:29 +02:00
|
|
|
void PreconfiguredSessionTests::initTestCase()
|
2016-07-08 12:07:53 +02:00
|
|
|
{
|
2018-05-08 17:33:29 +02:00
|
|
|
const QString preconfiguredSessionName = QLatin1String("ClangToolsTest");
|
2017-02-22 11:53:09 +01:00
|
|
|
if (!SessionManager::sessions().contains(preconfiguredSessionName))
|
2018-05-08 17:33:29 +02:00
|
|
|
QSKIP("Manually preconfigured session 'ClangToolsTest' needed.");
|
2016-07-08 12:07:53 +02:00
|
|
|
|
2017-02-22 11:53:09 +01:00
|
|
|
if (SessionManager::activeSession() == preconfiguredSessionName)
|
2016-10-20 13:18:54 +02:00
|
|
|
QSKIP("Session must not be already active.");
|
2016-07-08 12:07:53 +02:00
|
|
|
|
2016-10-20 13:18:54 +02:00
|
|
|
// Load session
|
2017-02-22 11:53:09 +01:00
|
|
|
const QStringList projects = SessionManager::projectsForSessionName(preconfiguredSessionName);
|
|
|
|
|
WaitForParsedProjects waitForParsedProjects(projects);
|
|
|
|
|
QVERIFY(SessionManager::loadSession(preconfiguredSessionName));
|
2016-10-20 13:18:54 +02:00
|
|
|
QVERIFY(waitForParsedProjects.wait());
|
2016-07-08 12:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-08 17:33:29 +02:00
|
|
|
void PreconfiguredSessionTests::testPreconfiguredSession()
|
2016-07-08 12:07:53 +02:00
|
|
|
{
|
|
|
|
|
QFETCH(Project *, project);
|
|
|
|
|
QFETCH(Target *, target);
|
|
|
|
|
|
|
|
|
|
QVERIFY(switchToProjectAndTarget(project, target));
|
|
|
|
|
|
2018-05-09 09:22:01 +02:00
|
|
|
ClangTidyClazyTool::instance()->startTool(false);
|
|
|
|
|
QSignalSpy waitUntilAnalyzerFinished(ClangTidyClazyTool::instance(), SIGNAL(finished(bool)));
|
2016-07-08 12:07:53 +02:00
|
|
|
QVERIFY(waitUntilAnalyzerFinished.wait(30000));
|
|
|
|
|
const QList<QVariant> arguments = waitUntilAnalyzerFinished.takeFirst();
|
2016-10-10 17:10:32 +02:00
|
|
|
const bool analyzerFinishedSuccessfully = arguments.first().toBool();
|
|
|
|
|
QVERIFY(analyzerFinishedSuccessfully);
|
2018-05-09 09:22:01 +02:00
|
|
|
QCOMPARE(ClangTidyClazyTool::instance()->diagnostics().count(), 0);
|
2016-07-08 12:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<Project *> validProjects(const QList<Project *> projectsOfSession)
|
|
|
|
|
{
|
|
|
|
|
QList<Project *> sortedProjects = projectsOfSession;
|
|
|
|
|
Utils::sort(sortedProjects, [](Project *lhs, Project *rhs){
|
|
|
|
|
return lhs->displayName() < rhs->displayName();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const auto isValidProject = [](Project *project) {
|
|
|
|
|
const QList <Target *> targets = project->targets();
|
|
|
|
|
if (targets.isEmpty()) {
|
|
|
|
|
qWarning("Skipping project \"%s\" since it has no targets.",
|
|
|
|
|
qPrintable(project->projectFilePath().fileName()));
|
|
|
|
|
}
|
|
|
|
|
return !targets.isEmpty();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Utils::filtered(sortedProjects, isValidProject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QList<Target *> validTargets(Project *project)
|
|
|
|
|
{
|
|
|
|
|
QList<Target *> sortedTargets = project->targets();
|
|
|
|
|
Utils::sort(sortedTargets, [](Target *lhs, Target *rhs){
|
|
|
|
|
return lhs->displayName() < rhs->displayName();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const QString projectFileName = project->projectFilePath().fileName();
|
|
|
|
|
const auto isValidTarget = [projectFileName](Target *target) {
|
|
|
|
|
Kit *kit = target->kit();
|
|
|
|
|
if (!kit || !kit->isValid()) {
|
|
|
|
|
qWarning("Project \"%s\": Skipping target \"%s\" since it has no (valid) kits.",
|
|
|
|
|
qPrintable(projectFileName),
|
|
|
|
|
qPrintable(target->displayName()));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-16 00:43:14 +01:00
|
|
|
const ToolChain * const toolchain = ToolChainKitInformation::toolChain(kit, ProjectExplorer::Constants::CXX_LANGUAGE_ID);
|
2016-07-08 12:07:53 +02:00
|
|
|
QTC_ASSERT(toolchain, return false);
|
2018-04-30 15:26:36 +02:00
|
|
|
|
|
|
|
|
if (CppTools::clangExecutable(CLANG_BINDIR).isEmpty()) {
|
2016-07-08 12:07:53 +02:00
|
|
|
qWarning("Project \"%s\": Skipping target \"%s\" since no suitable clang was found for the toolchain.",
|
|
|
|
|
qPrintable(projectFileName),
|
|
|
|
|
qPrintable(target->displayName()));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Utils::filtered(sortedTargets, isValidTarget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QByteArray dataTagName(Project *project, Target *target)
|
|
|
|
|
{
|
|
|
|
|
const QString projectFileName = project->projectFilePath().fileName();
|
|
|
|
|
const QString dataTagAsString = projectFileName + " -- " + target->displayName();
|
|
|
|
|
return dataTagAsString.toUtf8();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 17:33:29 +02:00
|
|
|
void PreconfiguredSessionTests::testPreconfiguredSession_data()
|
2016-07-08 12:07:53 +02:00
|
|
|
{
|
|
|
|
|
QTest::addColumn<Project *>("project");
|
|
|
|
|
QTest::addColumn<Target *>("target");
|
|
|
|
|
|
|
|
|
|
bool hasAddedTestData = false;
|
|
|
|
|
|
2017-02-22 11:53:09 +01:00
|
|
|
foreach (Project *project, validProjects(SessionManager::projects())) {
|
2016-07-08 12:07:53 +02:00
|
|
|
foreach (Target *target, validTargets(project)) {
|
|
|
|
|
hasAddedTestData = true;
|
|
|
|
|
QTest::newRow(dataTagName(project, target)) << project << target;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasAddedTestData)
|
|
|
|
|
QSKIP("Session has no valid projects/targets to test.");
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 17:33:29 +02:00
|
|
|
bool PreconfiguredSessionTests::switchToProjectAndTarget(Project *project,
|
2016-07-08 12:07:53 +02:00
|
|
|
Target *target)
|
|
|
|
|
{
|
2017-02-22 11:53:09 +01:00
|
|
|
Project * const activeProject = SessionManager::startupProject();
|
2016-07-08 12:07:53 +02:00
|
|
|
if (project == activeProject && target == activeProject->activeTarget())
|
|
|
|
|
return true; // OK, desired project/target already active.
|
|
|
|
|
|
2016-09-22 11:41:04 +02:00
|
|
|
if (project != activeProject)
|
2017-02-22 11:53:09 +01:00
|
|
|
SessionManager::setStartupProject(project);
|
2016-07-08 12:07:53 +02:00
|
|
|
|
2016-10-21 11:32:39 +02:00
|
|
|
if (target != project->activeTarget()) {
|
2016-10-20 13:18:54 +02:00
|
|
|
QSignalSpy spyFinishedParsing(ProjectExplorer::SessionManager::instance(),
|
|
|
|
|
&ProjectExplorer::SessionManager::projectFinishedParsing);
|
2017-02-22 11:53:09 +01:00
|
|
|
SessionManager::setActiveTarget(project, target, ProjectExplorer::SetActive::NoCascade);
|
2016-10-20 13:18:54 +02:00
|
|
|
QTC_ASSERT(spyFinishedParsing.wait(30000), return false);
|
2016-10-21 11:32:39 +02:00
|
|
|
|
2016-10-20 13:18:54 +02:00
|
|
|
const QVariant projectArgument = spyFinishedParsing.takeFirst().takeFirst();
|
|
|
|
|
QTC_ASSERT(projectArgument.canConvert<ProjectExplorer::Project *>(), return false);
|
|
|
|
|
|
|
|
|
|
return projectArgument.value<ProjectExplorer::Project *>() == project;
|
2016-07-08 12:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2018-03-14 12:58:12 +01:00
|
|
|
} // namespace ClangTools
|