Substitute tilde with USERPROFILE on Windows

The substitution is now more shell-like and tilde is only replaced
from the beginning of the path if it is trailed by a slash.
This commit is contained in:
Toni Spets 2020-07-17 19:36:24 +03:00 committed by Jonathan White
parent 005d9d368f
commit 7c39907251
2 changed files with 9 additions and 1 deletions

View File

@ -331,11 +331,15 @@ namespace Tools
#if defined(Q_OS_WIN)
QRegularExpression varRe("\\%([A-Za-z][A-Za-z0-9_]*)\\%");
QString homeEnv = "USERPROFILE";
#else
QRegularExpression varRe("\\$([A-Za-z][A-Za-z0-9_]*)");
subbed.replace("~", environment.value("HOME"));
QString homeEnv = "HOME";
#endif
if (subbed.startsWith("~/") || subbed.startsWith("~\\"))
subbed.replace(0, 1, environment.value(homeEnv));
QRegularExpressionMatch match;
do {

View File

@ -72,10 +72,14 @@ void TestTools::testEnvSubstitute()
#if defined(Q_OS_WIN)
environment.insert("HOMEDRIVE", "C:");
environment.insert("HOMEPATH", "\\Users\\User");
environment.insert("USERPROFILE", "C:\\Users\\User");
QCOMPARE(Tools::envSubstitute("%HOMEDRIVE%%HOMEPATH%\\.ssh\\id_rsa", environment),
QString("C:\\Users\\User\\.ssh\\id_rsa"));
QCOMPARE(Tools::envSubstitute("start%EMPTY%%EMPTY%%%HOMEDRIVE%%end", environment), QString("start%C:%end"));
QCOMPARE(Tools::envSubstitute("%USERPROFILE%\\.ssh\\id_rsa", environment),
QString("C:\\Users\\User\\.ssh\\id_rsa"));
QCOMPARE(Tools::envSubstitute("~\\.ssh\\id_rsa", environment), QString("C:\\Users\\User\\.ssh\\id_rsa"));
#else
environment.insert("HOME", QString("/home/user"));
environment.insert("USER", QString("user"));