forked from qt-creator/qt-creator
Utils: Adjust FilePath::relativePath()
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 <eike.ziller@qt.io> Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Jochen Becher <jochen_becher@gmx.de> Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -1043,6 +1043,8 @@ FilePath FilePath::relativePath(const FilePath &anchor) const
|
|||||||
return {};
|
return {};
|
||||||
QString relativeFilePath = calcRelativePath(absolutePath, absoluteAnchorPath);
|
QString relativeFilePath = calcRelativePath(absolutePath, absoluteAnchorPath);
|
||||||
if (!filename.isEmpty()) {
|
if (!filename.isEmpty()) {
|
||||||
|
if (relativeFilePath == ".")
|
||||||
|
relativeFilePath.clear();
|
||||||
if (!relativeFilePath.isEmpty())
|
if (!relativeFilePath.isEmpty())
|
||||||
relativeFilePath += '/';
|
relativeFilePath += '/';
|
||||||
relativeFilePath += filename;
|
relativeFilePath += filename;
|
||||||
@@ -1093,6 +1095,8 @@ QString FilePath::calcRelativePath(const QString &absolutePath, const QString &a
|
|||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
if (relativePath.isEmpty())
|
||||||
|
return QString(".");
|
||||||
return relativePath;
|
return relativePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -213,13 +213,13 @@ void tst_fileutils::calcRelativePath_data()
|
|||||||
QTest::newRow("empty") << "" << "" << "";
|
QTest::newRow("empty") << "" << "" << "";
|
||||||
QTest::newRow("leftempty") << "" << "/" << "";
|
QTest::newRow("leftempty") << "" << "/" << "";
|
||||||
QTest::newRow("rightempty") << "/" << "" << "";
|
QTest::newRow("rightempty") << "/" << "" << "";
|
||||||
QTest::newRow("root") << "/" << "/" << "";
|
QTest::newRow("root") << "/" << "/" << ".";
|
||||||
QTest::newRow("simple1") << "/a" << "/" << "a";
|
QTest::newRow("simple1") << "/a" << "/" << "a";
|
||||||
QTest::newRow("simple2") << "/" << "/a" << "..";
|
QTest::newRow("simple2") << "/" << "/a" << "..";
|
||||||
QTest::newRow("simple3") << "/a" << "/a" << "";
|
QTest::newRow("simple3") << "/a" << "/a" << ".";
|
||||||
QTest::newRow("extraslash1") << "/a/b/c" << "/a/b/c" << "";
|
QTest::newRow("extraslash1") << "/a/b/c" << "/a/b/c" << ".";
|
||||||
QTest::newRow("extraslash2") << "/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("extraslash3") << "/a/b/c/" << "/a/b/c" << ".";
|
||||||
QTest::newRow("normal1") << "/a/b/c" << "/a/x" << "../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("normal2") << "/a/b/c" << "/a/x/y" << "../../b/c";
|
||||||
QTest::newRow("normal3") << "/a/b/c" << "/x/y" << "../../a/b/c";
|
QTest::newRow("normal3") << "/a/b/c" << "/x/y" << "../../a/b/c";
|
||||||
@@ -246,7 +246,9 @@ void tst_fileutils::relativePath_data()
|
|||||||
QTest::addColumn<QString>("anchor");
|
QTest::addColumn<QString>("anchor");
|
||||||
QTest::addColumn<QString>("result");
|
QTest::addColumn<QString>("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_1") << "a/b/c/d" << "a/x/y/z" << "../../../b/c/d";
|
||||||
QTest::newRow("dir2dir_2") << "a/b" <<"a/b/c" << "..";
|
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";
|
QTest::newRow("file2file_1") << "a/b/c/d/file1.txt" << "a/file3.txt" << "b/c/d/file1.txt";
|
||||||
|
Reference in New Issue
Block a user