File indexing completed on 2024-04-28 04:37:34

0001 /*
0002     SPDX-FileCopyrightText: 2014 Sergey Kalinichev <kalinichev.so.0@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "projectsgenerator.h"
0008 
0009 #include <tests/kdevsignalspy.h>
0010 
0011 #include <interfaces/icore.h>
0012 #include <interfaces/iproject.h>
0013 #include <interfaces/iprojectcontroller.h>
0014 #include <util/path.h>
0015 
0016 #include <QString>
0017 #include <QDir>
0018 #include <QFile>
0019 #include <QTextStream>
0020 #include <QDebug>
0021 
0022 
0023 using namespace KDevelop;
0024 
0025 namespace
0026 {
0027 /// @param projectFile projectName.kdev4 file
0028 IProject* loadProject( const QString& projectFile, const QString& projectName )
0029 {
0030     KDevSignalSpy projectSpy(ICore::self()->projectController(), SIGNAL(projectOpened(KDevelop::IProject*)));
0031     ICore::self()->projectController()->openProject( QUrl::fromLocalFile(projectFile) );
0032 
0033     if (!projectSpy.wait(5000)) {
0034         qFatal("Expected project to be loaded within 5 seconds, but this didn't happen");
0035     }
0036     IProject* project = ICore::self()->projectController()->findProjectByName( projectName );
0037 
0038     return project;
0039 }
0040 
0041 void createFile( QFile& file )
0042 {
0043     file.remove();
0044     if ( !file.open( QIODevice::ReadWrite ) ) {
0045         qFatal("Cannot create the file %s", file.fileName().toUtf8().constData());
0046     }
0047 }
0048 }
0049 
0050 namespace KDevelop {
0051 IProject* ProjectsGenerator::GenerateSimpleProject()
0052 {
0053     const QString sp = QStringLiteral( "simpleproject" );
0054     auto rootFolder = QDir::temp();
0055     QDir(rootFolder.absolutePath() + QLatin1Char('/') + sp).removeRecursively();
0056     rootFolder.mkdir( sp );
0057     rootFolder.cd( sp );
0058     rootFolder.mkdir( QStringLiteral("src") );
0059     rootFolder.mkdir( QStringLiteral(".kdev4") );
0060 
0061     {
0062         QFile file( rootFolder.filePath( QStringLiteral("simpleproject.kdev4") ) );
0063         createFile( file );
0064         QTextStream stream1( &file );
0065         stream1 << "[Project]\nName=SimpleProject\nManager=KDevCustomBuildSystem";
0066     }
0067     {
0068         QFile file( rootFolder.filePath( QStringLiteral("src/main.cpp") ) );
0069         createFile( file );
0070     }
0071     {
0072         QFile file( rootFolder.filePath( QStringLiteral(".kdev4/simpleproject.kdev4") ) );
0073         createFile( file );
0074         QTextStream stream( &file );
0075         stream << "[Buildset]\n" <<
0076         "BuildItems=@Variant(\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x1a\\x00S\\x00i\\x00m\\x00p\\x00l\\x00e\\x00P\\x00r\\x00o\\x00j\\x00e\\x00c\\x00t)\n\n" <<
0077         "[CustomBuildSystem]\n" <<
0078         "CurrentConfiguration=BuildConfig0\n\n" <<
0079         "[CustomBuildSystem][BuildConfig0]\n" <<
0080         "BuildDir=file:///" << rootFolder.absolutePath() << "/build\n" <<
0081         "Title=mainbuild\n\n" <<
0082 
0083         "[CustomDefinesAndIncludes][ProjectPath0]\n" <<
0084         "Path=/\n" <<
0085         "[CustomDefinesAndIncludes][ProjectPath0][Defines]\n" <<
0086         "_DEBUG=\n" <<
0087         "VARIABLE=VALUE\n" <<
0088         "[CustomDefinesAndIncludes][ProjectPath0][Includes]\n" <<
0089         "1=" << QDir::rootPath() << "usr/include/mydir\n" <<
0090         "[Project]\n" <<
0091         "VersionControlSupport=\n";
0092     }
0093     return loadProject( QDir::tempPath() + QStringLiteral( "/simpleproject/simpleproject.kdev4" ), QStringLiteral("SimpleProject") );
0094 }
0095 
0096 IProject* ProjectsGenerator::GenerateMultiPathProject()
0097 {
0098     const QString mp = QStringLiteral( "multipathproject" );
0099     auto rootFolder = QDir::temp();
0100     QDir(rootFolder.absolutePath() + QLatin1Char('/') + mp).removeRecursively();
0101     rootFolder.mkdir( mp );
0102     rootFolder.cd( mp );
0103     rootFolder.mkdir( QStringLiteral("src") );
0104     rootFolder.mkdir( QStringLiteral(".kdev4") );
0105     rootFolder.mkdir( QStringLiteral("anotherFolder") );
0106 
0107     {
0108         QFile file( rootFolder.filePath( QStringLiteral("multipathproject.kdev4") ) );
0109         createFile( file );
0110         QTextStream stream1( &file );
0111         stream1 << "[Project]\nName=MultiPathProject\nManager=KDevCustomBuildSystem";
0112         ;
0113     }
0114     {
0115         QFile file1( rootFolder.filePath( QStringLiteral("src/main.cpp") ) );
0116         createFile( file1 );
0117         QFile file2( rootFolder.filePath( QStringLiteral("anotherFolder/tst.h") ) );
0118         createFile( file2 );
0119     }
0120     {
0121         QFile file( rootFolder.filePath( QStringLiteral(".kdev4/multipathproject.kdev4") ) );
0122         createFile( file );
0123         QTextStream stream( &file );
0124         stream << "[Buildset]\n" <<
0125         "BuildItems=@Variant(\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x0b\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00 \\x00M\\x00u\\x00l\\x00t\\x00i\\x00P\\x00a\\x00t\\x00h\\x00P\\x00r\\x00o\\x00j\\x00e\\x00c\\x00t)\n\n" <<
0126         "[CustomBuildSystem]\n" <<
0127         "CurrentConfiguration=BuildConfig0\n\n" <<
0128         "[CustomBuildSystem][BuildConfig0]\n" <<
0129         "BuildDir=file:///" << rootFolder.absolutePath() << "/build\n" <<
0130         "[CustomDefinesAndIncludes][ProjectPath0]\n" <<
0131         "Path=.\n" <<
0132         "[CustomDefinesAndIncludes][ProjectPath0][Defines]\n" <<
0133         "SOURCE=CONTENT\n" <<
0134         "_COPY=\n" <<
0135         "[CustomDefinesAndIncludes][ProjectPath0][Includes]\n" <<
0136         "1=" << QDir::rootPath() << "usr/include/otherdir\n" <<
0137         "[CustomDefinesAndIncludes][ProjectPath1]\n" <<
0138         "Path=src\n" <<
0139         "[CustomDefinesAndIncludes][ProjectPath1][Defines]\n" <<
0140         "BUILD=debug\n" <<
0141         "[CustomDefinesAndIncludes][ProjectPath1][Includes]\n" <<
0142         "1=" << QDir::rootPath() << "usr/local/include/mydir\n" <<
0143         "[CustomDefinesAndIncludes][ProjectPath2]\n" <<
0144         "Path=anotherFolder\n" <<
0145         "[CustomDefinesAndIncludes][ProjectPath2][Defines]\n" <<
0146         "HIDDEN=\n" <<
0147         "[Project]\n" <<
0148         "VersionControlSupport=\n";
0149     }
0150     return loadProject( QDir::tempPath() + QStringLiteral( "/multipathproject/multipathproject.kdev4" ), QStringLiteral("MultiPathProject") );
0151 }
0152 
0153 IProject* ProjectsGenerator::GenerateSimpleProjectWithOutOfProjectFiles()
0154 {
0155     auto project = GenerateSimpleProject();
0156     Q_ASSERT(project);
0157 
0158     auto rootFolder = QDir(project->path().path());
0159     const QString includePaths = QStringLiteral(".kdev_include_paths");
0160 
0161     QFile file(rootFolder.filePath(includePaths));
0162     createFile(file);
0163     QTextStream stream( &file );
0164     stream << QLatin1Char('.') + QDir::separator() + QStringLiteral("include1.h\n")
0165            << rootFolder.canonicalPath() + QDir::separator() + QStringLiteral("include2.h");
0166 
0167     return project;
0168 }
0169 
0170 IProject* ProjectsGenerator::GenerateEmptyProject()
0171 {
0172     const QString ep = QStringLiteral("emptyproject");
0173     auto rootFolder = QDir::temp();
0174     QDir(rootFolder.absolutePath() + QLatin1Char('/') + ep).removeRecursively();
0175     rootFolder.mkdir(ep);
0176     rootFolder.cd(ep);
0177     rootFolder.mkdir(QStringLiteral(".kdev4"));
0178 
0179     {
0180         QFile file(rootFolder.filePath(QStringLiteral("emptyproject.kdev4")));
0181         createFile(file);
0182         QTextStream stream(&file);
0183         stream << "[Project]\nName=EmptyProject\nManager=KDevCustomBuildSystem";
0184     }
0185 
0186     {
0187         QFile file(rootFolder.filePath(QStringLiteral(".kdev4/emptyproject.kdev4")));
0188         createFile(file);
0189         QTextStream stream(&file);
0190         stream << "[Project]\n" << "VersionControlSupport=\n";
0191     }
0192     return loadProject(QDir::tempPath() + QStringLiteral( "/emptyproject/emptyproject.kdev4" ), QStringLiteral("EmptyProject"));
0193 }
0194 
0195 IProject* ProjectsGenerator::GenerateEmptyBuildDirProject()
0196 {
0197     const QString ebp = QStringLiteral( "emptybuilddirproject" );
0198     auto rootFolder = QDir::temp();
0199     QDir(rootFolder.absolutePath() + QStringLiteral( "/" ) + ebp).removeRecursively();
0200     rootFolder.mkdir( ebp );
0201     rootFolder.cd( ebp );
0202     rootFolder.mkdir( QStringLiteral("src") );
0203     rootFolder.mkdir( QStringLiteral(".kdev4") );
0204 
0205     {
0206         QFile file( rootFolder.filePath( QStringLiteral("emptybuilddirproject.kdev4") ) );
0207         createFile( file );
0208         QTextStream stream1( &file );
0209         stream1 << "[Project]\nName=EmptyBuildDirProject\nManager=KDevCustomBuildSystem";
0210     }
0211     {
0212         QFile file( rootFolder.filePath( QStringLiteral("src/main.cpp") ) );
0213         createFile( file );
0214     }
0215     {
0216         QFile file( rootFolder.filePath( QStringLiteral(".kdev4/emptybuilddirproject.kdev4") ) );
0217         createFile( file );
0218         QTextStream stream( &file );
0219         stream << "[Buildset]\n" <<
0220         "BuildItems=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x01\x00\x00\x00(\x00E\x00m\x00p\x00t\x00y\x00B\x00u\x00i\x00l\x00d\x00d\x00i\x00r\x00P\x00r\x00o\x00j\x00e\x00c\x00t)\n\n" <<
0221         "[CustomBuildSystem]\n" <<
0222         "CurrentConfiguration=BuildConfig0\n\n" <<
0223         "[CustomBuildSystem][BuildConfig0]\n" <<
0224         "BuildDir=" <<
0225         "Title=mainbuild\n\n" <<
0226 
0227         "[CustomDefinesAndIncludes][ProjectPath0]\n" <<
0228         "Path=/\n" <<
0229         "[CustomDefinesAndIncludes][ProjectPath0][Defines]\n" <<
0230         "_DEBUG=\n" <<
0231         "VARIABLE=VALUE\n" <<
0232         "[CustomDefinesAndIncludes][ProjectPath0][Includes]\n" <<
0233         "1=" << QDir::rootPath() << "usr/include/mydir\n" <<
0234         "[Project]\n" <<
0235         "VersionControlSupport=\n";
0236     }
0237     return loadProject( QDir::tempPath() + QStringLiteral( "/simpleproject/simpleproject.kdev4" ), QStringLiteral("SimpleProject") );
0238 }
0239 
0240 
0241 } // KDevelop