]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: look for tests relative to source dir when running from build dir
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 15 Feb 2017 00:43:51 +0000 (19:43 -0500)
committerMartin Pitt <martin@piware.de>
Thu, 16 Feb 2017 20:36:31 +0000 (21:36 +0100)
automake helpfully sets a few variables for during build. When our executable
is in a directory underneath $(abs_top_builddir), we know that we're in the
build environment $(abs_top_srcdir) contains the sources, and test data is
under $(abs_top_srcdir)/test. This remains true no matter where the build
directory is relative to the source directory. It also works if the test
executable is invoked as ./test-whatever or .libs/test-whatever, since the
relative path is not used at all.

When running from outside of the build directory, we should be running from the
installed location and we can look for ../testdata relative to the location of
the exe file.

Of course, $SYSTEMD_TEST_DATA always overrides this logic.

Makefile.am
src/shared/tests.c

index a0eda73cb41e3211b706b83cdd6f6d8e9fd82581..c8c8d31ef05ad3887f09a584f0068546a116bcba 100644 (file)
@@ -252,6 +252,8 @@ AM_CPPFLAGS = \
        -I $(top_srcdir)/src/libsystemd/sd-device \
        -I $(top_srcdir)/src/libsystemd/sd-id128 \
        -I $(top_srcdir)/src/libsystemd-network \
+       -DABS_SRC_DIR=\"$(abs_top_srcdir)\" \
+       -DABS_BUILD_DIR=\"$(abs_top_builddir)\" \
        $(OUR_CPPFLAGS)
 
 AM_CFLAGS = $(OUR_CFLAGS)
index bae113bdc8f9f054eec9c7e52e1707fb8664362c..f11b93bee72567237b638d2511a33a593d75ed83 100644 (file)
@@ -24,6 +24,7 @@
 #include <util.h>
 
 #include "tests.h"
+#include "path-util.h"
 
 char* setup_fake_runtime_dir(void) {
         char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
@@ -41,10 +42,16 @@ const char* get_exe_relative_testdata_dir(void) {
         static char testdir[PATH_MAX];
 
         assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
+
+        /* Check if we're running from the builddir. If so, use the compiled in path. */
+        if (path_startswith(exedir, ABS_BUILD_DIR))
+                return ABS_SRC_DIR "/test";
+
+        /* Try relative path, according to the install-test layout */
         assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
-        if (access(testdir, F_OK) < 0) {
-                fprintf(stderr,  "Test data directory '%s' does not exist, set $SYSTEMD_TEST_DATA\n", testdir);
-                exit(1);
-        }
-        return testdir;
+        if (access(testdir, F_OK) >= 0)
+                return testdir;
+
+        fputs("ERROR: Cannot find testdata directory, set $SYSTEMD_TEST_DATA\n", stderr);
+        exit(1);
 }