test: move mkdir & mkdtemp calls to common place and fix them on MSVC Signed-off-by: Ran Benita <ran@unusedvar.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
diff --git a/meson.build b/meson.build
index 5b33b01..26fca41 100644
--- a/meson.build
+++ b/meson.build
@@ -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
diff --git a/test/common.c b/test/common.c
index c6f6644..1d5a99d 100644
--- a/test/common.c
+++ b/test/common.c
@@ -34,6 +34,7 @@
#include <limits.h>
#include <fcntl.h>
+#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _MSC_VER
@@ -163,6 +164,52 @@ test_key_seq(struct xkb_keymap *keymap, ...)
}
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)
{
char *path;
diff --git a/test/context.c b/test/context.c
index 9f6cad7..bb619b0 100644
--- a/test/context.c
+++ b/test/context.c
@@ -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;
}
diff --git a/test/registry.c b/test/registry.c
index 71a480f..1412818 100644
--- a/test/registry.c
+++ b/test/registry.c
@@ -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);
diff --git a/test/test.h b/test/test.h
index 81ba49b..ba06deb 100644
--- a/test/test.h
+++ b/test/test.h
@@ -54,6 +54,12 @@ 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);
char *