File indexing completed on 2024-06-16 04:24:41

0001 /*
0002     SPDX-FileCopyrightText: 2014 Sergey Kalinichev <kalinichev.so.0@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_custommake.h"
0008 
0009 #include <QFile>
0010 #include <QTextStream>
0011 #include <QDebug>
0012 #include <QTemporaryDir>
0013 
0014 #include <tests/autotestshell.h>
0015 #include <tests/testcore.h>
0016 
0017 #include "../makefileresolver.h"
0018 
0019 #include <QTest>
0020 
0021 using namespace KDevelop;
0022 
0023 namespace {
0024 void createFile( QFile& file )
0025 {
0026     file.remove();
0027     if ( !file.open( QIODevice::ReadWrite ) ) {
0028         qFatal("Cannot create the file %s", file.fileName().toUtf8().data());
0029     }
0030 }
0031 }
0032 
0033 void TestCustomMake::initTestCase()
0034 {
0035     AutoTestShell::init();
0036     TestCore::initialize(Core::NoUi);
0037 }
0038 
0039 void TestCustomMake::cleanupTestCase()
0040 {
0041     TestCore::shutdown();
0042 }
0043 
0044 void TestCustomMake::testIncludeDirectories()
0045 {
0046     QTemporaryDir tempDir;
0047     {
0048         QFile file( tempDir.path() + "/Makefile" );
0049         createFile( file );
0050         QFile testfile( tempDir.path() + "/testfile.cpp" );
0051         createFile(testfile);
0052         QTextStream stream1( &file );
0053         stream1 << "testfile.o:\n\t g++ testfile.cpp -I/testFile1 -I /testFile2 -isystem /testFile3 --include-dir=/testFile4 -o testfile";
0054     }
0055 
0056     MakeFileResolver mf;
0057     auto result = mf.resolveIncludePath(tempDir.path() + "/testfile.cpp");
0058     if (!result.success) {
0059       qDebug() << result.errorMessage << result.longErrorMessage;
0060       QFAIL("Failed to resolve include path.");
0061     }
0062     QCOMPARE(result.paths.size(), 4);
0063     QVERIFY(result.paths.contains(Path("/testFile1")));
0064     QVERIFY(result.paths.contains(Path("/testFile2")));
0065     QVERIFY(result.paths.contains(Path("/testFile3")));
0066     QVERIFY(result.paths.contains(Path("/testFile4")));
0067 }
0068 
0069 void TestCustomMake::testFrameworkDirectories()
0070 {
0071     QTemporaryDir tempDir;
0072     int expectedPaths = 2;
0073     {
0074         QFile file( tempDir.path() + "/Makefile" );
0075         createFile( file );
0076         QFile testfile( tempDir.path() + "/testfile.cpp" );
0077         createFile(testfile);
0078         QTextStream stream1( &file );
0079         stream1 << "testfile.o:\n\t clang++ testfile.cpp -iframework /System/Library/Frameworks -F/Library/Frameworks -o testfile";
0080     }
0081 
0082     MakeFileResolver mf;
0083     auto result = mf.resolveIncludePath(tempDir.path() + "/testfile.cpp");
0084     if (!result.success) {
0085       qDebug() << result.errorMessage << result.longErrorMessage;
0086       QFAIL("Failed to resolve include path.");
0087     }
0088     QCOMPARE(result.frameworkDirectories.size(), expectedPaths);
0089     QVERIFY(result.frameworkDirectories.contains(Path("/System/Library/Frameworks")));
0090     QVERIFY(result.frameworkDirectories.contains(Path("/Library/Frameworks")));
0091 }
0092 
0093 void TestCustomMake::testDefines()
0094 {
0095     MakeFileResolver mf;
0096     const auto result = mf.processOutput("-DFOO  -DFOO=\\\"foo\\\" -DBAR=ASDF -DLALA=1 -DMEH="
0097                                          " -DSTR=\"\\\"foo \\\\\\\" bar\\\"\" -DEND"
0098                                          " -DX=1 -UX", QString());
0099     QCOMPARE(result.defines.value("FOO", "not found"), QString("\"foo\""));
0100     QCOMPARE(result.defines.value("BAR", "not found"), QString("ASDF"));
0101     QCOMPARE(result.defines.value("LALA", "not found"), QString("1"));
0102     QCOMPARE(result.defines.value("MEH", "not found"), QString());
0103     QCOMPARE(result.defines.value("STR", "not found"), QString("\"foo \\\" bar\""));
0104     QCOMPARE(result.defines.value("END", "not found"), QString());
0105     QCOMPARE(result.defines.value("X", "not found"), QString("not found"));
0106 }
0107 
0108 QTEST_GUILESS_MAIN(TestCustomMake)
0109 
0110 #include "moc_test_custommake.cpp"