[gjs: 18/22] GjsTestTools: Move open_bytes here from GjsPrivate




commit a6e8c2855d975668db280671b35e31f8c6c4d721
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Apr 6 19:54:11 2021 +0200

    GjsTestTools: Move open_bytes here from GjsPrivate
    
    This is only needed for testing purposes, so no need to expose it in the
    exported typelib.

 .../js/libgjstesttools/gjs-test-tools.cpp          | 91 +++++++++++++++++++++
 .../js/libgjstesttools/gjs-test-tools.h            |  2 +
 installed-tests/js/testGDBus.js                    | 10 +--
 libgjs-private/gjs-util.c                          | 92 ----------------------
 libgjs-private/gjs-util.h                          |  4 -
 5 files changed, 98 insertions(+), 101 deletions(-)
---
diff --git a/installed-tests/js/libgjstesttools/gjs-test-tools.cpp 
b/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
index d5736ca0..c018cc2e 100644
--- a/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
+++ b/installed-tests/js/libgjstesttools/gjs-test-tools.cpp
@@ -9,6 +9,15 @@
 
 #include "gjs/jsapi-util.h"
 
+#ifdef G_OS_UNIX
+#    include <errno.h>
+#    include <fcntl.h> /* for FD_CLOEXEC */
+#    include <stdarg.h>
+#    include <unistd.h> /* for close, write */
+
+#    include <glib-unix.h> /* for g_unix_open_pipe */
+#endif
+
 static GObject* m_tmp_object = NULL;
 static GWeakRef m_tmp_weak;
 static std::unordered_set<GObject*> m_finalized_objects;
@@ -231,3 +240,85 @@ GObject* gjs_test_tools_get_disposed(GObject* object) {
     g_object_run_dispose(G_OBJECT(object));
     return object;
 }
+
+#ifdef G_OS_UNIX
+
+// Adapted from glnx_throw_errno_prefix()
+G_GNUC_PRINTF(2, 3)
+static gboolean throw_errno_prefix(GError** error, const char* fmt, ...) {
+    int errsv = errno;
+    char* old_msg;
+    GString* buf;
+
+    va_list args;
+
+    if (!error)
+        return FALSE;
+
+    va_start(args, fmt);
+
+    g_set_error_literal(error, G_IO_ERROR, g_io_error_from_errno(errsv),
+                        g_strerror(errsv));
+
+    old_msg = g_steal_pointer(&(*error)->message);
+    buf = g_string_new("");
+    g_string_append_vprintf(buf, fmt, args);
+    g_string_append(buf, ": ");
+    g_string_append(buf, old_msg);
+    g_free(old_msg);
+    (*error)->message = g_string_free(g_steal_pointer(&buf), FALSE);
+
+    va_end(args);
+
+    errno = errsv;
+    return FALSE;
+}
+
+#endif /* G_OS_UNIX */
+
+/**
+ * gjs_open_bytes:
+ * @bytes: bytes to send to the pipe
+ * @error: Return location for a #GError, or %NULL
+ *
+ * Creates a pipe and sends @bytes to it, such that it is suitable for passing
+ * to g_subprocess_launcher_take_fd().
+ *
+ * Returns: file descriptor, or -1 on error
+ */
+int gjs_test_tools_open_bytes(GBytes* bytes, GError** error) {
+    int pipefd[2], result;
+    size_t count;
+    const void* buf;
+    ssize_t bytes_written;
+
+    g_return_val_if_fail(bytes, -1);
+    g_return_val_if_fail(error == NULL || *error == NULL, -1);
+
+#ifdef G_OS_UNIX
+    if (!g_unix_open_pipe(pipefd, FD_CLOEXEC, error))
+        return -1;
+
+    buf = g_bytes_get_data(bytes, &count);
+
+    bytes_written = write(pipefd[1], buf, count);
+    if (bytes_written < 0) {
+        throw_errno_prefix(error, "write");
+        return -1;
+    }
+
+    if ((size_t)bytes_written != count)
+        g_warning("%s: %zu bytes sent, only %zd bytes written", __func__, count,
+                  bytes_written);
+
+    result = close(pipefd[1]);
+    if (result == -1) {
+        throw_errno_prefix(error, "close");
+        return -1;
+    }
+
+    return pipefd[0];
+#else
+    g_error("%s is currently supported on UNIX only", __func__);
+#endif
+}
diff --git a/installed-tests/js/libgjstesttools/gjs-test-tools.h 
b/installed-tests/js/libgjstesttools/gjs-test-tools.h
index 7f67413f..9ba75df1 100644
--- a/installed-tests/js/libgjstesttools/gjs-test-tools.h
+++ b/installed-tests/js/libgjstesttools/gjs-test-tools.h
@@ -49,4 +49,6 @@ GObject* gjs_test_tools_get_weak_other_thread();
 
 GObject* gjs_test_tools_get_disposed(GObject* object);
 
+int gjs_test_tools_open_bytes(GBytes* bytes, GError** error);
+
 G_END_DECLS
diff --git a/installed-tests/js/testGDBus.js b/installed-tests/js/testGDBus.js
index 6464d3d1..ae93cda2 100644
--- a/installed-tests/js/testGDBus.js
+++ b/installed-tests/js/testGDBus.js
@@ -2,7 +2,7 @@
 // SPDX-FileCopyrightText: 2008 litl, LLC
 
 const ByteArray = imports.byteArray;
-const {Gio, GjsPrivate, GLib} = imports.gi;
+const {Gio, GjsTestTools, GLib} = imports.gi;
 
 /* The methods list with their signatures.
  *
@@ -240,14 +240,14 @@ class Test {
     }
 
     fdOut(bytes) {
-        const fd = GjsPrivate.open_bytes(bytes);
+        const fd = GjsTestTools.open_bytes(bytes);
         const fdList = Gio.UnixFDList.new_from_array([fd]);
         return [0, fdList];
     }
 
     fdOut2Async([bytes], invocation) {
         GLib.idle_add(GLib.PRIORITY_DEFAULT, function () {
-            const fd = GjsPrivate.open_bytes(bytes);
+            const fd = GjsTestTools.open_bytes(bytes);
             const fdList = Gio.UnixFDList.new_from_array([fd]);
             invocation.return_value_with_unix_fd_list(new GLib.Variant('(h)', [0]),
                 fdList);
@@ -556,7 +556,7 @@ describe('Exported DBus object', function () {
 
     it('can call a remote method with a Unix FD', function (done) {
         const expectedBytes = ByteArray.fromString('some bytes');
-        const fd = GjsPrivate.open_bytes(expectedBytes);
+        const fd = GjsTestTools.open_bytes(expectedBytes);
         const fdList = Gio.UnixFDList.new_from_array([fd]);
         proxy.fdInRemote(0, fdList, ([bytes], exc, outFdList) => {
             expect(exc).toBeNull();
@@ -568,7 +568,7 @@ describe('Exported DBus object', function () {
 
     it('can call an asynchronously implemented remote method with a Unix FD', function (done) {
         const expectedBytes = ByteArray.fromString('some bytes');
-        const fd = GjsPrivate.open_bytes(expectedBytes);
+        const fd = GjsTestTools.open_bytes(expectedBytes);
         const fdList = Gio.UnixFDList.new_from_array([fd]);
         proxy.fdIn2Remote(0, fdList, ([bytes], exc, outFdList) => {
             expect(exc).toBeNull();
diff --git a/libgjs-private/gjs-util.c b/libgjs-private/gjs-util.c
index 4fd268cd..9dbe5f6f 100644
--- a/libgjs-private/gjs-util.c
+++ b/libgjs-private/gjs-util.c
@@ -9,21 +9,11 @@
 #include <locale.h>    /* for setlocale */
 #include <stddef.h>    /* for size_t */
 
-#include <gio/gio.h>
 #include <glib-object.h>
 #include <girepository.h>
 #include <glib.h>
 #include <glib/gi18n.h> /* for bindtextdomain, bind_textdomain_codeset, textdomain */
 
-#ifdef G_OS_UNIX
-#    include <errno.h>
-#    include <fcntl.h> /* for FD_CLOEXEC */
-#    include <stdarg.h>
-#    include <unistd.h> /* for close, write */
-
-#    include <glib-unix.h> /* for g_unix_open_pipe */
-#endif
-
 #include "libgjs-private/gjs-util.h"
 
 char *
@@ -108,88 +98,6 @@ gjs_param_spec_get_owner_type(GParamSpec *pspec)
     return pspec->owner_type;
 }
 
-#ifdef G_OS_UNIX
-
-// Adapted from glnx_throw_errno_prefix()
-G_GNUC_PRINTF(2, 3)
-static gboolean throw_errno_prefix(GError** error, const char* fmt, ...) {
-    int errsv = errno;
-    char* old_msg;
-    GString* buf;
-
-    va_list args;
-
-    if (!error)
-        return FALSE;
-
-    va_start(args, fmt);
-
-    g_set_error_literal(error, G_IO_ERROR, g_io_error_from_errno(errsv),
-                        g_strerror(errsv));
-
-    old_msg = g_steal_pointer(&(*error)->message);
-    buf = g_string_new("");
-    g_string_append_vprintf(buf, fmt, args);
-    g_string_append(buf, ": ");
-    g_string_append(buf, old_msg);
-    g_free(old_msg);
-    (*error)->message = g_string_free(g_steal_pointer(&buf), FALSE);
-
-    va_end(args);
-
-    errno = errsv;
-    return FALSE;
-}
-
-#endif /* G_OS_UNIX */
-
-/**
- * gjs_open_bytes:
- * @bytes: bytes to send to the pipe
- * @error: Return location for a #GError, or %NULL
- *
- * Creates a pipe and sends @bytes to it, such that it is suitable for passing
- * to g_subprocess_launcher_take_fd().
- *
- * Returns: file descriptor, or -1 on error
- */
-int gjs_open_bytes(GBytes* bytes, GError** error) {
-    int pipefd[2], result;
-    size_t count;
-    const void* buf;
-    ssize_t bytes_written;
-
-    g_return_val_if_fail(bytes, -1);
-    g_return_val_if_fail(error == NULL || *error == NULL, -1);
-
-#ifdef G_OS_UNIX
-    if (!g_unix_open_pipe(pipefd, FD_CLOEXEC, error))
-        return -1;
-
-    buf = g_bytes_get_data(bytes, &count);
-
-    bytes_written = write(pipefd[1], buf, count);
-    if (bytes_written < 0) {
-        throw_errno_prefix(error, "write");
-        return -1;
-    }
-
-    if ((size_t)bytes_written != count)
-        g_warning("%s: %zu bytes sent, only %zd bytes written", __func__, count,
-                  bytes_written);
-
-    result = close(pipefd[1]);
-    if (result == -1) {
-        throw_errno_prefix(error, "close");
-        return -1;
-    }
-
-    return pipefd[0];
-#else
-    g_error("%s is currently supported on UNIX only", __func__);
-#endif
-}
-
 static GParamSpec* gjs_gtk_container_class_find_child_property(
     GIObjectInfo* container_info, GObject* container, const char* property) {
     GIBaseInfo* class_info = NULL;
diff --git a/libgjs-private/gjs-util.h b/libgjs-private/gjs-util.h
index d4f8bbf6..c24f2359 100644
--- a/libgjs-private/gjs-util.h
+++ b/libgjs-private/gjs-util.h
@@ -57,10 +57,6 @@ void gjs_gtk_container_child_set_property(GObject* container, GObject* child,
                                           const char* property,
                                           const GValue* value);
 
-/* For tests */
-GJS_EXPORT
-int gjs_open_bytes(GBytes* bytes, GError** error);
-
 G_END_DECLS
 
 #endif /* LIBGJS_PRIVATE_GJS_UTIL_H_ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]