diff options
author | Ray Strode <rstrode@redhat.com> | 2016-08-04 18:00:00 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-02-21 00:49:14 +0100 |
commit | 37f3ffca273e5238794019caede7b7cd33a5de3a (patch) | |
tree | e33c5556d5cb2dd89064d8c3344b904fd8853f47 /src/test/test-fileio.c | |
parent | basic: drop unnecessary strempty() call in replace_env (diff) | |
download | systemd-37f3ffca273e5238794019caede7b7cd33a5de3a.tar.xz systemd-37f3ffca273e5238794019caede7b7cd33a5de3a.zip |
basic: add new merge_env_file function
merge_env_file is a new function, that's like load_env_file, but takes a
pre-existing environment as an input argument. New environment entries are
merged. Variable expansion is performed.
Falling back to the process environment is supported (when a flag is set).
Alternatively this could be implemented as passing an additional fallback
environment array, but later on we're adding another flag to allow braceless
expansion, and the two flags can be combined in one arg, so there's less
stuff to pass around.
Diffstat (limited to '')
-rw-r--r-- | src/test/test-fileio.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index a38bb874a9..84f394a713 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -206,6 +206,56 @@ static void test_parse_multiline_env_file(void) { unlink(p); } +static void test_merge_env_file(void) { + char t[] = "/tmp/test-fileio-XXXXXX"; + int fd, r; + FILE *f; + _cleanup_strv_free_ char **a = NULL; + char **i; + + fd = mkostemp_safe(t); + assert_se(fd >= 0); + + log_info("/* %s (%s) */", __func__, t); + + f = fdopen(fd, "w"); + assert_se(f); + + r = write_string_stream(f, + "one=1 \n" + "twelve=${one}2\n" + "twentyone=2${one}\n" + "one=2\n" + "twentytwo=2${one}\n", false); + assert(r >= 0); + + r = merge_env_file(&a, NULL, t); + assert_se(r >= 0); + strv_sort(a); + + STRV_FOREACH(i, a) + log_info("Got: <%s>", *i); + + assert_se(streq(a[0], "one=2")); + 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); + + + r = merge_env_file(&a, NULL, t); + assert_se(r >= 0); + strv_sort(a); + + STRV_FOREACH(i, a) + log_info("Got2: <%s>", *i); + + assert_se(streq(a[0], "one=2")); + 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); +} static void test_executable_is_script(void) { char t[] = "/tmp/test-executable-XXXXXX"; @@ -557,11 +607,13 @@ static void test_tempfn(void) { } int main(int argc, char *argv[]) { + log_set_max_level(LOG_DEBUG); log_parse_environment(); log_open(); test_parse_env_file(); test_parse_multiline_env_file(); + test_merge_env_file(); test_executable_is_script(); test_status_field(); test_capeff(); |