File indexing completed on 2024-04-28 07:42:12
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2000-2005 David Faure <faure@kde.org> 0004 SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "kfileutilstest.h" 0010 0011 #include <KFileUtils> 0012 #include <QStandardPaths> 0013 #include <QTest> 0014 0015 QTEST_MAIN(KFileUtilsTest) 0016 0017 void KFileUtilsTest::testSuggestName_data() 0018 { 0019 QTest::addColumn<QString>("oldName"); 0020 QTest::addColumn<QStringList>("existingFiles"); 0021 QTest::addColumn<QString>("expectedOutput"); 0022 0023 QTest::newRow("non-existing") << "foobar" << QStringList() << "foobar (1)"; 0024 QTest::newRow("existing") << "foobar" << QStringList(QStringLiteral("foobar")) << "foobar (1)"; 0025 QTest::newRow("existing_1") << "foobar" << (QStringList() << QStringLiteral("foobar") << QStringLiteral("foobar (1)")) << "foobar (2)"; 0026 QTest::newRow("extension") << "foobar.txt" << QStringList() << "foobar (1).txt"; 0027 QTest::newRow("extension_exists") << "foobar.txt" << (QStringList() << QStringLiteral("foobar.txt")) << "foobar (1).txt"; 0028 QTest::newRow("extension_exists_1") << "foobar.txt" << (QStringList() << QStringLiteral("foobar.txt") << QStringLiteral("foobar (1).txt")) 0029 << "foobar (2).txt"; 0030 QTest::newRow("two_extensions") << "foobar.tar.gz" << QStringList() << "foobar (1).tar.gz"; 0031 QTest::newRow("two_extensions_exists") << "foobar.tar.gz" << (QStringList() << QStringLiteral("foobar.tar.gz")) << "foobar (1).tar.gz"; 0032 QTest::newRow("two_extensions_exists_1") << "foobar.tar.gz" << (QStringList() << QStringLiteral("foobar.tar.gz") << QStringLiteral("foobar (1).tar.gz")) 0033 << "foobar (2).tar.gz"; 0034 QTest::newRow("with_space") << "foo bar" << QStringList(QStringLiteral("foo bar")) << "foo bar (1)"; 0035 QTest::newRow("dot_at_beginning") << ".aFile.tar.gz" << QStringList() << ".aFile (1).tar.gz"; 0036 QTest::newRow("dots_at_beginning") << "..aFile.tar.gz" << QStringList() << "..aFile (1).tar.gz"; 0037 QTest::newRow("empty_basename") << ".txt" << QStringList() << ". (1).txt"; 0038 QTest::newRow("empty_basename_2dots") << "..txt" << QStringList() << ". (1).txt"; 0039 QTest::newRow("basename_with_dots") << "filename.5.3.2.tar.gz" << QStringList() << "filename.5.3.2 (1).tar.gz"; 0040 QTest::newRow("unknown_extension_trashinfo") << "fileFromHome.trashinfo" << QStringList() << "fileFromHome (1).trashinfo"; 0041 } 0042 0043 void KFileUtilsTest::testSuggestName() 0044 { 0045 QFETCH(QString, oldName); 0046 QFETCH(QStringList, existingFiles); 0047 QFETCH(QString, expectedOutput); 0048 0049 QTemporaryDir dir; 0050 const QUrl baseUrl = QUrl::fromLocalFile(dir.path()); 0051 for (const QString &localFile : std::as_const(existingFiles)) { 0052 QFile file(dir.path() + QLatin1Char('/') + localFile); 0053 QVERIFY(file.open(QIODevice::WriteOnly)); 0054 } 0055 QCOMPARE(KFileUtils::suggestName(baseUrl, oldName), expectedOutput); 0056 } 0057 0058 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_ANDROID) 0059 #define XDG_PLATFORM 0060 #endif 0061 0062 void KFileUtilsTest::testfindAllUniqueFiles() 0063 { 0064 #ifndef XDG_PLATFORM 0065 QSKIP("This test requires XDG_DATA_DIRS; no way to configure QStandardPaths on Windows"); 0066 #endif 0067 const QString testBaseDirPath = QDir::currentPath() + QLatin1String("/kfileutilstestdata/"); 0068 QDir testDataBaseDir(testBaseDirPath); 0069 testDataBaseDir.mkpath(QStringLiteral(".")); 0070 testDataBaseDir.mkpath(QStringLiteral("testdir1/testDirName")); 0071 testDataBaseDir.mkpath(QStringLiteral("testdir2/testDirName")); 0072 testDataBaseDir.mkpath(QStringLiteral("testdir3/testDirName")); 0073 0074 QFile file1(testBaseDirPath + QLatin1String("/testdir1/testDirName/testfile.test")); 0075 file1.open(QFile::WriteOnly); 0076 QFile file2(testBaseDirPath + QLatin1String("/testdir2/testDirName/testfile.test")); 0077 file2.open(QFile::WriteOnly); 0078 QFile file3(testBaseDirPath + QLatin1String("/testdir3/testDirName/differentfile.test")); 0079 file3.open(QFile::WriteOnly); 0080 QFile file4(testBaseDirPath + QLatin1String("/testdir3/testDirName/nomatch.txt")); 0081 file4.open(QFile::WriteOnly); 0082 0083 qputenv("XDG_DATA_DIRS", qPrintable(QStringLiteral("%1testdir1:%1testdir2:%1testdir3").arg(testBaseDirPath))); 0084 0085 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("testDirName"), QStandardPaths::LocateDirectory); 0086 const QStringList expected = {testDataBaseDir.filePath(QStringLiteral("testdir1/testDirName/testfile.test")), 0087 testDataBaseDir.filePath(QStringLiteral("testdir3/testDirName/differentfile.test"))}; 0088 0089 const QStringList actual = KFileUtils::findAllUniqueFiles(dirs, QStringList{QStringLiteral("*.test")}); 0090 QCOMPARE(actual, expected); 0091 } 0092 0093 #include "moc_kfileutilstest.cpp"