From 37ec527e6d74d4bc816e1868919a97ac8a398cae Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 22 Feb 2023 11:58:17 +0100 Subject: [PATCH] Fix tilde expansion in FilePath::fromUserInput It was not working for just "~/" without additional path component. Change-Id: I559a7771fc09d2e604f567548585a668e8809729 Reviewed-by: Marcus Tillmanns Reviewed-by: --- src/libs/utils/filepath.cpp | 8 ++++---- tests/auto/utils/fileutils/tst_fileutils.cpp | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index db8008182f5..0e46b61c7d8 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -1038,10 +1038,10 @@ FilePath FilePath::fromStringWithExtension(const QString &filepath, const QStrin */ FilePath FilePath::fromUserInput(const QString &filePath) { - QString clean = doCleanPath(filePath); - if (clean.startsWith(QLatin1String("~/"))) - return FileUtils::homePath().pathAppended(clean.mid(2)); - return FilePath::fromString(clean); + const QString expandedPath = filePath.startsWith("~/") + ? (QDir::homePath() + "/" + filePath.mid(2)) + : filePath; + return FilePath::fromString(doCleanPath(expandedPath)); } /*! diff --git a/tests/auto/utils/fileutils/tst_fileutils.cpp b/tests/auto/utils/fileutils/tst_fileutils.cpp index 033cf680bea..334a7c99e36 100644 --- a/tests/auto/utils/fileutils/tst_fileutils.cpp +++ b/tests/auto/utils/fileutils/tst_fileutils.cpp @@ -654,6 +654,9 @@ void tst_fileutils::fromUserInput_data() 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("tilde") << D("~/", "", "", QDir::homePath()); + QTest::newRow("tilde-with-path") << D("~/foo", "", "", QDir::homePath() + "/foo"); + QTest::newRow("tilde-only") << D("~", "", "", "~"); QTest::newRow("unc-incomplete") << D("//", "", "", "//"); QTest::newRow("unc-incomplete-only-server") << D("//server", "", "", "//server");