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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <utils/filepath.h>
#include <utils/fsengine/fsengine.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <QDebug>
#include <QFileSystemModel>
#include <QTemporaryFile>
#include <QTest>
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();
void testRead();
void testWrite();
void testRootFromDotDot();
void testDirtyPaths();
private:
QString makeTestPath(QString path, bool asUrl = false);
private:
FSEngine engine;
QString tempFolder;
};
template<class... Args>
using Continuation = std::function<void(Args...)>;
QString startWithSlash(QString s)
{
if (!s.startsWith('/'))
s.prepend('/');
return s;
}
void tst_fsengine::initTestCase()
{
if (!FSEngine::isAvailable())
QSKIP("Utils was built without Filesystem Engine");
if (HostOsInfo::isWindowsHost())
QSKIP("The fsengine tests are not supported on Windows.");
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");
QCOMPARE(p.scheme(), u"device");
QCOMPARE(p.host(), u"test");
QCOMPARE(p.path(), u"/test.txt");
QString asString = p.toFSPathString();
QCOMPARE(asString, FilePath::specialDeviceRootPath() + "/test/test.txt");
FilePath p2 = FilePath::fromString(asString);
QCOMPARE(p2.scheme(), u"device");
QCOMPARE(p2.host(), u"test");
QCOMPARE(p2.path(), u"/test.txt");
}
void tst_fsengine::testRootPathContainsFakeDir()
{
const QStringList rootList = QDir::root().entryList();
QVERIFY(rootList.contains(FilePath::specialRootName()));
QDir schemes(FilePath::specialRootPath());
const QStringList schemeList = schemes.entryList();
QVERIFY(schemeList.contains("device"));
QDir devices(FilePath::specialDeviceRootPath());
const QStringList deviceList = devices.entryList();
QVERIFY(deviceList.contains("test"));
QDir deviceRoot(FilePath::specialDeviceRootPath() + "/test" + startWithSlash(QDir::rootPath()));
const QStringList deviceRootList = deviceRoot.entryList();
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)
{
if (asUrl)
return QString("device://test%1/tst_fsengine/%2").arg(tempFolder, path);
return QString(FilePath::specialDeviceRootPath() + "/test%1/tst_fsengine/%2")
.arg(startWithSlash(tempFolder))
.arg(path);
}
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));
}
const QStringList list = d.entryList();
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}")
.isLocal());
// Test lower-case "C:"
QVERIFY(!FilePath::fromString("c:/__qtc_devices__/device/{cd6c7e4b-12fd-43ca-9bb2-053a38e6b7c5}")
.isLocal());
}
void tst_fsengine::testUrl()
{
FilePath p = FilePath::fromString(makeTestPath("", true));
QVERIFY(!p.isLocal());
}
void tst_fsengine::testRead()
{
QTemporaryFile tmp;
QVERIFY(tmp.open());
const QByteArray data = "Hello World!";
tmp.write(data);
tmp.flush();
QFile file(FilePath::specialDeviceRootPath() + "/test" + tmp.fileName());
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!";
{
QFile file(FilePath::specialDeviceRootPath() + "/test" + path);
QVERIFY(file.open(QIODevice::WriteOnly));
QCOMPARE(file.write(data), qint64(data.size()));
}
QFile f(path);
QVERIFY(f.open(QIODevice::ReadOnly));
QCOMPARE(f.readAll(), data);
}
void tst_fsengine::testRootFromDotDot()
{
const QString path = QDir::rootPath() + "some-folder/..";
QFileInfo fInfo(path);
QCOMPARE(fInfo.fileName(), QString(".."));
QDir dRoot(path);
const auto dRootEntryList = dRoot.entryList();
QVERIFY(dRootEntryList.contains(FilePath::specialRootName()));
QFileInfo fInfo2(FilePath::specialRootPath() + "/xyz/..");
QCOMPARE(fInfo2.fileName(), "..");
QDir schemesWithDotDot(FilePath::specialRootPath() + "/xyz/..");
const QStringList schemeWithDotDotList = schemesWithDotDot.entryList();
QVERIFY(schemeWithDotDotList.contains("device"));
QFileInfo fInfo3(FilePath::specialDeviceRootPath() + "/xyz/..");
QCOMPARE(fInfo3.fileName(), "..");
QDir devicesWithDotDot(FilePath::specialDeviceRootPath() + "/test/..");
const QStringList deviceListWithDotDot = devicesWithDotDot.entryList();
QVERIFY(deviceListWithDotDot.contains("test"));
QFileInfo fInfo4(FilePath::specialDeviceRootPath() + "/test/tmp/..");
QCOMPARE(fInfo4.fileName(), "..");
}
void tst_fsengine::testDirtyPaths()
{
// "//__qtc_devices"
QVERIFY(QFileInfo("/" + FilePath::specialRootPath()).exists());
// "///__qtc_devices/device"
QVERIFY(QFileInfo("//" + FilePath::specialDeviceRootPath()).exists());
// "////__qtc_devices/device////test"
QVERIFY(QFileInfo("///" + FilePath::specialDeviceRootPath() + "////test").exists());
// "/////__qtc_devices/device/test/..."
QVERIFY(QFileInfo("////" + makeTestPath("")).exists());
}
QTEST_GUILESS_MAIN(tst_fsengine)
#include "tst_fsengine.moc"
|