diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-02-14 19:43:51 -0500 |
---|---|---|
committer | Martin Pitt <martin@piware.de> | 2017-02-16 21:36:31 +0100 |
commit | 1f35a3b2a4a6bdc97c0eacfee208e05b2f67f523 (patch) | |
tree | bdb51f8e51ec5a4dc56510f7bccd7f4c556ffa5d | |
parent | 94fa1497ba98bb083632bd4d3f6cfcf6db8cff03 (diff) |
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.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/shared/tests.c | 17 |
2 files changed, 14 insertions, 5 deletions
diff --git a/Makefile.am b/Makefile.am index a0eda73cb4..c8c8d31ef0 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 bae113bdc8..f11b93bee7 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -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); } |