]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: modernize load_testdata_env() a bit
authorLennart Poettering <lennart@poettering.net>
Fri, 1 Apr 2022 08:52:47 +0000 (10:52 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 1 Apr 2022 09:14:56 +0000 (11:14 +0200)
Let's add assert() around everyhing we don't expect to fail.

Port to path_extract_directory().

Log errrors from load_env_file_pairs() which we ignore.

src/shared/tests.c

index 70fbbb45a5495f812b6bd340f9dd74a2bd9539ee..a65080cbf43f9773b38350391b60a891d58394b5 100644 (file)
@@ -48,23 +48,26 @@ char* setup_fake_runtime_dir(void) {
 
 static void load_testdata_env(void) {
         static bool called = false;
-        _cleanup_free_ char *s = NULL;
-        _cleanup_free_ char *envpath = NULL;
+        _cleanup_free_ char *s = NULL, *d = NULL, *envpath = NULL;
         _cleanup_strv_free_ char **pairs = NULL;
+        int r;
 
         if (called)
                 return;
         called = true;
 
         assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
-        dirname(s);
+        assert_se(path_extract_directory(s, &d) >= 0);
+        assert_se(envpath = path_join(d, "systemd-runtest.env"));
 
-        envpath = path_join(s, "systemd-runtest.env");
-        if (load_env_file_pairs(NULL, envpath, &pairs) < 0)
+        r = load_env_file_pairs(NULL, envpath, &pairs);
+        if (r < 0) {
+                log_debug_errno(r, "Reading %s failed: %m", envpath);
                 return;
+        }
 
         STRV_FOREACH_PAIR(k, v, pairs)
-                setenv(*k, *v, 0);
+                assert_se(setenv(*k, *v, 0) >= 0);
 }
 
 int get_testdata_dir(const char *suffix, char **ret) {