From 28d2674b54d51f48950576f6748f2f38a4cacbbb Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Tue, 17 Aug 2021 08:49:57 +0200 Subject: [PATCH] Utils: Adjust FilePath::relativePath() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using the same path for the anchor and the relative path we end up in an empty path which is conceptually wrong. Especially when passing such a result directly without further checking may lead to bad issues. Return a path containing just the '.' (dot) instead. Change-Id: I86e1ab91e6610831fc05bd81776d278e9fe10105 Reviewed-by: Eike Ziller Reviewed-by: André Hartmann Reviewed-by: Jochen Becher Reviewed-by: hjk --- src/libs/utils/filepath.cpp | 4 ++++ tests/auto/utils/fileutils/tst_fileutils.cpp | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index d219471c871..103804b0b0c 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -1043,6 +1043,8 @@ FilePath FilePath::relativePath(const FilePath &anchor) const return {}; QString relativeFilePath = calcRelativePath(absolutePath, absoluteAnchorPath); if (!filename.isEmpty()) { + if (relativeFilePath == ".") + relativeFilePath.clear(); if (!relativeFilePath.isEmpty()) relativeFilePath += '/'; relativeFilePath += filename; @@ -1093,6 +1095,8 @@ QString FilePath::calcRelativePath(const QString &absolutePath, const QString &a } ++i; } + if (relativePath.isEmpty()) + return QString("."); return relativePath; } diff --git a/tests/auto/utils/fileutils/tst_fileutils.cpp b/tests/auto/utils/fileutils/tst_fileutils.cpp index cb4f30d79d0..28e79b01e5f 100644 --- a/tests/auto/utils/fileutils/tst_fileutils.cpp +++ b/tests/auto/utils/fileutils/tst_fileutils.cpp @@ -213,13 +213,13 @@ void tst_fileutils::calcRelativePath_data() QTest::newRow("empty") << "" << "" << ""; QTest::newRow("leftempty") << "" << "/" << ""; QTest::newRow("rightempty") << "/" << "" << ""; - QTest::newRow("root") << "/" << "/" << ""; + QTest::newRow("root") << "/" << "/" << "."; QTest::newRow("simple1") << "/a" << "/" << "a"; QTest::newRow("simple2") << "/" << "/a" << ".."; - QTest::newRow("simple3") << "/a" << "/a" << ""; - QTest::newRow("extraslash1") << "/a/b/c" << "/a/b/c" << ""; - QTest::newRow("extraslash2") << "/a/b/c" << "/a/b/c/" << ""; - QTest::newRow("extraslash3") << "/a/b/c/" << "/a/b/c" << ""; + QTest::newRow("simple3") << "/a" << "/a" << "."; + QTest::newRow("extraslash1") << "/a/b/c" << "/a/b/c" << "."; + QTest::newRow("extraslash2") << "/a/b/c" << "/a/b/c/" << "."; + QTest::newRow("extraslash3") << "/a/b/c/" << "/a/b/c" << "."; QTest::newRow("normal1") << "/a/b/c" << "/a/x" << "../b/c"; QTest::newRow("normal2") << "/a/b/c" << "/a/x/y" << "../../b/c"; QTest::newRow("normal3") << "/a/b/c" << "/x/y" << "../../a/b/c"; @@ -246,7 +246,9 @@ void tst_fileutils::relativePath_data() QTest::addColumn("anchor"); QTest::addColumn("result"); - QTest::newRow("samedir") << "/" << "/" << ""; + QTest::newRow("samedir") << "/" << "/" << "."; + QTest::newRow("samedir_but_file") << "a/b/c/d/file1.txt" << "a/b/c/d" << "file1.txt"; + QTest::newRow("samedir_but_file2") << "a/b/c/d" << "a/b/c/d/file1.txt" << "."; QTest::newRow("dir2dir_1") << "a/b/c/d" << "a/x/y/z" << "../../../b/c/d"; QTest::newRow("dir2dir_2") << "a/b" <<"a/b/c" << ".."; QTest::newRow("file2file_1") << "a/b/c/d/file1.txt" << "a/file3.txt" << "b/c/d/file1.txt";