| 126 | } |
| 127 | |
| 128 | void FindReplaceTest::testSingleFileAsDirectoryChoice() |
| 129 | { |
| 130 | QTemporaryDir tmpDir; |
| 131 | QVERIFY2(tmpDir.isValid(), qPrintable("couldn't create temporary directory: " + tmpDir.errorString())); |
| 132 | |
| 133 | const QByteArray fileContents = "A"; |
| 134 | QString filePath = "testfile.cpp"; |
| 135 | |
| 136 | using FilesystemHelpers::makeAbsoluteCreateAndWrite; |
| 137 | QString errorPath = makeAbsoluteCreateAndWrite(tmpDir.path(), filePath, fileContents); |
| 138 | QVERIFY2(errorPath.isEmpty(), qPrintable("couldn't create or write to temporary file or directory " + errorPath)); |
| 139 | |
| 140 | const QString siblingDirPath = tmpDir.filePath("dir"); |
| 141 | QVERIFY(QDir{}.mkpath(siblingDirPath)); |
| 142 | |
| 143 | const QString siblingDirSameStartPath = tmpDir.filePath("test"); |
| 144 | QVERIFY(filePath.startsWith(siblingDirSameStartPath)); |
| 145 | QVERIFY(QDir{}.mkpath(siblingDirSameStartPath)); |
| 146 | |
| 147 | GrepJobSettings settings = verbatimGrepJobSettings(); |
| 148 | settings.pattern = fileContents; |
| 149 | |
| 150 | const auto test = [filePath, &settings](const QList<QUrl>& directoryChoice, bool expectMatch) { |
| 151 | GrepJob job; |
| 152 | GrepOutputModel model; |
| 153 | job.setOutputModel(&model); |
| 154 | job.setDirectoryChoice(directoryChoice); |
| 155 | job.setSettings(settings); |
| 156 | |
| 157 | QVERIFY(job.exec()); |
| 158 | |
| 159 | const auto index = model.nextItemIndex(QModelIndex{}); |
| 160 | |
| 161 | if (!expectMatch) { |
| 162 | QVERIFY(!index.isValid()); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | QVERIFY(index.isValid()); |
| 167 | auto* const item = dynamic_cast<const GrepOutputItem*>(model.itemFromIndex(index)); |
| 168 | QVERIFY(item); |
| 169 | QVERIFY(item->isText()); |
| 170 | QCOMPARE(item->filename(), filePath); |
| 171 | |
| 172 | const auto nextIndex = model.nextItemIndex(index); |
| 173 | QVERIFY2(!nextIndex.isValid() || nextIndex == index, "unexpected second matched file"); |
| 174 | }; |
| 175 | |
| 176 | const QString nonexistentFilePath = "/tmp/nonexistent/file/path.kdevelop"; |
| 177 | QVERIFY2(!QFileInfo::exists(nonexistentFilePath), "what a strange file path to exist..."); |
| 178 | // Test searching nowhere, in a nonexistent file and in empty directories in addition to |
| 179 | // the file set up above. The search locations, where nothing can be found, are |
| 180 | // unrelated to the main test, but do not complicate this test function much either. |
| 181 | const QList<QUrl> directoryChoices[] = {{}, |
| 182 | {QUrl::fromLocalFile(nonexistentFilePath)}, |
| 183 | {QUrl::fromLocalFile(siblingDirPath)}, |
| 184 | {QUrl::fromLocalFile(siblingDirSameStartPath)}, |
| 185 | {QUrl::fromLocalFile(filePath)}}; |
nothing calls this directly
no test coverage detected