2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2022-05-31 11:16:44 +02:00
|
|
|
|
|
|
|
|
#include <utils/filepath.h>
|
|
|
|
|
#include <utils/fsengine/fsengine.h>
|
2022-10-10 17:32:56 +02:00
|
|
|
#include <utils/environment.h>
|
2022-05-31 11:16:44 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFileSystemModel>
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
|
|
class tst_fsengine : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void initTestCase();
|
|
|
|
|
|
|
|
|
|
void testFilePathFromToString();
|
|
|
|
|
|
|
|
|
|
void testRootPathContainsFakeDir();
|
|
|
|
|
void testNotExistingFile();
|
|
|
|
|
void testCreateFile();
|
|
|
|
|
void testListDir();
|
|
|
|
|
void testCreateDir();
|
|
|
|
|
void testWindowsPaths();
|
|
|
|
|
void testUrl();
|
2022-08-25 15:12:19 +02:00
|
|
|
void testBrokenWindowsPath();
|
2022-09-22 08:02:44 +02:00
|
|
|
void testRead();
|
|
|
|
|
void testWrite();
|
2022-05-31 11:16:44 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QString makeTestPath(QString path, bool asUrl = false);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FSEngine engine;
|
|
|
|
|
QString tempFolder;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class... Args>
|
|
|
|
|
using Continuation = std::function<void(Args...)>;
|
|
|
|
|
|
2022-07-26 09:36:13 +02:00
|
|
|
QString startWithSlash(QString s)
|
|
|
|
|
{
|
|
|
|
|
if (!s.startsWith('/'))
|
|
|
|
|
s.prepend('/');
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
void tst_fsengine::initTestCase()
|
|
|
|
|
{
|
|
|
|
|
if (!FSEngine::isAvailable())
|
|
|
|
|
QSKIP("Utils was built without Filesystem Engine");
|
|
|
|
|
|
2022-09-15 09:32:28 +02:00
|
|
|
if (HostOsInfo::isWindowsHost())
|
|
|
|
|
QSKIP("The fsengine tests are not supported on Windows.");
|
|
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
FSEngine::addDevice(FilePath::fromString("device://test"));
|
|
|
|
|
|
|
|
|
|
tempFolder = QDir::tempPath();
|
|
|
|
|
QDir testFolder(QString("%1/tst_fsengine").arg(tempFolder));
|
|
|
|
|
if (testFolder.exists())
|
|
|
|
|
QVERIFY(testFolder.removeRecursively());
|
|
|
|
|
|
|
|
|
|
QDir(tempFolder).mkdir("tst_fsengine");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testFilePathFromToString()
|
|
|
|
|
{
|
|
|
|
|
FilePath p = FilePath::fromString("device://test/test.txt");
|
2022-08-04 13:35:42 +02:00
|
|
|
QCOMPARE(p.scheme(), u"device");
|
|
|
|
|
QCOMPARE(p.host(), u"test");
|
|
|
|
|
QCOMPARE(p.path(), u"/test.txt");
|
2022-05-31 11:16:44 +02:00
|
|
|
|
2022-09-16 10:29:51 +02:00
|
|
|
QString asString = p.toFSPathString();
|
2022-11-29 10:28:37 +01:00
|
|
|
QCOMPARE(asString, FilePath::specialDeviceRootPath() + "/test/test.txt");
|
2022-05-31 11:16:44 +02:00
|
|
|
|
|
|
|
|
FilePath p2 = FilePath::fromString(asString);
|
2022-11-29 10:28:37 +01:00
|
|
|
QCOMPARE(p2.scheme(), u"device");
|
|
|
|
|
QCOMPARE(p2.host(), u"test");
|
|
|
|
|
QCOMPARE(p2.path(), u"/test.txt");
|
2022-05-31 11:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testRootPathContainsFakeDir()
|
|
|
|
|
{
|
2022-09-21 15:25:53 +02:00
|
|
|
const QStringList rootList = QDir::root().entryList();
|
2022-11-29 10:28:37 +01:00
|
|
|
QVERIFY(rootList.contains(FilePath::specialRootName()));
|
2022-05-31 11:16:44 +02:00
|
|
|
|
2022-11-29 10:28:37 +01:00
|
|
|
QDir schemes(FilePath::specialRootPath());
|
2022-09-21 15:25:53 +02:00
|
|
|
const QStringList schemeList = schemes.entryList();
|
2022-05-31 11:16:44 +02:00
|
|
|
QVERIFY(schemeList.contains("device"));
|
|
|
|
|
|
2023-02-22 15:58:18 +01:00
|
|
|
QDir devices(FilePath::specialDeviceRootPath());
|
|
|
|
|
const QStringList deviceList = devices.entryList();
|
|
|
|
|
QVERIFY(deviceList.contains("test"));
|
|
|
|
|
|
2022-11-29 10:28:37 +01:00
|
|
|
QDir deviceRoot(FilePath::specialDeviceRootPath() + "/test" + startWithSlash(QDir::rootPath()));
|
2022-09-21 15:25:53 +02:00
|
|
|
const QStringList deviceRootList = deviceRoot.entryList();
|
2022-05-31 11:16:44 +02:00
|
|
|
QVERIFY(!deviceRootList.isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testNotExistingFile()
|
|
|
|
|
{
|
|
|
|
|
QFile f(makeTestPath("test-does-not-exist.txt"));
|
|
|
|
|
|
|
|
|
|
QCOMPARE(f.open(QIODevice::ReadOnly), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testCreateFile()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
QFile f(makeTestPath("test-create-file.txt"));
|
|
|
|
|
QCOMPARE(f.exists(), false);
|
|
|
|
|
QVERIFY(f.open(QIODevice::WriteOnly));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile f(makeTestPath("test-create-file.txt"));
|
|
|
|
|
QCOMPARE(f.exists(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testCreateDir()
|
|
|
|
|
{
|
|
|
|
|
QDir d(makeTestPath({}));
|
|
|
|
|
QCOMPARE(d.mkdir("test-create-dir"), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString tst_fsengine::makeTestPath(QString path, bool asUrl)
|
|
|
|
|
{
|
2022-11-29 10:28:37 +01:00
|
|
|
if (asUrl)
|
2022-05-31 11:16:44 +02:00
|
|
|
return QString("device://test%1/tst_fsengine/%2").arg(tempFolder, path);
|
|
|
|
|
|
2022-11-29 10:28:37 +01:00
|
|
|
return QString(FilePath::specialDeviceRootPath() + "/test%1/tst_fsengine/%2")
|
2022-07-26 09:36:13 +02:00
|
|
|
.arg(startWithSlash(tempFolder), path);
|
2022-05-31 11:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testListDir()
|
|
|
|
|
{
|
|
|
|
|
QDir dd(makeTestPath({}));
|
|
|
|
|
QCOMPARE(dd.mkdir("test-list-dir"), true);
|
|
|
|
|
|
|
|
|
|
QDir d(makeTestPath("test-list-dir"));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
QFile f(makeTestPath("test-list-dir/f1.txt"));
|
|
|
|
|
QVERIFY(f.open(QIODevice::WriteOnly));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 15:25:53 +02:00
|
|
|
const QStringList list = d.entryList();
|
2022-05-31 11:16:44 +02:00
|
|
|
QVERIFY(list.contains("f1.txt"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testWindowsPaths()
|
|
|
|
|
{
|
|
|
|
|
// Test upper-case "C:"
|
|
|
|
|
QVERIFY(FilePath::fromString("C:/__qtc_devices__/device/{cd6c7e4b-12fd-43ca-9bb2-053a38e6b7c5}")
|
|
|
|
|
.needsDevice());
|
|
|
|
|
|
|
|
|
|
// Test lower-case "C:"
|
|
|
|
|
QVERIFY(FilePath::fromString("c:/__qtc_devices__/device/{cd6c7e4b-12fd-43ca-9bb2-053a38e6b7c5}")
|
|
|
|
|
.needsDevice());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testUrl()
|
|
|
|
|
{
|
|
|
|
|
FilePath p = FilePath::fromString(makeTestPath("", true));
|
|
|
|
|
|
|
|
|
|
QVERIFY(p.needsDevice());
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 15:12:19 +02:00
|
|
|
void tst_fsengine::testBrokenWindowsPath()
|
|
|
|
|
{
|
|
|
|
|
QTemporaryFile tmp;
|
|
|
|
|
QVERIFY(tmp.open());
|
|
|
|
|
|
|
|
|
|
QString localFileName = tmp.fileName();
|
|
|
|
|
localFileName.insert(HostOsInfo::isWindowsHost() ? 2 : 0, '/');
|
|
|
|
|
|
|
|
|
|
QFile file(localFileName);
|
|
|
|
|
QVERIFY(file.open(QIODevice::ReadOnly));
|
|
|
|
|
QCOMPARE(tmp.fileName(), QFileInfo(localFileName).canonicalFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:02:44 +02:00
|
|
|
void tst_fsengine::testRead()
|
|
|
|
|
{
|
|
|
|
|
QTemporaryFile tmp;
|
|
|
|
|
QVERIFY(tmp.open());
|
|
|
|
|
|
|
|
|
|
const QByteArray data = "Hello World!";
|
|
|
|
|
|
|
|
|
|
tmp.write(data);
|
|
|
|
|
tmp.flush();
|
|
|
|
|
|
2022-11-29 10:28:37 +01:00
|
|
|
QFile file(FilePath::specialDeviceRootPath() + "/test" + tmp.fileName());
|
2022-09-22 08:02:44 +02:00
|
|
|
QVERIFY(file.open(QIODevice::ReadOnly));
|
|
|
|
|
QCOMPARE(file.readAll(), data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_fsengine::testWrite()
|
|
|
|
|
{
|
|
|
|
|
QTemporaryDir dir;
|
|
|
|
|
const QString path = dir.path() + "/testWrite.txt";
|
|
|
|
|
const QByteArray data = "Hello World!";
|
|
|
|
|
{
|
2022-11-29 10:28:37 +01:00
|
|
|
QFile file(FilePath::specialDeviceRootPath() + "/test" + path);
|
2022-09-22 08:02:44 +02:00
|
|
|
QVERIFY(file.open(QIODevice::WriteOnly));
|
|
|
|
|
QCOMPARE(file.write(data), qint64(data.size()));
|
|
|
|
|
}
|
|
|
|
|
QFile f(path);
|
|
|
|
|
QVERIFY(f.open(QIODevice::ReadOnly));
|
|
|
|
|
QCOMPARE(f.readAll(), data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
QTEST_GUILESS_MAIN(tst_fsengine)
|
|
|
|
|
#include "tst_fsengine.moc"
|