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