forked from qt-creator/qt-creator
Utils: Remove slash normalization in critical path
Should be done by callers, most notably fromUserInput Change-Id: I0dec8e3ab76d49d0ddf4da3088499e71c536ab83 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io> Reviewed-by: hjk <hjk@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
@@ -810,15 +810,11 @@ void FilePath::setPath(QStringView path)
|
||||
setParts(scheme(), host(), path);
|
||||
}
|
||||
|
||||
void FilePath::setFromString(const QString &unnormalizedFileName)
|
||||
void FilePath::setFromString(const QString &fileName)
|
||||
{
|
||||
static const QStringView qtcDevSlash(u"__qtc_devices__/");
|
||||
static const QStringView colonSlashSlash(u"://");
|
||||
|
||||
QString fileName = unnormalizedFileName;
|
||||
if (fileName.contains('\\'))
|
||||
fileName.replace('\\', '/');
|
||||
|
||||
const QChar slash('/');
|
||||
const QStringView fileNameView(fileName);
|
||||
|
||||
@@ -926,7 +922,7 @@ FilePath FilePath::fromVariant(const QVariant &variant)
|
||||
{
|
||||
if (variant.type() == QVariant::Url)
|
||||
return FilePath::fromUrl(variant.toUrl());
|
||||
return FilePath::fromString(variant.toString());
|
||||
return FilePath::fromUserInput(variant.toString());
|
||||
}
|
||||
|
||||
QVariant FilePath::toVariant() const
|
||||
|
@@ -58,6 +58,9 @@ private slots:
|
||||
void fromString_data();
|
||||
void fromString();
|
||||
|
||||
void fromUserInput_data();
|
||||
void fromUserInput();
|
||||
|
||||
void toString_data();
|
||||
void toString();
|
||||
|
||||
@@ -205,7 +208,7 @@ void tst_fileutils::parentDir()
|
||||
QFETCH(QString, parentPath);
|
||||
QFETCH(QString, expectFailMessage);
|
||||
|
||||
FilePath result = FilePath::fromString(path).parentDir();
|
||||
FilePath result = FilePath::fromUserInput(path).parentDir();
|
||||
if (!expectFailMessage.isEmpty())
|
||||
QEXPECT_FAIL("", expectFailMessage.toUtf8().constData(), Continue);
|
||||
QCOMPARE(result.toString(), parentPath);
|
||||
@@ -245,8 +248,8 @@ void tst_fileutils::isChildOf()
|
||||
QFETCH(QString, childPath);
|
||||
QFETCH(bool, result);
|
||||
|
||||
const FilePath child = FilePath::fromString(childPath);
|
||||
const FilePath parent = FilePath::fromString(path);
|
||||
const FilePath child = FilePath::fromUserInput(childPath);
|
||||
const FilePath parent = FilePath::fromUserInput(path);
|
||||
|
||||
QCOMPARE(child.isChildOf(parent), result);
|
||||
}
|
||||
@@ -576,8 +579,8 @@ void tst_fileutils::fromString_data()
|
||||
QTest::newRow("unix-folder-with-trailing-slash") << D("/tmp/", "", "", "/tmp/");
|
||||
|
||||
QTest::newRow("windows-root") << D("c:", "", "", "c:");
|
||||
QTest::newRow("windows-folder") << D("c:\\Windows", "", "", "c:/Windows");
|
||||
QTest::newRow("windows-folder-with-trailing-slash") << D("c:\\Windows\\", "", "", "c:/Windows/");
|
||||
QTest::newRow("windows-folder") << D("c:/Windows", "", "", "c:/Windows");
|
||||
QTest::newRow("windows-folder-with-trailing-slash") << D("c:/Windows/", "", "", "c:/Windows/");
|
||||
QTest::newRow("windows-folder-slash") << D("C:/Windows", "", "", "C:/Windows");
|
||||
|
||||
QTest::newRow("docker-root-url") << D("docker://1234/", "docker", "1234", "/");
|
||||
@@ -633,6 +636,88 @@ void tst_fileutils::fromString()
|
||||
QCOMPARE(filePath.path(), data.path);
|
||||
}
|
||||
|
||||
void tst_fileutils::fromUserInput_data()
|
||||
{
|
||||
using D = FromStringData;
|
||||
QTest::addColumn<D>("data");
|
||||
|
||||
QTest::newRow("empty") << D("", "", "", "");
|
||||
QTest::newRow("single-colon") << D(":", "", "", ":");
|
||||
QTest::newRow("single-slash") << D("/", "", "", "/");
|
||||
QTest::newRow("single-char") << D("a", "", "", "a");
|
||||
QTest::newRow("relative") << D("./rel", "", "", "rel");
|
||||
QTest::newRow("qrc") << D(":/test.txt", "", "", ":/test.txt");
|
||||
QTest::newRow("qrc-no-slash") << D(":test.txt", "", "", ":test.txt");
|
||||
|
||||
QTest::newRow("unc-incomplete") << D("//", "", "", "//");
|
||||
QTest::newRow("unc-incomplete-only-server") << D("//server", "", "", "//server");
|
||||
QTest::newRow("unc-incomplete-only-server-2") << D("//server/", "", "", "//server/");
|
||||
QTest::newRow("unc-server-and-share") << D("//server/share", "", "", "//server/share");
|
||||
QTest::newRow("unc-server-and-share-2") << D("//server/share/", "", "", "//server/share");
|
||||
QTest::newRow("unc-full") << D("//server/share/test.txt", "", "", "//server/share/test.txt");
|
||||
|
||||
QTest::newRow("unix-root") << D("/", "", "", "/");
|
||||
QTest::newRow("unix-folder") << D("/tmp", "", "", "/tmp");
|
||||
QTest::newRow("unix-folder-with-trailing-slash") << D("/tmp/", "", "", "/tmp");
|
||||
|
||||
QTest::newRow("windows-root") << D("c:", "", "", "c:");
|
||||
QTest::newRow("windows-folder") << D("c:/Windows", "", "", "c:/Windows");
|
||||
QTest::newRow("windows-folder-with-trailing-slash") << D("c:\\Windows\\", "", "", "c:/Windows");
|
||||
QTest::newRow("windows-folder-slash") << D("C:/Windows", "", "", "C:/Windows");
|
||||
|
||||
QTest::newRow("docker-root-url") << D("docker://1234/", "docker", "1234", "/");
|
||||
QTest::newRow("docker-root-url-special-linux") << D("/__qtc_devices__/docker/1234/", "docker", "1234", "/");
|
||||
QTest::newRow("docker-root-url-special-win") << D("c:/__qtc_devices__/docker/1234/", "docker", "1234", "/");
|
||||
QTest::newRow("docker-relative-path") << D("docker://1234/./rel", "docker", "1234", "rel", FailEverywhere);
|
||||
|
||||
QTest::newRow("qtc-dev-linux") << D("/__qtc_devices__", "", "", "/__qtc_devices__");
|
||||
QTest::newRow("qtc-dev-win") << D("c:/__qtc_devices__", "", "", "c:/__qtc_devices__");
|
||||
QTest::newRow("qtc-dev-type-linux") << D("/__qtc_devices__/docker", "", "", "/__qtc_devices__/docker");
|
||||
QTest::newRow("qtc-dev-type-win") << D("c:/__qtc_devices__/docker", "", "", "c:/__qtc_devices__/docker");
|
||||
QTest::newRow("qtc-dev-type-dev-linux") << D("/__qtc_devices__/docker/1234", "docker", "1234", "/");
|
||||
QTest::newRow("qtc-dev-type-dev-win") << D("c:/__qtc_devices__/docker/1234", "docker", "1234", "/");
|
||||
|
||||
// "Remote Windows" is currently truly not supported.
|
||||
QTest::newRow("cross-os-linux")
|
||||
<< D("/__qtc_devices__/docker/1234/c:/test.txt", "docker", "1234", "c:/test.txt", FailEverywhere);
|
||||
QTest::newRow("cross-os-win")
|
||||
<< D("c:/__qtc_devices__/docker/1234/c:/test.txt", "docker", "1234", "c:/test.txt", FailEverywhere);
|
||||
QTest::newRow("cross-os-unclean-linux")
|
||||
<< D("/__qtc_devices__/docker/1234/c:\\test.txt", "docker", "1234", "c:/test.txt", FailEverywhere);
|
||||
QTest::newRow("cross-os-unclean-win")
|
||||
<< D("c:/__qtc_devices__/docker/1234/c:\\test.txt", "docker", "1234", "c:/test.txt", FailEverywhere);
|
||||
|
||||
QTest::newRow("unc-full-in-docker-linux")
|
||||
<< D("/__qtc_devices__/docker/1234//server/share/test.txt", "docker", "1234", "//server/share/test.txt", FailEverywhere);
|
||||
QTest::newRow("unc-full-in-docker-win")
|
||||
<< D("c:/__qtc_devices__/docker/1234//server/share/test.txt", "docker", "1234", "//server/share/test.txt", FailEverywhere);
|
||||
|
||||
QTest::newRow("unc-dos-1") << D("//?/c:", "", "", "c:");
|
||||
QTest::newRow("unc-dos-com") << D("//./com1", "", "", "//./com1");
|
||||
}
|
||||
|
||||
void tst_fileutils::fromUserInput()
|
||||
{
|
||||
QFETCH(FromStringData, data);
|
||||
|
||||
FilePath filePath = FilePath::fromUserInput(data.input);
|
||||
|
||||
bool expectFail = ((data.expectedPass & FailOnLinux) && !HostOsInfo::isWindowsHost())
|
||||
|| ((data.expectedPass & FailOnWindows) && HostOsInfo::isWindowsHost());
|
||||
|
||||
if (expectFail) {
|
||||
QString actual = filePath.scheme() + '|' + filePath.host() + '|' + filePath.path();
|
||||
QString expected = data.scheme + '|' + data.host + '|' + data.path;
|
||||
QEXPECT_FAIL("", "", Continue);
|
||||
QCOMPARE(actual, expected);
|
||||
return;
|
||||
}
|
||||
|
||||
QCOMPARE(filePath.scheme(), data.scheme);
|
||||
QCOMPARE(filePath.host(), data.host);
|
||||
QCOMPARE(filePath.path(), data.path);
|
||||
}
|
||||
|
||||
void tst_fileutils::fromToString_data()
|
||||
{
|
||||
QTest::addColumn<QString>("scheme");
|
||||
|
Reference in New Issue
Block a user