summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2016-08-09 10:20:22 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-02-20 23:32:53 -0500
commitb82f58bfe396b395bce3452bc0ba2f972fb01ab8 (patch)
tree02767a48aced8b08db4aba14890a224a97c05111 /src/test
parent4bed076c5f79ce26451ea3d73950d895f630f9a7 (diff)
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.]
Diffstat (limited to 'src/test')
-rw-r--r--src/test/test-env-util.c14
-rw-r--r--src/test/test-fileio.c16
2 files changed, 27 insertions, 3 deletions
diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c
index 77a5219d82..dfcd9cb724 100644
--- a/src/test/test-env-util.c
+++ b/src/test/test-env-util.c
@@ -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) {
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index b117335db8..b1d688c89e 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -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) {