2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2016-04-08 18:54:05 +02:00
|
|
|
|
2017-02-12 23:14:43 +01:00
|
|
|
#include <alloc-util.h>
|
|
|
|
#include <fs-util.h>
|
|
|
|
#include <libgen.h>
|
2016-04-08 18:54:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "fileio.h"
|
2017-02-15 01:43:51 +01:00
|
|
|
#include "path-util.h"
|
2018-09-12 08:55:02 +02:00
|
|
|
#include "strv.h"
|
|
|
|
#include "tests.h"
|
2016-04-08 18:54:05 +02:00
|
|
|
|
|
|
|
char* setup_fake_runtime_dir(void) {
|
|
|
|
char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
|
|
|
|
|
|
|
|
assert_se(mkdtemp(t));
|
|
|
|
assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
|
|
|
|
assert_se(p = strdup(t));
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
2017-02-12 23:14:43 +01:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
static void load_testdata_env(void) {
|
|
|
|
static bool called = false;
|
2018-09-11 02:17:22 +02:00
|
|
|
_cleanup_free_ char *s = NULL;
|
2018-09-12 08:55:02 +02:00
|
|
|
_cleanup_free_ char *envpath = NULL;
|
|
|
|
_cleanup_strv_free_ char **pairs = NULL;
|
|
|
|
char **k, **v;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
if (called)
|
|
|
|
return;
|
|
|
|
called = true;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
|
|
|
assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
|
2018-09-12 08:55:02 +02:00
|
|
|
dirname(s);
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
envpath = path_join(NULL, s, "systemd-runtest.env");
|
|
|
|
if (load_env_file_pairs(NULL, envpath, NULL, &pairs) < 0)
|
|
|
|
return;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
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");
|
2018-09-11 02:17:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 08:15:09 +02:00
|
|
|
const char* get_testdata_dir(void) {
|
2017-02-15 08:52:17 +01:00
|
|
|
const char *env;
|
2018-09-12 08:55:02 +02:00
|
|
|
|
|
|
|
load_testdata_env();
|
2017-02-12 23:14:43 +01:00
|
|
|
|
2017-02-15 08:52:17 +01:00
|
|
|
/* if the env var is set, use that */
|
|
|
|
env = getenv("SYSTEMD_TEST_DATA");
|
2018-09-12 08:55:02 +02:00
|
|
|
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);
|
2017-02-15 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
return env;
|
2017-02-12 23:14:43 +01:00
|
|
|
}
|