c2dfb7
From b69552ccc0e33f713ae3a2baf1b0173cf221507d Mon Sep 17 00:00:00 2001
c2dfb7
From: Yu Watanabe <watanabe.yu+github@gmail.com>
c2dfb7
Date: Tue, 11 Sep 2018 09:17:22 +0900
c2dfb7
Subject: [PATCH] test: introduce test_is_running_from_builddir()
c2dfb7
c2dfb7
(cherry picked from commit 8cb10a4f4dabc508a04f76ea55f23ef517881b61)
c2dfb7
c2dfb7
Resolves: #1823767
c2dfb7
---
c2dfb7
 src/shared/tests.c | 23 ++++++++++++++++++++---
c2dfb7
 src/shared/tests.h |  1 +
c2dfb7
 2 files changed, 21 insertions(+), 3 deletions(-)
c2dfb7
c2dfb7
diff --git a/src/shared/tests.c b/src/shared/tests.c
c2dfb7
index b10343650f..c77eb00924 100644
c2dfb7
--- a/src/shared/tests.c
c2dfb7
+++ b/src/shared/tests.c
c2dfb7
@@ -19,6 +19,24 @@ char* setup_fake_runtime_dir(void) {
c2dfb7
         return p;
c2dfb7
 }
c2dfb7
 
c2dfb7
+bool test_is_running_from_builddir(char **exedir) {
c2dfb7
+        _cleanup_free_ char *s = NULL;
c2dfb7
+        bool r;
c2dfb7
+
c2dfb7
+        /* Check if we're running from the builddir. Optionally, this returns
c2dfb7
+         * the path to the directory where the binary is located. */
c2dfb7
+
c2dfb7
+        assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
c2dfb7
+        r = path_startswith(s, ABS_BUILD_DIR);
c2dfb7
+
c2dfb7
+        if (exedir) {
c2dfb7
+                dirname(s);
c2dfb7
+                *exedir = TAKE_PTR(s);
c2dfb7
+        }
c2dfb7
+
c2dfb7
+        return r;
c2dfb7
+}
c2dfb7
+
c2dfb7
 const char* get_testdata_dir(const char *suffix) {
c2dfb7
         const char *env;
c2dfb7
         /* convenience: caller does not need to free result */
c2dfb7
@@ -35,14 +53,13 @@ const char* get_testdata_dir(const char *suffix) {
c2dfb7
                 strncpy(testdir, env, sizeof(testdir) - 1);
c2dfb7
         } else {
c2dfb7
                 _cleanup_free_ char *exedir = NULL;
c2dfb7
-                assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
c2dfb7
 
c2dfb7
                 /* Check if we're running from the builddir. If so, use the compiled in path. */
c2dfb7
-                if (path_startswith(exedir, ABS_BUILD_DIR))
c2dfb7
+                if (test_is_running_from_builddir(&exedir))
c2dfb7
                         assert_se(snprintf(testdir, sizeof(testdir), "%s/test", ABS_SRC_DIR) > 0);
c2dfb7
                 else
c2dfb7
                         /* Try relative path, according to the install-test layout */
c2dfb7
-                        assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
c2dfb7
+                        assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", exedir) > 0);
c2dfb7
 
c2dfb7
                 /* test this without the suffix, as it may contain a glob */
c2dfb7
                 if (access(testdir, F_OK) < 0) {
c2dfb7
diff --git a/src/shared/tests.h b/src/shared/tests.h
c2dfb7
index cad21169f8..7f45c32d32 100644
c2dfb7
--- a/src/shared/tests.h
c2dfb7
+++ b/src/shared/tests.h
c2dfb7
@@ -2,5 +2,6 @@
c2dfb7
 #pragma once
c2dfb7
 
c2dfb7
 char* setup_fake_runtime_dir(void);
c2dfb7
+bool test_is_running_from_builddir(char **exedir);
c2dfb7
 const char* get_testdata_dir(const char *suffix);
c2dfb7
 void test_setup_logging(int level);