From: Zbigniew Jędrzejewski-Szmek Date: Wed, 15 Feb 2017 00:43:51 +0000 (-0500) Subject: tests: look for tests relative to source dir when running from build dir X-Git-Tag: v233~82^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1f35a3b2a4a6bdc97c0eacfee208e05b2f67f523;p=thirdparty%2Fsystemd.git tests: look for tests relative to source dir when running from build dir 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. --- diff --git a/Makefile.am b/Makefile.am index a0eda73cb41..c8c8d31ef05 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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) diff --git a/src/shared/tests.c b/src/shared/tests.c index bae113bdc8f..f11b93bee72 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -24,6 +24,7 @@ #include #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); }