summaryrefslogtreecommitdiff
path: root/src/shared/tests.c
diff options
context:
space:
mode:
authorFilipe Brandenburger <filbranden@google.com>2018-09-11 23:55:02 -0700
committerFilipe Brandenburger <filbranden@google.com>2018-09-12 09:49:03 -0700
commite2d413707fc68ed033a83e10a055ca638a1e1e18 (patch)
treea58e24cbbe43ea9e22c4dce0835b179f763f617b /src/shared/tests.c
parent55890a40c3ec0c061c04d1395a38c26313132d12 (diff)
downloadsystemd-e2d413707fc68ed033a83e10a055ca638a1e1e18.tar.gz
test: use ${builddir}/systemd-runtest.env to set $SYSTEMD_TEST_DATA
This simplifies get_testdata_dir() to simply checking for an environment variable, with an additional function to locate a systemd-runtest.env file in the same directory as the test binary and reading environment variable assignments from that file if it exists. This makes it possible to: - Run `ninja test` from the build dir and have it use ${srcdir}/test for test unit definitions. - Run a test directly, such as `build/test-execute` and have it locate them correctly. - Run installed tests (from systemd-tests package) and locate the test units in the installed location (/usr/lib/systemd/tests/testdata), in which case the absence of the systemd-runtest.env file will have get_testdata_dir() use the installed location hardcoded into the binaries. Explicit setting of $SYSTEMD_TEST_DATA still overrides the contents of systemd-runtest.env.
Diffstat (limited to 'src/shared/tests.c')
-rw-r--r--src/shared/tests.c67
1 files changed, 32 insertions, 35 deletions
diff --git a/src/shared/tests.c b/src/shared/tests.c
index a002260439..ac11f5191b 100644
--- a/src/shared/tests.c
+++ b/src/shared/tests.c
@@ -6,8 +6,11 @@
#include <stdlib.h>
#include <util.h>
-#include "tests.h"
+#include "alloc-util.h"
+#include "fileio.h"
#include "path-util.h"
+#include "strv.h"
+#include "tests.h"
char* setup_fake_runtime_dir(void) {
char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
@@ -19,53 +22,47 @@ char* setup_fake_runtime_dir(void) {
return p;
}
-bool test_is_running_from_builddir(char **exedir) {
+static void load_testdata_env(void) {
+ static bool called = false;
_cleanup_free_ char *s = NULL;
- bool r;
+ _cleanup_free_ char *envpath = NULL;
+ _cleanup_strv_free_ char **pairs = NULL;
+ char **k, **v;
- /* Check if we're running from the builddir. Optionally, this returns
- * the path to the directory where the binary is located. */
+ if (called)
+ return;
+ called = true;
assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
- r = path_startswith(s, ABS_BUILD_DIR);
+ dirname(s);
- if (exedir) {
- dirname(s);
- *exedir = TAKE_PTR(s);
- }
+ envpath = path_join(NULL, s, "systemd-runtest.env");
+ if (load_env_file_pairs(NULL, envpath, NULL, &pairs) < 0)
+ return;
- return r;
+ STRV_FOREACH_PAIR(k, v, pairs)
+ setenv(*k, *v, 0);
+}
+
+bool test_is_running_from_builddir(char **exedir) {
+ load_testdata_env();
+
+ return !!getenv("SYSTEMD_TEST_DATA");
}
const char* get_testdata_dir(void) {
const char *env;
- /* convenience: caller does not need to free result */
- static char testdir[PATH_MAX];
+
+ load_testdata_env();
/* if the env var is set, use that */
env = getenv("SYSTEMD_TEST_DATA");
- testdir[sizeof(testdir) - 1] = '\0';
- if (env) {
- if (access(env, F_OK) < 0) {
- fputs("ERROR: $SYSTEMD_TEST_DATA directory does not exist\n", stderr);
- exit(EXIT_FAILURE);
- }
- strncpy(testdir, env, sizeof(testdir) - 1);
- } else {
- _cleanup_free_ char *exedir = NULL;
-
- /* Check if we're running from the builddir. If so, use the compiled in path. */
- if (test_is_running_from_builddir(&exedir))
- assert_se(snprintf(testdir, sizeof(testdir), "%s/test", ABS_SRC_DIR) > 0);
- else
- /* Try relative path, according to the install-test layout */
- assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", exedir) > 0);
-
- if (access(testdir, F_OK) < 0) {
- fputs("ERROR: Cannot find testdata directory, set $SYSTEMD_TEST_DATA\n", stderr);
- exit(EXIT_FAILURE);
- }
+ if (!env)
+ env = SYSTEMD_TEST_DATA;
+ if (access(env, F_OK) < 0) {
+ fprintf(stderr, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env);
+ exit(EXIT_FAILURE);
}
- return testdir;
+ return env;
}