aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-03-29 01:06:51 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-03-29 01:06:51 +0000
commit71397499900d3dcc444d59fd2a51cb9a0af70ad4 (patch)
treeb867d6463a001fd0df4839fec65d7df560b14f49
parent67bf0a80940fed704e8835f6cb62ac65409b762c (diff)
downloadpaludis-71397499900d3dcc444d59fd2a51cb9a0af70ad4.tar.gz
paludis-71397499900d3dcc444d59fd2a51cb9a0af70ad4.tar.xz
Make make_env_command work with single quotes
-rw-r--r--paludis/util/system.cc10
-rw-r--r--paludis/util/system_TEST.cc38
2 files changed, 44 insertions, 4 deletions
diff --git a/paludis/util/system.cc b/paludis/util/system.cc
index 7aea446..c2dc581 100644
--- a/paludis/util/system.cc
+++ b/paludis/util/system.cc
@@ -98,8 +98,14 @@ system_internals::MakeEnvCommand
system_internals::MakeEnvCommand::operator() (const std::string & k,
const std::string & v) const
{
- /// \bug this will explode if there're single quotes in the value
- return MakeEnvCommand(cmd, args + k + "='" + v + "' ");
+ std::string vv;
+ for (std::string::size_type p(0) ; p < v.length() ; ++p)
+ if ('\'' == v[p])
+ vv.append("'\"'\"'");
+ else
+ vv.append(v.substr(p, 1));
+
+ return MakeEnvCommand(cmd, args + k + "='" + vv + "' ");
}
system_internals::MakeEnvCommand::operator std::string() const
diff --git a/paludis/util/system_TEST.cc b/paludis/util/system_TEST.cc
index ef2f809..2221394 100644
--- a/paludis/util/system_TEST.cc
+++ b/paludis/util/system_TEST.cc
@@ -18,6 +18,7 @@
*/
#include <paludis/util/system.hh>
+#include <paludis/util/pstream.hh>
#include <test/test_framework.hh>
#include <test/test_runner.hh>
@@ -109,10 +110,43 @@ namespace test_cases
void run()
{
TEST_CHECK(0 != run_command(make_env_command("printenv PALUDUS_TEST_ENV_VAR")));
- TEST_CHECK(0 == run_command(make_env_command("test -z `printenv PALUDUS_TEST_ENV_VAR`")(
+ TEST_CHECK(0 == run_command(make_env_command("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")));
+ TEST_CHECK(0 == run_command(make_env_command("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")(
"PALUDUS_TEST_ENV_VAR", "")));
- TEST_CHECK(0 == run_command(make_env_command("test -n `printenv PALUDUS_TEST_ENV_VAR`")(
+ TEST_CHECK(0 == run_command(make_env_command("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")(
"PALUDUS_TEST_ENV_VAR", "foo")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ -z $PALUDUS_TEST_ENV_VAR ]]'")(
+ "PALUDUS_TEST_ENV_VAR", "foo")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ -n $PALUDUS_TEST_ENV_VAR ]]'")(
+ "PALUDUS_TEST_ENV_VAR", "")));
+ TEST_CHECK(0 == run_command(make_env_command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")(
+ "PALUDUS_TEST_ENV_VAR", "foo")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")(
+ "PALUDUS_TEST_ENV_VAR", "")));
+ TEST_CHECK(0 != run_command(make_env_command("bash -c '[[ $PALUDUS_TEST_ENV_VAR == foo ]]'")(
+ "PALUDUS_TEST_ENV_VAR", "bar")));
}
} test_make_env_command;
+
+ /**
+ * \test Test make_env_command with quotes.
+ *
+ * \ingroup Test
+ */
+ struct MakeEnvCommandQuoteTest : TestCase
+ {
+ MakeEnvCommandQuoteTest() : TestCase("make_env_command quotes") { }
+
+ void run()
+ {
+ TEST_CHECK(0 == run_command(make_env_command(
+ "bash -c '[[ x$PALUDUS_TEST_ENV_VAR == \"x....\" ]]'")
+ ("PALUDUS_TEST_ENV_VAR", "....")));
+ TEST_CHECK(0 == run_command(make_env_command(
+ "bash -c '[[ x$PALUDUS_TEST_ENV_VAR == \"x..'\"'\"'..\" ]]'")
+ ("PALUDUS_TEST_ENV_VAR", "..'..")));
+ }
+ } test_make_env_command_quotes;
}