1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "compilationdatabasetests.h"
27 
28 #include <coreplugin/icore.h>
29 #include <cpptools/cpptoolstestcase.h>
30 #include <cpptools/projectinfo.h>
31 #include <projectexplorer/kitinformation.h>
32 #include <projectexplorer/kitmanager.h>
33 #include <projectexplorer/projectexplorer.h>
34 #include <projectexplorer/toolchain.h>
35 #include <projectexplorer/toolchainmanager.h>
36 #include <utils/algorithm.h>
37 
38 #include <QtTest>
39 
40 using namespace ProjectExplorer;
41 
42 namespace CompilationDatabaseProjectManager {
43 
CompilationDatabaseTests(QObject * parent)44 CompilationDatabaseTests::CompilationDatabaseTests(QObject *parent)
45     : QObject(parent)
46 {}
47 
48 CompilationDatabaseTests::~CompilationDatabaseTests() = default;
49 
initTestCase()50 void CompilationDatabaseTests::initTestCase()
51 {
52     const QList<Kit *> allKits = KitManager::kits();
53     if (allKits.empty())
54         QSKIP("This test requires at least one kit to be present.");
55 
56     ToolChain *toolchain = ToolChainManager::toolChain([](const ToolChain *tc) {
57         return tc->isValid() && tc->language() == Constants::CXX_LANGUAGE_ID;
58     });
59     if (!toolchain)
60         QSKIP("This test requires that there is at least one C++ toolchain present.");
61 
62     m_tmpDir = std::make_unique<CppTools::Tests::TemporaryCopiedDir>(":/database_samples");
63     QVERIFY(m_tmpDir->isValid());
64 }
65 
cleanupTestCase()66 void CompilationDatabaseTests::cleanupTestCase()
67 {
68     m_tmpDir.reset();
69 }
70 
testProject()71 void CompilationDatabaseTests::testProject()
72 {
73     QFETCH(QString, projectFilePath);
74 
75     CppTools::Tests::ProjectOpenerAndCloser projectManager;
76     const CppTools::ProjectInfo projectInfo = projectManager.open(projectFilePath, true);
77     QVERIFY(projectInfo.isValid());
78 
79     projectInfo.projectParts();
80     QVector<CppTools::ProjectPart::Ptr> projectParts = projectInfo.projectParts();
81     QVERIFY(!projectParts.isEmpty());
82 
83     CppTools::ProjectPart &projectPart = *projectParts.first();
84     QVERIFY(!projectPart.headerPaths.isEmpty());
85     QVERIFY(!projectPart.projectMacros.isEmpty());
86     QVERIFY(!projectPart.toolChainMacros.isEmpty());
87     QVERIFY(!projectPart.files.isEmpty());
88 }
89 
testProject_data()90 void CompilationDatabaseTests::testProject_data()
91 {
92     QTest::addColumn<QString>("projectFilePath");
93 
94     addTestRow("qtc/compile_commands.json");
95     addTestRow("llvm/compile_commands.json");
96 }
97 
addTestRow(const QByteArray & relativeFilePath)98 void CompilationDatabaseTests::addTestRow(const QByteArray &relativeFilePath)
99 {
100     const QString absoluteFilePath = m_tmpDir->absolutePath(relativeFilePath);
101 
102     QTest::newRow(relativeFilePath.constData()) << absoluteFilePath;
103 }
104 
105 } // namespace CompilationDatabaseProjectManager
106