Commit f9858bf5b97a6ae9a79629fd724a8177232eda00

Ran Benita 2022-09-24T10:27:51

test: move mkdir & mkdtemp calls to common place and fix them on MSVC Signed-off-by: Ran Benita <ran@unusedvar.com>

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 *