basic: support default and alternate values for env expansion
Sometimes it's useful to provide a default value during an environment
expansion, if the environment variable isn't already set.
For instance $XDG_DATA_DIRS is suppose to default to:
/usr/local/share/:/usr/share/
if it's not yet set. That means callers wishing to augment
XDG_DATA_DIRS need to manually add those two values.
This commit changes replace_env to support the following shell
compatible default value syntax:
XDG_DATA_DIRS=/foo:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share}
Likewise, it's useful to provide an alternate value during an
environment expansion, if the environment variable isn't already set.
For instance, $LD_LIBRARY_PATH will inadvertently search the current
working directory if it starts or ends with a colon, so the following
is usually wrong:
LD_LIBRARY_PATH=/foo/lib:${LD_LIBRARY_PATH}
To address that, this changes replace_env to support the following
shell compatible alternate value syntax:
LD_LIBRARY_PATH=/foo/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
[zj: gate the new syntax under REPLACE_ENV_ALLOW_EXTENDED switch, so
existing callers are not modified.]
This commit is contained in:
committed by
Zbigniew Jędrzejewski-Szmek
parent
4bed076c5f
commit
b82f58bfe3
@@ -185,6 +185,12 @@ static void test_replace_env_argv(void) {
|
||||
"${FOO",
|
||||
"FOO$$${FOO}",
|
||||
"$$FOO${FOO}",
|
||||
"${FOO:-${BAR}}",
|
||||
"${QUUX:-${FOO}}",
|
||||
"${FOO:+${BAR}}",
|
||||
"${QUUX:+${BAR}}",
|
||||
"${FOO:+|${BAR}|}}",
|
||||
"${FOO:+|${BAR}{|}",
|
||||
NULL
|
||||
};
|
||||
_cleanup_strv_free_ char **r = NULL;
|
||||
@@ -202,7 +208,13 @@ static void test_replace_env_argv(void) {
|
||||
assert_se(streq(r[8], "${FOO"));
|
||||
assert_se(streq(r[9], "FOO$BAR BAR"));
|
||||
assert_se(streq(r[10], "$FOOBAR BAR"));
|
||||
assert_se(strv_length(r) == 11);
|
||||
assert_se(streq(r[11], "${FOO:-waldo}"));
|
||||
assert_se(streq(r[12], "${QUUX:-BAR BAR}"));
|
||||
assert_se(streq(r[13], "${FOO:+waldo}"));
|
||||
assert_se(streq(r[14], "${QUUX:+waldo}"));
|
||||
assert_se(streq(r[15], "${FOO:+|waldo|}}"));
|
||||
assert_se(streq(r[16], "${FOO:+|waldo{|}"));
|
||||
assert_se(strv_length(r) == 17);
|
||||
}
|
||||
|
||||
static void test_env_clean(void) {
|
||||
|
||||
+14
-2
@@ -229,6 +229,10 @@ static void test_merge_env_file(void) {
|
||||
"twentytwo=2${one}\n"
|
||||
"xxx_minus_three=$xxx - 3\n"
|
||||
"xxx=0x$one$one$one\n"
|
||||
"yyy=${one:-fallback}\n"
|
||||
"zzz=${one:+replacement}\n"
|
||||
"zzzz=${foobar:-${nothing}}\n"
|
||||
"zzzzz=${nothing:+${nothing}}\n"
|
||||
, false);
|
||||
assert(r >= 0);
|
||||
|
||||
@@ -245,7 +249,11 @@ static void test_merge_env_file(void) {
|
||||
assert_se(streq(a[3], "twentytwo=22"));
|
||||
assert_se(streq(a[4], "xxx=0x222"));
|
||||
assert_se(streq(a[5], "xxx_minus_three= - 3"));
|
||||
assert_se(a[6] == NULL);
|
||||
assert_se(streq(a[6], "yyy=2"));
|
||||
assert_se(streq(a[7], "zzz=replacement"));
|
||||
assert_se(streq(a[8], "zzzz="));
|
||||
assert_se(streq(a[9], "zzzzz="));
|
||||
assert_se(a[10] == NULL);
|
||||
|
||||
r = merge_env_file(&a, NULL, t);
|
||||
assert_se(r >= 0);
|
||||
@@ -260,7 +268,11 @@ static void test_merge_env_file(void) {
|
||||
assert_se(streq(a[3], "twentytwo=22"));
|
||||
assert_se(streq(a[4], "xxx=0x222"));
|
||||
assert_se(streq(a[5], "xxx_minus_three=0x222 - 3"));
|
||||
assert_se(a[6] == NULL);
|
||||
assert_se(streq(a[6], "yyy=2"));
|
||||
assert_se(streq(a[7], "zzz=replacement"));
|
||||
assert_se(streq(a[8], "zzzz="));
|
||||
assert_se(streq(a[9], "zzzzz="));
|
||||
assert_se(a[10] == NULL);
|
||||
}
|
||||
|
||||
static void test_merge_env_file_invalid(void) {
|
||||
|
||||
Reference in New Issue
Block a user