Utils: Fix FilePath::isRootPath()

Change-Id: I287bae74469ba501ecb03d51f04f7aaa5f4a7268
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-08-10 06:44:12 +02:00
parent 4605bf57dd
commit 324e924178
2 changed files with 61 additions and 6 deletions

View File

@@ -204,7 +204,27 @@ FilePath FilePath::currentWorkingPath()
bool FilePath::isRootPath() const
{
// FIXME: Make host-independent
if (needsDevice()) {
QStringView path = pathView();
if (osType() != OsTypeWindows)
return path == QLatin1String("/");
// Remote windows paths look like this: "/c:/", so we remove the leading '/'
if (path.startsWith('/'))
path = path.mid(1);
if (path.length() > 3)
return false;
if (!startsWithDriveLetter())
return false;
if (path.length() == 3 && path[2] != QLatin1Char('/'))
return false;
return true;
}
return *this == FilePath::fromString(QDir::rootPath());
}
@@ -1346,15 +1366,15 @@ bool FilePath::contains(const QString &s) const
/*!
\brief Checks whether the FilePath starts with a drive letter.
Defaults to \c false if it is a non-Windows host or represents a path on device
Returns whether FilePath starts with a drive letter
*/
bool FilePath::startsWithDriveLetter() const
{
const QStringView p = pathView();
return !needsDevice() && p.size() >= 2 && isWindowsDriveLetter(p[0]) && p.at(1) == ':';
QStringView p = pathView();
if (needsDevice() && !p.isEmpty())
p = p.mid(1);
return p.size() >= 2 && isWindowsDriveLetter(p[0]) && p.at(1) == ':';
}
/*!

View File

@@ -115,6 +115,8 @@ private slots:
void sort();
void sort_data();
void isRootPath();
private:
QTemporaryDir tempDir;
QString rootPath;
@@ -1308,6 +1310,14 @@ void tst_filepath::startsWithDriveLetter_data()
QTest::newRow("simple-win") << FilePath::fromString("c:/a") << true;
QTest::newRow("simple-linux") << FilePath::fromString("/c:/a") << false;
QTest::newRow("relative") << FilePath("a/b") << false;
QTest::newRow("remote-slash") << FilePath::fromString("docker://1234/") << false;
QTest::newRow("remote-single-letter") << FilePath::fromString("docker://1234/c") << false;
QTest::newRow("remote-drive") << FilePath::fromString("docker://1234/c:") << true;
QTest::newRow("remote-invalid-drive") << FilePath::fromString("docker://1234/c:a") << true;
QTest::newRow("remote-with-path") << FilePath::fromString("docker://1234/c:/a") << true;
QTest::newRow("remote-z") << FilePath::fromString("docker://1234/z:") << true;
QTest::newRow("remote-1") << FilePath::fromString("docker://1234/1:") << false;
}
void tst_filepath::startsWithDriveLetter()
@@ -1656,6 +1666,31 @@ void tst_filepath::sort()
QCOMPARE(sortedPaths, sorted);
}
void tst_filepath::isRootPath()
{
FilePath localRoot = FilePath::fromString(QDir::rootPath());
QVERIFY(localRoot.isRootPath());
FilePath localNonRoot = FilePath::fromString(QDir::rootPath() + "x");
QVERIFY(!localNonRoot.isRootPath());
if (HostOsInfo::isWindowsHost()) {
FilePath remoteWindowsRoot = FilePath::fromString("device://test/c:/");
QVERIFY(remoteWindowsRoot.isRootPath());
FilePath remoteWindowsRoot1 = FilePath::fromString("device://test/c:");
QVERIFY(remoteWindowsRoot1.isRootPath());
FilePath remoteWindowsNotRoot = FilePath::fromString("device://test/c:/x");
QVERIFY(!remoteWindowsNotRoot.isRootPath());
} else {
FilePath remoteRoot = FilePath::fromString("device://test/");
QVERIFY(remoteRoot.isRootPath());
FilePath remotePath = FilePath::fromString("device://test/x");
QVERIFY(!remotePath.isRootPath());
}
}
void tst_filepath::sort_data()
{
QTest::addColumn<QStringList>("input");