[gegl/wip/Jehan/gegl_node_process_success: 2/4] operations: update png-save to report properly errors.



commit 2b0518bed7f9cb4959a3439217d9b03c7711ec7c
Author: Jehan <jehan girinstud io>
Date:   Mon Mar 25 18:15:26 2019 +0100

    operations: update png-save to report properly errors.
    
    And create a unit test to check gegl_node_success() returns the proper
    success and error of a gegl_node_process() call. We test by trying to
    write (with gegl:png-save) onto a non-writable file.

 operations/external/png-save.c |   8 +--
 tests/simple/Makefile.am       |   1 +
 tests/simple/test-errors.c     | 108 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 3 deletions(-)
---
diff --git a/operations/external/png-save.c b/operations/external/png-save.c
index b57d933d4..00e5d7541 100644
--- a/operations/external/png-save.c
+++ b/operations/external/png-save.c
@@ -259,7 +259,8 @@ process (GeglOperation       *operation,
   if (png == NULL || info == NULL)
     {
       status = FALSE;
-      g_warning ("failed to initialize PNG writer");
+      gegl_operation_set_error (operation, g_error_new (g_quark_from_static_string ("gegl"),
+                                                        0, "failed to initialize PNG writer"));
       goto cleanup;
     }
 
@@ -267,7 +268,7 @@ process (GeglOperation       *operation,
   if (stream == NULL)
     {
       status = FALSE;
-      g_warning ("%s", error->message);
+      gegl_operation_set_error (operation, error);
       goto cleanup;
     }
 
@@ -276,7 +277,8 @@ process (GeglOperation       *operation,
   if (export_png (operation, input, result, png, info, o->compression, o->bitdepth))
     {
       status = FALSE;
-      g_warning("could not export PNG file");
+      gegl_operation_set_error (operation, g_error_new (g_quark_from_static_string ("gegl"),
+                                                        0, "could not export PNG file"));
       goto cleanup;
     }
 
diff --git a/tests/simple/Makefile.am b/tests/simple/Makefile.am
index f23061520..881721ef6 100644
--- a/tests/simple/Makefile.am
+++ b/tests/simple/Makefile.am
@@ -14,6 +14,7 @@ noinst_PROGRAMS =                     \
        test-convert-format             \
        test-color-op                   \
        test-empty-tile                 \
+       test-errors                     \
        test-format-sensing             \
        test-gegl-rectangle             \
        test-gegl-color             \
diff --git a/tests/simple/test-errors.c b/tests/simple/test-errors.c
new file mode 100644
index 000000000..a967153f0
--- /dev/null
+++ b/tests/simple/test-errors.c
@@ -0,0 +1,108 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+#include <stdio.h>
+#include <unistd.h>
+
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+
+#include "gegl.h"
+
+static gboolean save_denied    (void);
+static gboolean load_denied    (void);
+static gboolean load_zero_blit (void);
+
+/* Trying to save in a non-writable file with gegl_node_process(). */
+static gboolean
+save_denied (void)
+{
+  GeglNode  *graph;
+  GeglNode  *color;
+  GeglNode  *crop;
+  GeglNode  *save;
+  GeglColor *red;
+  GError    *error   = NULL;
+  gchar     *path;
+  gboolean   success = FALSE;
+  gint       fd;
+
+  red = gegl_color_new ("rgb(1.0, 0.0, 0.0)");
+
+  /* Create a new empty file and forbid writing. */
+  fd = g_file_open_tmp (NULL, &path, NULL);
+  close (fd);
+  g_chmod (path, S_IRUSR);
+
+  /* Try to save. */
+  graph = gegl_node_new ();
+  color = gegl_node_new_child (graph,
+                               "operation", "gegl:color",
+                               "value",     red,
+                               NULL);
+  crop = gegl_node_new_child (graph,
+                              "operation", "gegl:crop",
+                              "width", 100.0,
+                              "height", 100.0,
+                              NULL);
+  save = gegl_node_new_child (graph,
+                              "operation", "gegl:png-save",
+                              "path",      path,
+                              NULL);
+  gegl_node_link_many (color, crop, save, NULL);
+
+  gegl_node_process (save);
+  if (! gegl_node_process_success (save, &error))
+    {
+      /* Expected error is "Error opening file “/tmp/.ZBD4YZ”: Permission denied"
+       * We test against error domain and code programmatically (no i18n
+       * issue, or string change problems, etc.
+       */
+      success = (error                       &&
+                 error->domain == G_IO_ERROR &&
+                 error->code == G_IO_ERROR_PERMISSION_DENIED);
+    }
+
+  g_object_unref (graph);
+  g_clear_error (&error);
+
+  /* Delete the temp file. */
+  g_unlink (path);
+  g_free (path);
+
+  return success;
+}
+
+int
+main (int argc, char **argv)
+{
+  int success;
+
+  gegl_init (0, NULL);
+  g_object_set (G_OBJECT (gegl_config()),
+                "swap",       "RAM",
+                "use-opencl", FALSE,
+                NULL);
+
+  if (save_denied ())
+    success = 0;
+  else
+    success = -1;
+
+  gegl_exit ();
+
+  return success;
+}


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