File indexing completed on 2024-05-12 04:39:29

0001 /*
0002     SPDX-FileCopyrightText: 2020 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QTest>
0008 #include <QObject>
0009 #include <QVersionNumber>
0010 #include <QLoggingCategory>
0011 #include <QDir>
0012 #include <QJsonObject>
0013 
0014 #include <tests/autotestshell.h>
0015 #include <tests/testcore.h>
0016 
0017 #include <interfaces/iproject.h>
0018 #include <interfaces/iprojectcontroller.h>
0019 #include <interfaces/iprojectfilemanager.h>
0020 #include <interfaces/ibuildsystemmanager.h>
0021 #include <interfaces/iprojectbuilder.h>
0022 #include <project/projectmodel.h>
0023 
0024 #include <KJob>
0025 
0026 #include <cmakefileapi.h>
0027 #include <cmakeutils.h>
0028 #include <cmakeprojectdata.h>
0029 #include <testhelpers.h>
0030 
0031 using namespace KDevelop;
0032 
0033 class TestCMakeFileApi : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestSuite()
0039     {
0040         QLoggingCategory::setFilterRules(QStringLiteral("default.debug=true\nkdevelop.projectmanagers.cmake.debug=true\n"));
0041 
0042         if (!CMake::FileApi::supported(CMake::findExecutable())) {
0043             QSKIP("cmake exe doesn't support file API");
0044         }
0045 
0046         AutoTestShell::init({"KDevCMakeManager", "KDevCMakeBuilder", "KDevMakeBuilder", "KDevStandardOutputView"});
0047         TestCore::initialize();
0048     }
0049 
0050     void cleanupTestCase()
0051     {
0052         TestCore::shutdown();
0053     }
0054 
0055     void testConfigure()
0056     {
0057         auto project = loadProject(QStringLiteral("single_subdirectory"));
0058         QVERIFY(project);
0059 
0060         auto bsm = project->buildSystemManager();
0061         const auto buildPath = bsm->buildDirectory(project->projectItem());
0062         const auto buildDir = buildPath.toLocalFile();
0063         QVERIFY(!buildDir.isEmpty());
0064         QVERIFY(QDir(buildDir).removeRecursively());
0065 
0066         auto builder = bsm->builder();
0067         auto configureJob = builder->configure(project);
0068         QVERIFY(configureJob);
0069         QVERIFY(configureJob->exec());
0070 
0071         const auto index = CMake::FileApi::findReplyIndexFile(buildDir);
0072         QVERIFY(index.isValid());
0073         QVERIFY(!index.isOutdated());
0074 
0075         const auto projectData = CMake::FileApi::parseReplyIndexFile(index, project->path(), buildPath);
0076         QVERIFY(projectData.compilationData.isValid);
0077         QCOMPARE(projectData.targets.size(), 1);
0078         const auto subDirPath = Path(project->path(), "subdir");
0079         QVERIFY(projectData.targets.contains(subDirPath));
0080         const auto targets = projectData.targets[subDirPath];
0081         QCOMPARE(targets.size(), 1);
0082         const auto target = targets.first();
0083         QCOMPARE(target.name, QLatin1String("foo"));
0084         QCOMPARE(target.type, CMakeTarget::Executable);
0085         const auto buildSubDirPath = Path(buildPath, "subdir");
0086         QCOMPARE(target.artifacts, {Path(buildSubDirPath, "foo")});
0087         const auto fooSrcPath = Path(subDirPath, "foo.cpp");
0088         QCOMPARE(target.sources, {fooSrcPath});
0089 
0090         QCOMPARE(projectData.compilationData.files.size(), 1);
0091         QVERIFY(projectData.compilationData.files.contains(fooSrcPath));
0092         const auto srcInfo = projectData.compilationData.files[fooSrcPath];
0093         QCOMPARE(srcInfo.language, QLatin1String("CXX"));
0094         QCOMPARE(srcInfo.includes.size(), 3);
0095         QVERIFY(srcInfo.includes.contains(buildPath));
0096         QVERIFY(srcInfo.includes.contains(buildSubDirPath));
0097         QVERIFY(srcInfo.includes.contains(subDirPath));
0098 
0099         QVERIFY(projectData.cmakeFiles.contains(Path(project->path(), "CMakeLists.txt")));
0100         QVERIFY(projectData.cmakeFiles.contains(Path(subDirPath, "CMakeLists.txt")));
0101         QCOMPARE(projectData.cmakeFiles.size(), 2);
0102 
0103         QVERIFY(!projectData.isOutdated);
0104     }
0105 };
0106 
0107 QTEST_MAIN(TestCMakeFileApi)
0108 #include "test_cmakefileapi.moc"