FilePath: Fix absoluteFilePath to behave like absolutePath

Change-Id: Ibb2cba32c40ec9febab5f20c78156481525c8904
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-10-11 15:50:42 +02:00
parent 82b2b2cae9
commit c50891611a
2 changed files with 35 additions and 15 deletions

View File

@@ -785,9 +785,9 @@ FilePath FilePath::absolutePath() const
FilePath FilePath::absoluteFilePath() const FilePath FilePath::absoluteFilePath() const
{ {
if (isAbsolutePath()) if (isAbsolutePath())
return *this; return cleanPath();
if (!needsDevice() && isEmpty()) if (!needsDevice() && isEmpty())
return *this; return cleanPath();
return FilePath::currentWorkingPath().resolvePath(*this); return FilePath::currentWorkingPath().resolvePath(*this);
} }

View File

@@ -46,8 +46,8 @@ private slots:
void relativePath_data(); void relativePath_data();
void relativePath(); void relativePath();
void absoluteFilePath_data(); void absolute_data();
void absoluteFilePath(); void absolute();
void fromToString_data(); void fromToString_data();
void fromToString(); void fromToString();
@@ -379,26 +379,46 @@ void tst_fileutils::schemeAndHostLength()
QCOMPARE(actual, result); QCOMPARE(actual, result);
} }
void tst_fileutils::absoluteFilePath_data() void tst_fileutils::absolute_data()
{ {
QTest::addColumn<FilePath>("path"); QTest::addColumn<FilePath>("path");
QTest::addColumn<FilePath>("result"); QTest::addColumn<FilePath>("absoluteFilePath");
QTest::addColumn<FilePath>("absolutePath");
QTest::newRow("absolute1") << FilePath::fromString("/") << FilePath::fromString("/"); QTest::newRow("absolute1") << FilePath::fromString("/")
QTest::newRow("absolute2") << FilePath::fromString("C:/a/b") << FilePath::fromString("C:/a/b"); << FilePath::fromString("/")
QTest::newRow("absolute3") << FilePath::fromString("/a/b") << FilePath::fromString("/a/b"); << FilePath::fromString("/");
QTest::newRow("default-constructed") << FilePath() << FilePath(); QTest::newRow("absolute2") << FilePath::fromString("C:/a/b")
<< FilePath::fromString("C:/a/b")
<< FilePath::fromString("C:/a");
QTest::newRow("absolute3") << FilePath::fromString("/a/b")
<< FilePath::fromString("/a/b")
<< FilePath::fromString("/a");
QTest::newRow("absolute4") << FilePath::fromString("/a/b/..")
<< FilePath::fromString("/a")
<< FilePath::fromString("/");
QTest::newRow("absolute5") << FilePath::fromString("/a/b/c/../d")
<< FilePath::fromString("/a/b/d")
<< FilePath::fromString("/a/b");
QTest::newRow("absolute6") << FilePath::fromString("/a/../b/c/d")
<< FilePath::fromString("/b/c/d")
<< FilePath::fromString("/b/c");
QTest::newRow("default-constructed") << FilePath() << FilePath() << FilePath();
QTest::newRow("relative") << FilePath::fromString("a/b") QTest::newRow("relative") << FilePath::fromString("a/b")
<< FilePath::fromString(QDir::currentPath() + "/a/b"); << FilePath::fromString(QDir::currentPath() + "/a/b")
<< FilePath::fromString(QDir::currentPath() + "/a");
QTest::newRow("qrc") << FilePath::fromString(":/foo/bar.txt") QTest::newRow("qrc") << FilePath::fromString(":/foo/bar.txt")
<< FilePath::fromString(":/foo/bar.txt"); << FilePath::fromString(":/foo/bar.txt")
<< FilePath::fromString(":/foo");
} }
void tst_fileutils::absoluteFilePath() void tst_fileutils::absolute()
{ {
QFETCH(FilePath, path); QFETCH(FilePath, path);
QFETCH(FilePath, result); QFETCH(FilePath, absoluteFilePath);
QCOMPARE(path.absoluteFilePath(), result); QFETCH(FilePath, absolutePath);
QCOMPARE(path.absoluteFilePath(), absoluteFilePath);
QCOMPARE(path.absolutePath(), absolutePath);
} }
void tst_fileutils::toString_data() void tst_fileutils::toString_data()