Skip to content

Commit

Permalink
Substitute tilde with USERPROFILE on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hifi authored and droidmonkey committed Jul 18, 2020
1 parent 005d9d3 commit 7c39907
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions tests/TestTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down

0 comments on commit 7c39907

Please sign in to comment.