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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-02-14 19:43:51 -05:00 committed by Martin Pitt
parent 94fa1497ba
commit 1f35a3b2a4
2 changed files with 14 additions and 5 deletions

View 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)

View 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);
}