1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/*
This file is part of the KDE project
SPDX-FileCopyrightText: 2014 David Faure <faure@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <QDir>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QTest>
#include <KIO/MkpathJob>
class MkpathJobTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
QStandardPaths::setTestModeEnabled(true);
QVERIFY(m_tempDir.isValid());
m_dir = m_tempDir.path();
}
void cleanupTestCase()
{
}
void shouldDoNothingIfExists()
{
QVERIFY(QFile::exists(m_dir));
const QStringList oldEntries = QDir(m_dir).entryList();
KIO::MkpathJob *job = KIO::mkpath(QUrl::fromLocalFile(m_dir));
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QVERIFY(QFile::exists(m_dir));
QCOMPARE(spy.count(), 0);
QCOMPARE(QDir(m_dir).entryList(), oldEntries); // nothing got created in there
}
void shouldCreateOneDirectory()
{
QUrl url = QUrl::fromLocalFile(m_dir);
url.setPath(url.path() + "/subdir1");
KIO::MkpathJob *job = KIO::mkpath(url);
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spy.count(), 1);
QVERIFY(QFile::exists(url.toLocalFile()));
}
void shouldCreateTwoDirectories()
{
QUrl url = QUrl::fromLocalFile(m_dir);
url.setPath(url.path() + "/subdir2/subsubdir");
KIO::MkpathJob *job = KIO::mkpath(url);
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spy.count(), 2);
QVERIFY(QFile::exists(url.toLocalFile()));
}
void shouldDoNothingIfExistsWithBasePath()
{
const QStringList oldEntries = QDir(m_dir).entryList();
QUrl url = QUrl::fromLocalFile(m_dir);
KIO::MkpathJob *job = KIO::mkpath(url, url);
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(job->totalAmount(KJob::Directories), 0ULL);
QCOMPARE(spy.count(), 0);
QVERIFY(QFile::exists(url.toLocalFile()));
QCOMPARE(QDir(m_dir).entryList(), oldEntries); // nothing got created in there
}
void shouldCreateOneDirectoryWithBasePath()
{
QUrl url = QUrl::fromLocalFile(m_dir);
const QUrl baseUrl = url;
url.setPath(url.path() + "/subdir3");
KIO::MkpathJob *job = KIO::mkpath(url, baseUrl);
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spy.count(), 1);
QCOMPARE(job->totalAmount(KJob::Directories), 1ULL);
QVERIFY(QFile::exists(url.toLocalFile()));
}
void shouldCreateTwoDirectoriesWithBasePath()
{
QUrl url = QUrl::fromLocalFile(m_dir);
const QUrl baseUrl = url;
url.setPath(url.path() + "/subdir4/subsubdir");
KIO::MkpathJob *job = KIO::mkpath(url, baseUrl);
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spy.count(), 2);
QCOMPARE(job->totalAmount(KJob::Directories), 2ULL);
QVERIFY(QFile::exists(url.toLocalFile()));
}
void shouldIgnoreUnrelatedBasePath()
{
QUrl url = QUrl::fromLocalFile(m_dir);
url.setPath(url.path() + "/subdir5/subsubdir");
KIO::MkpathJob *job = KIO::mkpath(url, QUrl::fromLocalFile(QStringLiteral("/does/not/exist")));
job->setUiDelegate(nullptr);
QSignalSpy spy(job, &KIO::MkpathJob::directoryCreated);
QVERIFY2(job->exec(), qPrintable(job->errorString()));
QCOMPARE(spy.count(), 2);
QVERIFY(QFile::exists(url.toLocalFile()));
}
private:
QTemporaryDir m_tempDir;
QString m_dir;
};
QTEST_MAIN(MkpathJobTest)
#include "mkpathjobtest.moc"
|