From c7c99d5badc82e5271a11c9aab7b86286f3a1cec Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 2 Sep 2024 15:44:26 +0200 Subject: [PATCH] PathChooser: Fix handling of ~ - Just "~" should be expanded to home path, not requiring a slash "~/" - Be resistant to spaces at start and end. This is usually a user mistake. Fixes: QTCREATORBUG-31432 Change-Id: I7a286b9d2bc426e6950782f96725bb5d8fbbc075 Reviewed-by: Marcus Tillmanns --- src/libs/utils/filepath.cpp | 3 ++- src/libs/utils/pathchooser.cpp | 2 +- tests/auto/utils/filepath/tst_filepath.cpp | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index 0613395f2e6..aabeeab6f95 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -1430,7 +1430,8 @@ FilePath FilePath::fromStringWithExtension(const QString &filepath, const QStrin */ FilePath FilePath::fromUserInput(const QString &filePath) { - const QString expandedPath = filePath.startsWith("~/") + const QString expandedPath = filePath == "~" ? QDir::homePath() + : filePath.startsWith("~/") ? (QDir::homePath() + "/" + filePath.mid(2)) : filePath; return FilePath::fromString(doCleanPath(expandedPath)); diff --git a/src/libs/utils/pathchooser.cpp b/src/libs/utils/pathchooser.cpp index 8e941f52a34..26c190730ce 100644 --- a/src/libs/utils/pathchooser.cpp +++ b/src/libs/utils/pathchooser.cpp @@ -344,7 +344,7 @@ void PathChooser::setEnvironment(const Environment &env) FilePath PathChooser::unexpandedFilePath() const { - return FilePath::fromUserInput(d->m_lineEdit->text()); + return FilePath::fromUserInput(d->m_lineEdit->text().trimmed()); } FilePath PathChooser::filePath() const diff --git a/tests/auto/utils/filepath/tst_filepath.cpp b/tests/auto/utils/filepath/tst_filepath.cpp index 369fb6a2584..b90615a5483 100644 --- a/tests/auto/utils/filepath/tst_filepath.cpp +++ b/tests/auto/utils/filepath/tst_filepath.cpp @@ -873,7 +873,7 @@ void tst_filepath::fromUserInput_data() 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("tilde-only") << D("~", "", "", QDir::homePath()); QTest::newRow("unc-incomplete") << D("//", "", "", "//"); QTest::newRow("unc-incomplete-only-server") << D("//server", "", "", "//server");