test: move mkdir & mkdtemp calls to common place and fix them on MSVC

Signed-off-by: Ran Benita <ran@unusedvar.com>
master
Ran Benita 2022-09-24 10:27:51 +03:00
parent 4d8293901b
commit f9858bf5b9
5 changed files with 63 additions and 24 deletions

View File

@ -685,7 +685,7 @@ if get_option('enable-xkbregistry')
'registry',
executable('test-registry', 'test/registry.c',
include_directories: include_directories('src'),
dependencies: dep_libxkbregistry),
dependencies: [dep_libxkbregistry, test_dep]),
env: test_env,
)
endif

View File

@ -34,6 +34,7 @@
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
@ -162,6 +163,52 @@ test_key_seq(struct xkb_keymap *keymap, ...)
return ret;
}
char *
test_makedir(const char *parent, const char *path)
{
char *dirname;
int err;
dirname = asprintf_safe("%s/%s", parent, path);
assert(dirname);
#ifdef _MSC_VER
err = _mkdir(dirname);
#else
err = mkdir(dirname, 0777);
#endif
assert(err == 0);
return dirname;
}
char *
test_maketempdir(const char *template)
{
#ifdef _MSC_VER
const char *basetmp = getenv("TMP");
if (basetmp == NULL) {
basetmp = getenv("TEMP");
}
if (basetmp == NULL) {
basetmp = getenv("top_builddir");
}
assert(basetmp != NULL);
char *tmpdir = asprintf_safe("%s/%s", basetmp, template);
assert(tmpdir != NULL);
char *tmp = _mktemp(tmpdir);
assert(tmp == tmpdir);
int ret = _mkdir(tmp);
assert(ret == 0);
return tmpdir;
#else
char *tmpdir = asprintf_safe("/tmp/%s", template);
assert(tmpdir != NULL);
char *tmp = mkdtemp(tmpdir);
assert(tmp == tmpdir);
return tmpdir;
#endif
}
char *
test_get_path(const char *path_rel)
{

View File

@ -73,29 +73,15 @@ static void restore_env(void)
static const char *makedir(const char *parent, const char *path)
{
char *dirname;
int err;
dirname = asprintf_safe("%s/%s", parent, path);
assert(dirname);
err = mkdir(dirname, 0777);
assert(err == 0);
char *dirname = test_makedir(parent, path);
dirnames[ndirs++] = dirname;
return dirname;
}
static const char *maketmpdir(void)
{
const char *template = "/tmp/xkbcommon-test.XXXXXX";
char *tmpdir = strdup(template);
tmpdir = mkdtemp(tmpdir);
assert(tmpdir != NULL);
char *tmpdir = test_maketempdir("xkbcommon-test.XXXXXX");
dirnames[ndirs++] = tmpdir;
return tmpdir;
}

View File

@ -35,6 +35,7 @@
#include "xkbcommon/xkbregistry.h"
#include "utils.h"
#include "test.h"
#define NO_VARIANT NULL
@ -132,14 +133,13 @@ test_create_rules(const char *ruleset,
int rc;
FILE *fp;
tmpdir = asprintf_safe("/tmp/%s.%d.XXXXXX", ruleset, iteration++);
assert(tmpdir);
assert(mkdtemp(tmpdir) == tmpdir);
char *template = asprintf_safe("%s.%d.XXXXXX", ruleset, iteration++);
assert(template != NULL);
tmpdir = test_maketempdir(template);
free(template);
free(test_makedir(tmpdir, "rules"));
rc = snprintf_safe(buf, sizeof(buf), "%s/rules", tmpdir);
assert(rc);
rc = mkdir(buf, 0777);
assert(rc == 0);
rc = snprintf_safe(buf, sizeof(buf), "%s/rules/%s.xml", tmpdir, ruleset);
assert(rc);

View File

@ -53,6 +53,12 @@ test_key_seq(struct xkb_keymap *keymap, ...);
int
test_key_seq_va(struct xkb_keymap *keymap, va_list args);
char *
test_makedir(const char *parent, const char *path);
char *
test_maketempdir(const char *template);
char *
test_get_path(const char *path_rel);