diff --git a/outputview/outputexecutejob.cpp b/outputview/outputexecutejob.cpp --- a/outputview/outputexecutejob.cpp +++ b/outputview/outputexecutejob.cpp @@ -438,7 +438,9 @@ if( environmentProfile.isEmpty() ) { environmentProfile = environmentGroup.defaultGroup(); } - OutputExecuteJobPrivate::mergeEnvironment( environment, environmentGroup.variables( environmentProfile ) ); + QMap userEnv = environmentGroup.variables( environmentProfile ); + expandVariables ( userEnv, environment ); + OutputExecuteJobPrivate::mergeEnvironment( environment, userEnv ); OutputExecuteJobPrivate::mergeEnvironment( environment, m_environmentOverrides ); if( m_properties.testFlag( OutputExecuteJob::PortableMessages ) ) { environment.remove( "LC_ALL" ); diff --git a/util/environmentgrouplist.h b/util/environmentgrouplist.h --- a/util/environmentgrouplist.h +++ b/util/environmentgrouplist.h @@ -23,8 +23,8 @@ #include "utilexport.h" #include - -class KConfig; +#include + class KConfig; template class QMap; class QString; class QStringList; @@ -139,7 +139,7 @@ class EnvironmentGroupListPrivate* const d; }; - +KDEVPLATFORMUTIL_EXPORT void expandVariables ( QMap& variables, QProcessEnvironment& environment ); } #endif diff --git a/util/environmentgrouplist.cpp b/util/environmentgrouplist.cpp --- a/util/environmentgrouplist.cpp +++ b/util/environmentgrouplist.cpp @@ -25,6 +25,7 @@ #include +#include namespace KDevelop { class EnvironmentGroupListPrivate @@ -201,3 +202,21 @@ return env; } + +void KDevelop::expandVariables ( QMap& variables, QProcessEnvironment& environment ) +{ + QRegularExpression rVar("(? + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public License +along with this library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#ifndef TESTENVIRONMENT_H +#define TESTENVIRONMENT_H +#include + +class TestEnvironment : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testBasic_data(); + void testBasic(); +}; + +#endif // TESTENVIRONMENT_H diff --git a/util/tests/test_environment.cpp b/util/tests/test_environment.cpp new file mode 100644 --- /dev/null +++ b/util/tests/test_environment.cpp @@ -0,0 +1,56 @@ +/* This file is part of KDevelop +Copyright 2015 Artur Puzio + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public License +along with this library; see the file COPYING.LIB. If not, write to +the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +Boston, MA 02110-1301, USA. +*/ + +#include "test_environment.h" + +#include "util/environmentgrouplist.h" + +#include + +QTEST_MAIN(TestEnvironment); + +using ProcEnv = QMap; + +void TestEnvironment::testBasic_data() +{ + QTest::addColumn("test"); + QTest::addColumn("goodAns"); + + QTest::newRow("simple variables") << ProcEnv({{"VAR1","data"},{"Var2","some other data"}}) << ProcEnv({{"VAR1","data"},{"Var2","some other data"}}); + QTest::newRow("no variables") << ProcEnv({}) << ProcEnv({}); + QTest::newRow("PATH append and prepend") << ProcEnv({{"PATH","/home/usr/bin:$PATH:/home/user/folder"}}) << ProcEnv({{"PATH", "/home/usr/bin:/bin:/usr/bin:/home/user/folder"}}); + QTest::newRow("\\$VAR") << ProcEnv({{"MY_VAR","\\$PATH something \\$HOME"}}) << ProcEnv({{"MY_VAR","$PATH something $HOME"}}); + QTest::newRow("spaces, \\$VAR after $VAR") << ProcEnv({{"MY_VAR","$PATH:$HOME something \\$HOME"}}) << ProcEnv({{"MY_VAR","/bin:/usr/bin:/home/tom something $HOME"}}); + QTest::newRow("VAR2=$VAR1") << ProcEnv({{"VAR1","/some/path"},{"VAR2","$VAR1"}}) << ProcEnv({{"VAR1","/some/path"},{"VAR2",""}}); +} + +void TestEnvironment::testBasic() +{ + QProcessEnvironment environment; + environment.insert("PATH","/bin:/usr/bin"); + environment.insert("HOME","/home/tom"); + QFETCH(ProcEnv, test); + QFETCH(ProcEnv, goodAns); + + KDevelop::expandVariables( test, environment ); + for (auto it = goodAns.cbegin(); it!= goodAns.cend(); ++it) + { + QCOMPARE(test.value(it.key()), it.value()); + } +}