tests/test_properties.c

changeset 1628
b15ed93f2aa7
parent 1605
55b13f583356
--- a/tests/test_properties.c	Fri Dec 19 13:47:10 2025 +0100
+++ b/tests/test_properties.c	Fri Dec 19 13:47:50 2025 +0100
@@ -491,14 +491,27 @@
     cxPropertiesDestroy(&prop);
 }
 
+static FILE *cx_opentmp(char *tpl) {
+#if defined _POSIX_C_SOURCE && _POSIX_C_SOURCE >= 200809L
+    strcpy(tpl, "ucxtestXXXXXX");
+    int fd = mkstemp(tpl);
+    if (fd < 0) return NULL;
+    return fdopen(fd, "w");
+#else
+    char *fname = tmpnam(tpl);
+    return fname == NULL ? NULL : fopen(fname, "wx");
+#endif
+}
+
 CX_TEST(test_properties_load) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
         fprintf(f, "\n\nkey3    = value3\n\n");
 
@@ -556,17 +569,18 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_load_empty_file) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fclose(f);
         f = NULL;
 
@@ -586,17 +600,18 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_load_only_comments) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fputs("# test file\n\n# contains only comments\n\n# key = value\n", f);
         fclose(f);
         f = NULL;
@@ -612,17 +627,18 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_load_error) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fputs("# test file\n\ntest = value\n = value2\n", f);
         fclose(f);
         f = NULL;
@@ -642,7 +658,7 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_load_file_not_exists) {
@@ -657,11 +673,12 @@
 CX_TEST(test_properties_load_cxmutstr_map) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
         fclose(f);
         f = NULL;
@@ -682,17 +699,18 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_load_incompatible_map) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
-    char fname[16] = "ucxtestXXXXXX";
-    int tmpfd = mkstemp(fname);
-    FILE *f = tmpfd < 0 ? NULL : fdopen(tmpfd, "w");
+    char fname[L_tmpnam];
+    FILE *f = cx_opentmp(fname);
+    bool rmtmp = false;
     CX_TEST_DO {
         CX_TEST_ASSERTM(f, "test file cannot be opened, test aborted");
+        rmtmp = true;
         fputs("# test file\n\ntest = value\ntest2 = value2\n", f);
         fclose(f);
         f = NULL;
@@ -708,7 +726,7 @@
     }
     cx_testing_allocator_destroy(&talloc);
     if (f) fclose(f);
-    remove(fname);
+    if (rmtmp) remove(fname);
 }
 
 CX_TEST(test_properties_multiple_fill) {

mercurial