Fix plugin unit tests when using projects

The ClangTools and the AutoTest plugins use an internal
mechanism to load and configure a project when performing
their integrated unit tests.
Both assumed to have exactly one kit present for these
tests.
Make it possible to have more kits present when starting
with existing settings or if more kits get automatically
generated when starting with clean settings.

Change-Id: If2bc66320c4854f1d34a19d17107e8f0b7d64d39
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Christian Stenger
2020-06-16 13:11:10 +02:00
parent c6db1c29d6
commit 6e798401a0
15 changed files with 65 additions and 29 deletions

View File

@@ -61,13 +61,20 @@ AutoTestUnitTests::AutoTestUnitTests(TestTreeModel *model, QObject *parent)
void AutoTestUnitTests::initTestCase()
{
const QList<Kit *> allKits = KitManager::kits();
if (allKits.count() != 1)
QSKIP("This test requires exactly one kit to be present");
if (auto qtVersion = QtSupport::QtKitAspect::qtVersion(allKits.first()))
if (allKits.count() == 0)
QSKIP("This test requires at least one kit to be present");
m_kit = findOr(allKits, nullptr, [](Kit *k) {
return k->isValid() && QtSupport::QtKitAspect::qtVersion(k) != nullptr;
});
if (!m_kit)
QSKIP("The test requires at least one valid kit with a valid Qt");
if (auto qtVersion = QtSupport::QtKitAspect::qtVersion(m_kit))
m_isQt4 = qtVersion->qtVersionString().startsWith('4');
else
QSKIP("Could not figure out which Qt version is used for default kit.");
const ToolChain * const toolchain = ToolChainKitAspect::cxxToolChain(allKits.first());
const ToolChain * const toolchain = ToolChainKitAspect::cxxToolChain(m_kit);
if (!toolchain)
QSKIP("This test requires that there is a kit with a toolchain.");
@@ -99,7 +106,7 @@ void AutoTestUnitTests::testCodeParser()
QFETCH(int, expectedDataTagsCount);
CppTools::Tests::ProjectOpenerAndCloser projectManager;
const CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
const CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true, m_kit);
QVERIFY(projectInfo.isValid());
QSignalSpy parserSpy(m_model->parser(), SIGNAL(parsingFinished()));
@@ -150,7 +157,7 @@ void AutoTestUnitTests::testCodeParserSwitchStartup()
CppTools::Tests::ProjectOpenerAndCloser projectManager;
for (int i = 0; i < projectFilePaths.size(); ++i) {
qDebug() << "Opening project" << projectFilePaths.at(i);
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePaths.at(i), true);
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePaths.at(i), true, m_kit);
QVERIFY(projectInfo.isValid());
QSignalSpy parserSpy(m_model->parser(), SIGNAL(parsingFinished()));
@@ -198,7 +205,7 @@ void AutoTestUnitTests::testCodeParserGTest()
QFETCH(QString, projectFilePath);
CppTools::Tests::ProjectOpenerAndCloser projectManager;
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true, m_kit);
QVERIFY(projectInfo.isValid());
QSignalSpy parserSpy(m_model->parser(), SIGNAL(parsingFinished()));
@@ -247,7 +254,7 @@ void AutoTestUnitTests::testCodeParserBoostTest()
QFETCH(QString, projectFilePath);
QFETCH(QString, extension);
CppTools::Tests::ProjectOpenerAndCloser projectManager;
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true, m_kit);
QVERIFY(projectInfo.isValid());
QSignalSpy parserSpy(m_model->parser(), SIGNAL(parsingFinished()));

View File

@@ -28,6 +28,7 @@
#include <QObject>
namespace CppTools { namespace Tests { class TemporaryCopiedDir; } }
namespace ProjectExplorer { class Kit; }
namespace Autotest {
@@ -60,6 +61,7 @@ private:
CppTools::Tests::TemporaryCopiedDir *m_tmpDir = nullptr;
bool m_isQt4 = false;
bool m_checkBoost = false;
ProjectExplorer::Kit *m_kit = nullptr;
};
} // namespace Internal

View File

@@ -235,7 +235,7 @@ bool OpenProjectCommand::run()
QTC_ASSERT(openProjectSucceeded, return false);
Project *project = openProjectSucceeded.project();
project->configureAsExampleProject();
project->configureAsExampleProject(nullptr);
return CppTools::Tests::TestCase::waitUntilProjectIsFullyOpened(project, timeOutInMs());
}

View File

@@ -40,6 +40,8 @@
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/toolchain.h>
#include <qtsupport/qtkitinformation.h>
#include <utils/executeondestruction.h>
#include <utils/fileutils.h>
@@ -60,9 +62,16 @@ namespace Internal {
void ClangToolsUnitTests::initTestCase()
{
const QList<Kit *> allKits = KitManager::kits();
if (allKits.count() != 1)
QSKIP("This test requires exactly one kit to be present");
const ToolChain *const toolchain = ToolChainKitAspect::cxxToolChain(allKits.first());
if (allKits.count() == 0)
QSKIP("This test requires at least one kit to be present");
m_kit = findOr(allKits, nullptr, [](Kit *k) {
return k->isValid() && QtSupport::QtKitAspect::qtVersion(k) != nullptr;
});
if (!m_kit)
QSKIP("This test requires at least one valid kit with a valid Qt");
const ToolChain *const toolchain = ToolChainKitAspect::cxxToolChain(m_kit);
if (!toolchain)
QSKIP("This test requires that there is a kit with a toolchain.");
@@ -99,14 +108,14 @@ void ClangToolsUnitTests::testProject()
QFETCH(int, expectedDiagCount);
QFETCH(ClangDiagnosticConfig, diagnosticConfig);
if (projectFilePath.contains("mingw")) {
const auto toolchain = ToolChainKitAspect::cxxToolChain(KitManager::kits().constFirst());
const auto toolchain = ToolChainKitAspect::cxxToolChain(m_kit);
if (toolchain->typeId() != ProjectExplorer::Constants::MINGW_TOOLCHAIN_TYPEID)
QSKIP("This test is mingw specific, does not run for other toolchains");
}
// Open project
Tests::ProjectOpenerAndCloser projectManager;
const ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
const ProjectInfo projectInfo = projectManager.open(projectFilePath, true, m_kit);
const bool isProjectOpen = projectInfo.isValid();
QVERIFY(isProjectOpen);

View File

@@ -32,6 +32,8 @@ class ClangDiagnosticConfig;
namespace Tests { class TemporaryCopiedDir; }
} // namespace CppTools
namespace ProjectExplorer { class Kit; }
namespace ClangTools {
namespace Internal {
@@ -55,6 +57,7 @@ private:
private:
CppTools::Tests::TemporaryCopiedDir *m_tmpDir = nullptr;
ProjectExplorer::Kit *m_kit = nullptr;
};
} // namespace Internal

View File

@@ -445,9 +445,11 @@ Utils::FilePath CompilationDatabaseProject::rootPathFromSettings() const
#endif
}
void CompilationDatabaseProject::configureAsExampleProject()
void CompilationDatabaseProject::configureAsExampleProject(Kit *kit)
{
if (KitManager::defaultKit())
if (kit)
addTargetForKit(kit);
else if (KitManager::defaultKit())
addTargetForKit(KitManager::defaultKit());
}

View File

@@ -54,7 +54,7 @@ public:
Utils::FilePath rootPathFromSettings() const;
private:
void configureAsExampleProject() override;
void configureAsExampleProject(ProjectExplorer::Kit *kit) override;
};
class CompilationDatabaseBuildSystem : public ProjectExplorer::BuildSystem

View File

@@ -277,7 +277,8 @@ ProjectOpenerAndCloser::~ProjectOpenerAndCloser()
QCoreApplication::processEvents();
}
ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile, bool configureAsExampleProject)
ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile, bool configureAsExampleProject,
Kit *kit)
{
ProjectExplorerPlugin::OpenProjectResult result = ProjectExplorerPlugin::openProject(projectFile);
if (!result) {
@@ -287,7 +288,7 @@ ProjectInfo ProjectOpenerAndCloser::open(const QString &projectFile, bool config
Project *project = result.project();
if (configureAsExampleProject)
project->configureAsExampleProject();
project->configureAsExampleProject(kit);
if (TestCase::waitUntilProjectIsFullyOpened(project)) {
m_openProjects.append(project);

View File

@@ -38,7 +38,10 @@ class Snapshot;
}
namespace Core { class IEditor; }
namespace ProjectExplorer { class Project; }
namespace ProjectExplorer {
class Kit;
class Project;
}
namespace TextEditor {
class BaseTextEditor;
@@ -118,7 +121,8 @@ public:
ProjectOpenerAndCloser();
~ProjectOpenerAndCloser(); // Closes opened projects
ProjectInfo open(const QString &projectFile, bool configureAsExampleProject = false);
ProjectInfo open(const QString &projectFile, bool configureAsExampleProject = false,
ProjectExplorer::Kit *kit = nullptr);
private:
QList<ProjectExplorer::Project *> m_openProjects;

View File

@@ -910,7 +910,7 @@ bool Project::needsBuildConfigurations() const
return d->m_needsBuildConfigurations;
}
void Project::configureAsExampleProject()
void Project::configureAsExampleProject(Kit * /*kit*/)
{
}

View File

@@ -138,7 +138,7 @@ public:
virtual bool needsConfiguration() const;
bool needsBuildConfigurations() const;
virtual void configureAsExampleProject();
virtual void configureAsExampleProject(ProjectExplorer::Kit *kit);
virtual ProjectImporter *projectImporter() const;

View File

@@ -139,10 +139,14 @@ ProjectImporter *QbsProject::projectImporter() const
return m_importer;
}
void QbsProject::configureAsExampleProject()
void QbsProject::configureAsExampleProject(Kit *kit)
{
QList<BuildInfo> infoList;
const QList<Kit *> kits = KitManager::kits();
QList<Kit *> kits;
if (kit)
kits.append(kit);
else
kits = KitManager::kits();
for (Kit *k : kits) {
if (QtSupport::QtKitAspect::qtVersion(k) != nullptr) {
if (auto factory = BuildConfigurationFactory::find(k, projectFilePath()))

View File

@@ -64,7 +64,7 @@ public:
ProjectExplorer::DeploymentKnowledge deploymentKnowledge() const override;
void configureAsExampleProject() final;
void configureAsExampleProject(ProjectExplorer::Kit *kit) final;
private:
mutable ProjectExplorer::ProjectImporter *m_importer = nullptr;

View File

@@ -1012,10 +1012,14 @@ void CentralizedFolderWatcher::delayedFolderChanged(const QString &folder)
m_buildSystem->updateCodeModels();
}
void QmakeProject::configureAsExampleProject()
void QmakeProject::configureAsExampleProject(Kit *kit)
{
QList<BuildInfo> infoList;
const QList<Kit *> kits = KitManager::kits();
QList<Kit *> kits;
if (kit)
kits.append(kit);
else
kits = KitManager::kits();
for (Kit *k : kits) {
if (QtSupport::QtKitAspect::qtVersion(k) != nullptr) {
if (auto factory = BuildConfigurationFactory::find(k, projectFilePath()))

View File

@@ -63,7 +63,7 @@ public:
ProjectExplorer::Tasks projectIssues(const ProjectExplorer::Kit *k) const final;
void configureAsExampleProject() final;
void configureAsExampleProject(ProjectExplorer::Kit *kit) final;
ProjectExplorer::ProjectImporter *projectImporter() const final;