Allow braceless variables to be expanded

(Only in environment.d files.)

We have only basic compatibility with shell syntax, but specifying variables
without using braces is probably more common, and I think a lot of people would
be surprised if this didn't work.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2017-02-20 23:30:50 -05:00
parent cb4499d005
commit ccad1fd07c
6 changed files with 91 additions and 11 deletions
+29 -2
View File
@@ -142,7 +142,32 @@ static void test_env_strv_get_n(void) {
getenv("PATH")));
}
static void test_replace_env_arg(void) {
static void test_replace_env(bool braceless) {
const char *env[] = {
"FOO=BAR BAR",
"BAR=waldo",
NULL
};
_cleanup_free_ char *t = NULL, *s = NULL, *q = NULL, *r = NULL, *p = NULL;
unsigned flags = REPLACE_ENV_ALLOW_BRACELESS*braceless;
t = replace_env("FOO=$FOO=${FOO}", (char**) env, flags);
assert_se(streq(t, braceless ? "FOO=BAR BAR=BAR BAR" : "FOO=$FOO=BAR BAR"));
s = replace_env("BAR=$BAR=${BAR}", (char**) env, flags);
assert_se(streq(s, braceless ? "BAR=waldo=waldo" : "BAR=$BAR=waldo"));
q = replace_env("BARBAR=$BARBAR=${BARBAR}", (char**) env, flags);
assert_se(streq(q, braceless ? "BARBAR==" : "BARBAR=$BARBAR="));
q = replace_env("BAR=$BAR$BAR${BAR}${BAR}", (char**) env, flags);
assert_se(streq(q, braceless ? "BAR=waldowaldowaldowaldo" : "BAR=$BAR$BARwaldowaldo"));
p = replace_env("${BAR}$BAR$BAR", (char**) env, flags);
assert_se(streq(p, braceless ? "waldowaldowaldo" : "waldo$BAR$BAR"));
}
static void test_replace_env_argv(void) {
const char *env[] = {
"FOO=BAR BAR",
"BAR=waldo",
@@ -256,7 +281,9 @@ int main(int argc, char *argv[]) {
test_strv_env_set();
test_strv_env_merge();
test_env_strv_get_n();
test_replace_env_arg();
test_replace_env(false);
test_replace_env(true);
test_replace_env_argv();
test_env_clean();
test_env_name_is_valid();
test_env_value_is_valid();
+10 -4
View File
@@ -226,7 +226,10 @@ static void test_merge_env_file(void) {
"twelve=${one}2\n"
"twentyone=2${one}\n"
"one=2\n"
"twentytwo=2${one}\n", false);
"twentytwo=2${one}\n"
"xxx_minus_three=$xxx - 3\n"
"xxx=0x$one$one$one\n"
, false);
assert(r >= 0);
r = merge_env_file(&a, NULL, t);
@@ -240,8 +243,9 @@ static void test_merge_env_file(void) {
assert_se(streq(a[1], "twelve=12"));
assert_se(streq(a[2], "twentyone=21"));
assert_se(streq(a[3], "twentytwo=22"));
assert_se(a[4] == NULL);
assert_se(streq(a[4], "xxx=0x222"));
assert_se(streq(a[5], "xxx_minus_three= - 3"));
assert_se(a[6] == NULL);
r = merge_env_file(&a, NULL, t);
assert_se(r >= 0);
@@ -254,7 +258,9 @@ static void test_merge_env_file(void) {
assert_se(streq(a[1], "twelve=12"));
assert_se(streq(a[2], "twentyone=21"));
assert_se(streq(a[3], "twentytwo=22"));
assert_se(a[4] == NULL);
assert_se(streq(a[4], "xxx=0x222"));
assert_se(streq(a[5], "xxx_minus_three=0x222 - 3"));
assert_se(a[6] == NULL);
}
static void test_executable_is_script(void) {