File indexing completed on 2024-04-28 03:51:40
0001 /* 0002 This file is part of the KDE Baloo project. 0003 SPDX-FileCopyrightText: 2014 Vishesh Handa <vhanda@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "fileindexerconfigutils.h" 0009 #include "filtereddiriterator.h" 0010 #include "fileindexerconfig.h" 0011 0012 #include <QTemporaryDir> 0013 #include <QTest> 0014 0015 namespace { 0016 const QStringList dataSet() { 0017 static QStringList dataSet = { 0018 QStringLiteral("home/"), 0019 QStringLiteral("home/1"), 0020 QStringLiteral("home/2"), 0021 QStringLiteral("home/kde/"), 0022 QStringLiteral("home/kde/1"), 0023 QStringLiteral("home/docs/"), 0024 QStringLiteral("home/docs/1"), 0025 QStringLiteral("home/docs/.fire"), 0026 QStringLiteral("home/.hiddenDir/"), 0027 QStringLiteral("home/.hiddenFile"), 0028 QStringLiteral("home/.includedHidden/"), 0029 QStringLiteral("home/.includedHidden/dir/"), 0030 QStringLiteral("home/.includedHidden/file"), 0031 QStringLiteral("home/.includedHidden/.hidden"), 0032 }; 0033 return dataSet; 0034 } 0035 } 0036 0037 class FilteredDirIteratorTest : public QObject 0038 { 0039 Q_OBJECT 0040 private Q_SLOTS: 0041 void testFiles(); 0042 void testIndexHidden(); 0043 void testFolders(); 0044 void testAddingExcludedFolder(); 0045 void testNoConfig(); 0046 }; 0047 0048 using namespace Baloo; 0049 0050 void FilteredDirIteratorTest::testFiles() 0051 { 0052 std::unique_ptr<QTemporaryDir> dir(Test::createTmpFilesAndFolders(dataSet())); 0053 0054 QStringList includeFolders = { 0055 dir->path() + QLatin1String("/home"), 0056 dir->path() + QLatin1String("/home/.includedHidden"), 0057 }; 0058 0059 QStringList excludeFolders = { 0060 dir->path() + QLatin1String("/home/kde") 0061 }; 0062 0063 Test::writeIndexerConfig(includeFolders, excludeFolders); 0064 0065 FileIndexerConfig config; 0066 FilteredDirIterator it(&config, includeFolders.first()); 0067 0068 QSet<QString> expected = { 0069 QStringLiteral("/home"), 0070 QStringLiteral("/home/docs"), 0071 QStringLiteral("/home/docs/1"), 0072 QStringLiteral("/home/1"), 0073 QStringLiteral("/home/2"), 0074 QStringLiteral("/home/.includedHidden"), 0075 QStringLiteral("/home/.includedHidden/dir"), 0076 QStringLiteral("/home/.includedHidden/file"), 0077 }; 0078 0079 QSet<QString> list; 0080 while (!it.next().isEmpty()) { 0081 QString path = it.filePath(); 0082 // Strip temporary dir prefix 0083 path = path.mid(dir->path().length()); 0084 0085 list << path; 0086 QVERIFY(config.shouldFolderBeIndexed(it.filePath())); 0087 QVERIFY(expected.contains(path)); 0088 } 0089 QCOMPARE(list, expected); 0090 } 0091 0092 void FilteredDirIteratorTest::testIndexHidden() 0093 { 0094 std::unique_ptr<QTemporaryDir> dir(Test::createTmpFilesAndFolders(dataSet())); 0095 0096 QStringList includeFolders = { 0097 dir->path() + QLatin1String("/home"), 0098 dir->path() + QLatin1String("/home/.includedHidden"), 0099 }; 0100 0101 QStringList excludeFolders = { 0102 dir->path() + QLatin1String("/home/kde") 0103 }; 0104 0105 Test::writeIndexerConfig(includeFolders, excludeFolders, {}, true); 0106 0107 FileIndexerConfig config; 0108 FilteredDirIterator it(&config, includeFolders.first()); 0109 0110 QSet<QString> expected = { 0111 QStringLiteral("/home"), 0112 QStringLiteral("/home/docs"), 0113 QStringLiteral("/home/docs/1"), 0114 QStringLiteral("/home/1"), 0115 QStringLiteral("/home/2"), 0116 QStringLiteral("/home/docs/.fire"), 0117 QStringLiteral("/home/.hiddenDir"), 0118 QStringLiteral("/home/.hiddenFile"), 0119 QStringLiteral("/home/.includedHidden"), 0120 QStringLiteral("/home/.includedHidden/dir"), 0121 QStringLiteral("/home/.includedHidden/file"), 0122 QStringLiteral("/home/.includedHidden/.hidden"), 0123 }; 0124 0125 QSet<QString> list; 0126 while (!it.next().isEmpty()) { 0127 QString path = it.filePath(); 0128 // Strip temporary dir prefix 0129 path = path.mid(dir->path().length()); 0130 0131 list << path; 0132 QVERIFY(config.shouldFolderBeIndexed(it.filePath())); 0133 QVERIFY(expected.contains(path)); 0134 } 0135 QCOMPARE(list, expected); 0136 } 0137 0138 void FilteredDirIteratorTest::testFolders() 0139 { 0140 std::unique_ptr<QTemporaryDir> dir(Test::createTmpFilesAndFolders(dataSet())); 0141 0142 QStringList includeFolders = { 0143 dir->path() + QLatin1String("/home"), 0144 }; 0145 0146 QStringList excludeFolders = { 0147 dir->path() + QLatin1String("/home/kde") 0148 }; 0149 0150 Test::writeIndexerConfig(includeFolders, excludeFolders); 0151 0152 FileIndexerConfig config; 0153 FilteredDirIterator it(&config, includeFolders.first(), FilteredDirIterator::DirsOnly); 0154 0155 QSet<QString> expected = { 0156 QStringLiteral("/home"), 0157 QStringLiteral("/home/docs"), 0158 }; 0159 0160 QSet<QString> list; 0161 while (!it.next().isEmpty()) { 0162 QString path = it.filePath(); 0163 path = path.mid(dir->path().length()); 0164 0165 list << path; 0166 QVERIFY(config.shouldFolderBeIndexed(it.filePath())); 0167 QVERIFY(expected.contains(path)); 0168 } 0169 QCOMPARE(list, expected); 0170 } 0171 0172 void FilteredDirIteratorTest::testAddingExcludedFolder() 0173 { 0174 std::unique_ptr<QTemporaryDir> dir(Test::createTmpFilesAndFolders(dataSet())); 0175 0176 QStringList includeFolders = { 0177 dir->path() + QLatin1String("/home"), 0178 }; 0179 0180 QStringList excludeFolders = { 0181 dir->path() + QLatin1String("/home/kde") 0182 }; 0183 0184 Test::writeIndexerConfig(includeFolders, excludeFolders); 0185 0186 FileIndexerConfig config; 0187 FilteredDirIterator it(&config, excludeFolders.first()); 0188 0189 QVERIFY(it.next().isEmpty()); 0190 } 0191 0192 void FilteredDirIteratorTest::testNoConfig() 0193 { 0194 std::unique_ptr<QTemporaryDir> dir(Test::createTmpFilesAndFolders(dataSet())); 0195 0196 FilteredDirIterator it(nullptr, dir->path() + QLatin1String("/home")); 0197 0198 QSet<QString> expected = { 0199 QStringLiteral("/home"), 0200 QStringLiteral("/home/1"), 0201 QStringLiteral("/home/2"), 0202 QStringLiteral("/home/kde"), 0203 QStringLiteral("/home/kde/1"), 0204 QStringLiteral("/home/docs"), 0205 QStringLiteral("/home/docs/1"), 0206 }; 0207 0208 QSet<QString> list; 0209 while (!it.next().isEmpty()) { 0210 QString path = it.filePath(); 0211 path = path.mid(dir->path().length()); 0212 0213 list << path; 0214 QVERIFY(expected.contains(path)); 0215 } 0216 QCOMPARE(list, expected); 0217 } 0218 0219 QTEST_GUILESS_MAIN(FilteredDirIteratorTest) 0220 0221 #include "filtereddiriteratortest.moc"