File indexing completed on 2025-01-12 12:29:34
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <QDir> 0009 #include <QSignalSpy> 0010 #include <QStandardPaths> 0011 #include <QTemporaryDir> 0012 #include <QTest> 0013 0014 #include <KIO/MkpathJob> 0015 0016 class MkpathJobTest : public QObject 0017 { 0018 Q_OBJECT 0019 0020 private Q_SLOTS: 0021 void initTestCase() 0022 { 0023 QStandardPaths::setTestModeEnabled(true); 0024 0025 QVERIFY(m_tempDir.isValid()); 0026 m_dir = m_tempDir.path(); 0027 } 0028 0029 void cleanupTestCase() 0030 { 0031 } 0032 0033 void shouldDoNothingIfExists() 0034 { 0035 QVERIFY(QFile::exists(m_dir)); 0036 const QStringList oldEntries = QDir(m_dir).entryList(); 0037 KIO::MkpathJob *job = KIO::mkpath(QUrl::fromLocalFile(m_dir)); 0038 job->setUiDelegate(nullptr); 0039 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0040 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0041 QVERIFY(QFile::exists(m_dir)); 0042 QCOMPARE(spy.count(), 0); 0043 QCOMPARE(QDir(m_dir).entryList(), oldEntries); // nothing got created in there 0044 } 0045 0046 void shouldCreateOneDirectory() 0047 { 0048 QUrl url = QUrl::fromLocalFile(m_dir); 0049 url.setPath(url.path() + "/subdir1"); 0050 KIO::MkpathJob *job = KIO::mkpath(url); 0051 job->setUiDelegate(nullptr); 0052 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0053 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0054 QCOMPARE(spy.count(), 1); 0055 QVERIFY(QFile::exists(url.toLocalFile())); 0056 } 0057 0058 void shouldCreateTwoDirectories() 0059 { 0060 QUrl url = QUrl::fromLocalFile(m_dir); 0061 url.setPath(url.path() + "/subdir2/subsubdir"); 0062 KIO::MkpathJob *job = KIO::mkpath(url); 0063 job->setUiDelegate(nullptr); 0064 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0065 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0066 QCOMPARE(spy.count(), 2); 0067 QVERIFY(QFile::exists(url.toLocalFile())); 0068 } 0069 0070 void shouldDoNothingIfExistsWithBasePath() 0071 { 0072 const QStringList oldEntries = QDir(m_dir).entryList(); 0073 QUrl url = QUrl::fromLocalFile(m_dir); 0074 KIO::MkpathJob *job = KIO::mkpath(url, url); 0075 job->setUiDelegate(nullptr); 0076 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0077 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0078 QCOMPARE(job->totalAmount(KJob::Directories), 0ULL); 0079 QCOMPARE(spy.count(), 0); 0080 QVERIFY(QFile::exists(url.toLocalFile())); 0081 QCOMPARE(QDir(m_dir).entryList(), oldEntries); // nothing got created in there 0082 } 0083 0084 void shouldCreateOneDirectoryWithBasePath() 0085 { 0086 QUrl url = QUrl::fromLocalFile(m_dir); 0087 const QUrl baseUrl = url; 0088 url.setPath(url.path() + "/subdir3"); 0089 KIO::MkpathJob *job = KIO::mkpath(url, baseUrl); 0090 job->setUiDelegate(nullptr); 0091 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0092 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0093 QCOMPARE(spy.count(), 1); 0094 QCOMPARE(job->totalAmount(KJob::Directories), 1ULL); 0095 QVERIFY(QFile::exists(url.toLocalFile())); 0096 } 0097 0098 void shouldCreateTwoDirectoriesWithBasePath() 0099 { 0100 QUrl url = QUrl::fromLocalFile(m_dir); 0101 const QUrl baseUrl = url; 0102 url.setPath(url.path() + "/subdir4/subsubdir"); 0103 KIO::MkpathJob *job = KIO::mkpath(url, baseUrl); 0104 job->setUiDelegate(nullptr); 0105 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0106 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0107 QCOMPARE(spy.count(), 2); 0108 QCOMPARE(job->totalAmount(KJob::Directories), 2ULL); 0109 QVERIFY(QFile::exists(url.toLocalFile())); 0110 } 0111 0112 void shouldIgnoreUnrelatedBasePath() 0113 { 0114 QUrl url = QUrl::fromLocalFile(m_dir); 0115 url.setPath(url.path() + "/subdir5/subsubdir"); 0116 KIO::MkpathJob *job = KIO::mkpath(url, QUrl::fromLocalFile(QStringLiteral("/does/not/exist"))); 0117 job->setUiDelegate(nullptr); 0118 QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated); 0119 QVERIFY2(job->exec(), qPrintable(job->errorString())); 0120 QCOMPARE(spy.count(), 2); 0121 QVERIFY(QFile::exists(url.toLocalFile())); 0122 } 0123 0124 private: 0125 QTemporaryDir m_tempDir; 0126 QString m_dir; 0127 }; 0128 0129 QTEST_MAIN(MkpathJobTest) 0130 0131 #include "mkpathjobtest.moc"