[gnome-builder] uri: fix to_string generation for non-file URIs



commit 0fd1bfa3ea40435e0b067f27de47173578ee5591
Author: Christian Hergert <christian hergert me>
Date:   Thu Apr 28 15:57:22 2016 -0700

    uri: fix to_string generation for non-file URIs
    
    This was using legacy fields from the uri work-in-progress which were
    outdated. We really need to get a GUri that we can use instead.

 libide/ide-uri.c     |   65 ++++++++++++++---------
 tests/Makefile.am    |    6 ++
 tests/test-ide-uri.c |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 189 insertions(+), 25 deletions(-)
---
diff --git a/libide/ide-uri.c b/libide/ide-uri.c
index 6106370..3bb3f15 100644
--- a/libide/ide-uri.c
+++ b/libide/ide-uri.c
@@ -25,6 +25,12 @@
 
 #include "ide-uri.h"
 
+#if 0
+# define GOTO(l) do { g_print ("%s():%d\n", G_STRFUNC, __LINE__); goto l; } while (0)
+#else
+# define GOTO(l) goto l
+#endif
+
 /**
  * SECTION:ide-uri
  * @short_description: URI-handling utilities
@@ -128,12 +134,6 @@ struct _IdeUri
   gchar   *path;
   gchar   *query;
   gchar   *fragment;
-
-  gchar   *encoded_userinfo;
-  gchar   *encoded_host;
-  gchar   *encoded_path;
-  gchar   *encoded_query;
-  gchar   *encoded_fragment;
 };
 
 G_DEFINE_BOXED_TYPE (IdeUri, ide_uri, ide_uri_ref, ide_uri_unref)
@@ -613,7 +613,7 @@ ide_uri_new_relative (IdeUri            *base_uri,
     {
       g_set_error_literal (error, IDE_URI_ERROR, IDE_URI_ERROR_MISC,
                            _("URI is not absolute, and no base URI was provided"));
-      goto fail;
+      GOTO (fail);
     }
 
   if (raw->user)
@@ -621,37 +621,37 @@ ide_uri_new_relative (IdeUri            *base_uri,
       if (!parse_userinfo (raw->user, flags,
                            &uri->user, &uri->password, &uri->auth_params,
                            error))
-        goto fail;
+        GOTO (fail);
     }
 
   if (raw->host)
     {
       if (!parse_host (raw->host, flags, &uri->host, error))
-        goto fail;
+        GOTO (fail);
     }
 
   if (raw_port)
     {
       if (!parse_port (raw_port, &uri->port, error))
-        goto fail;
+        GOTO (fail);
     }
 
   uri->path = uri_normalize (raw->path, flags, IDE_URI_ERROR_BAD_PATH, error);
   if (!uri->path)
-    goto fail;
+    GOTO (fail);
 
   if (raw->query)
     {
       uri->query = uri_normalize (raw->query, flags, IDE_URI_ERROR_BAD_QUERY, error);
       if (!uri->query)
-        goto fail;
+        GOTO (fail);
     }
 
   if (raw->fragment)
     {
       uri->fragment = uri_normalize (raw->fragment, flags, IDE_URI_ERROR_BAD_FRAGMENT, error);
       if (!uri->fragment)
-        goto fail;
+        GOTO (fail);
     }
 
   if (!uri->scheme && !base_uri)
@@ -659,7 +659,7 @@ ide_uri_new_relative (IdeUri            *base_uri,
       g_set_error (error, IDE_URI_ERROR, IDE_URI_ERROR_MISC,
                    _("Could not parse '%s' as absolute URI"),
                    uri_string);
-      goto fail;
+      GOTO (fail);
     }
 
   if (base_uri)
@@ -764,9 +764,24 @@ ide_uri_to_string (IdeUri              *uri,
     {
       g_string_append (str, "//");
 
-      if (uri->encoded_userinfo && !(flags & IDE_URI_HIDE_AUTH_PARAMS))
+      if (uri->user)
         {
-          g_string_append (str, uri->encoded_userinfo);
+          g_string_append (str, uri->user);
+
+          if (!(flags & IDE_URI_HIDE_AUTH_PARAMS))
+            {
+              if (uri->auth_params)
+                {
+                  g_string_append_c (str, ':');
+                  g_string_append (str, uri->auth_params);
+                }
+              else if (uri->password)
+                {
+                  g_string_append_c (str, ':');
+                  g_string_append (str, uri->password);
+                }
+            }
+
           g_string_append_c (str, '@');
         }
 
@@ -777,18 +792,18 @@ ide_uri_to_string (IdeUri              *uri,
         g_string_append_printf (str, ":%d", uri->port);
     }
 
-  if (uri->encoded_path)
-    g_string_append (str, uri->encoded_path);
+  if (uri->path)
+    g_string_append (str, uri->path);
 
-  if (uri->encoded_query)
+  if (uri->query)
     {
       g_string_append_c (str, '?');
-      g_string_append (str, uri->encoded_query);
+      g_string_append (str, uri->query);
     }
-  if (uri->encoded_fragment && !(flags & IDE_URI_HIDE_FRAGMENT))
+  if (uri->fragment && !(flags & IDE_URI_HIDE_FRAGMENT))
     {
       g_string_append_c (str, '#');
-      g_string_append (str, uri->encoded_fragment);
+      g_string_append (str, uri->fragment);
     }
 
   return g_string_free (str, FALSE);
@@ -1187,19 +1202,19 @@ ide_uri_parse_host (const gchar       *uri_string,
       g_set_error (error, IDE_URI_ERROR, IDE_URI_ERROR_BAD_HOST,
                    _("URI '%s' has no host component"),
                    uri_string);
-      goto fail;
+      GOTO (fail);
     }
 
   if (raw_port)
     {
       if (!parse_port (raw_port, port, error))
-        goto fail;
+        GOTO (fail);
     }
   else
     *port = 0;
 
   if (!parse_host (raw_host, flags, host, error))
-    goto fail;
+    GOTO (fail);
 
   *scheme = raw_scheme;
   g_free (raw_host);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a9810f2..a2d45da 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -122,6 +122,12 @@ test_ide_vcs_uri_CFLAGS = $(tests_cflags)
 test_ide_vcs_uri_LDADD = $(tests_libs)
 
 
+TESTS += test-ide-uri
+test_ide_uri_SOURCES = test-ide-uri.c
+test_ide_uri_CFLAGS = $(tests_cflags)
+test_ide_uri_LDADD = $(tests_libs)
+
+
 #TESTS += test-c-parse-helper
 #test_c_parse_helper_SOURCES = test-c-parse-helper.c
 #test_c_parse_helper_CFLAGS = \
diff --git a/tests/test-ide-uri.c b/tests/test-ide-uri.c
new file mode 100644
index 0000000..c351eea
--- /dev/null
+++ b/tests/test-ide-uri.c
@@ -0,0 +1,143 @@
+/* test-ide-uri.c
+ *
+ * Copyright (C) 2016 Christian Hergert <christian hergert me>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <ide.h>
+
+static void
+test_uri_file (void)
+{
+  static struct {
+    const gchar *input;
+    const gchar *output;
+  } uris[] = {
+    { "file:///tmp/foo.txt", "file:///tmp/foo.txt" },
+    { "file:///tmp/foo.txt#a=1", "file:///tmp/foo.txt#a=1" },
+    { "file:///tmp", "file:///tmp" },
+#if 0 /* Broken */
+    { "file:///tmp/foo/var///baz", "file:///tmp/foo/var/baz" },
+#endif
+  };
+  guint i;
+
+  for (i = 0; i < G_N_ELEMENTS (uris); i++)
+    {
+      GError *error = NULL;
+      g_autoptr(IdeUri) uri = ide_uri_new (uris[i].input, 0, &error);
+
+      g_assert_no_error (error);
+
+      if (uris[i].output)
+        {
+          g_autofree gchar *str = NULL;
+
+          g_assert (uri != NULL);
+
+          str = ide_uri_to_string (uri, 0);
+          g_assert_cmpstr (str, ==, uris[i].output);
+        }
+    }
+
+  /* Test creation from file, when there is no # fragment */
+  for (i = 0; i < G_N_ELEMENTS (uris); i++)
+    {
+      if (uris[i].output && !strchr (uris[i].input, '#'))
+        {
+          g_autoptr(GFile) file = g_file_new_for_uri (uris[i].input);
+          g_autoptr(IdeUri) uri = ide_uri_new_from_file (file);
+          g_autofree gchar *str = NULL;
+
+          g_assert (uri != NULL);
+
+          str = ide_uri_to_string (uri, 0);
+          g_assert_cmpstr (str, ==, uris[i].output);
+        }
+    }
+}
+
+static void
+test_uri_sftp (void)
+{
+  static struct {
+    const gchar *input;
+    const gchar *output;
+  } uris[] = {
+    { "sftp://127.0.0.1:1234/foo/bar/#baz";, "sftp://127.0.0.1:1234/foo/bar/#baz"; },
+  };
+  guint i;
+
+  for (i = 0; i < G_N_ELEMENTS (uris); i++)
+    {
+      GError *error = NULL;
+      g_autoptr(IdeUri) uri = ide_uri_new (uris[i].input, 0, &error);
+
+      g_assert_no_error (error);
+
+      if (uris[i].output)
+        {
+          g_autofree gchar *str = NULL;
+
+          g_assert (uri != NULL);
+
+          str = ide_uri_to_string (uri, 0);
+          g_assert_cmpstr (str, ==, uris[i].output);
+        }
+    }
+}
+
+static void
+test_uri_smb (void)
+{
+  static struct {
+    const gchar *input;
+    const gchar *output;
+  } uris[] = {
+    { "smb://homie/foo/bar/", "smb://homie/foo/bar/" },
+  };
+  guint i;
+
+  for (i = 0; i < G_N_ELEMENTS (uris); i++)
+    {
+      GError *error = NULL;
+      g_autoptr(IdeUri) uri = ide_uri_new (uris[i].input, 0, &error);
+
+      g_assert_no_error (error);
+
+      if (uris[i].output)
+        {
+          g_autofree gchar *str = NULL;
+
+          g_assert (uri != NULL);
+
+          str = ide_uri_to_string (uri, 0);
+          g_assert_cmpstr (str, ==, uris[i].output);
+        }
+    }
+}
+
+gint
+main (int argc,
+      gchar *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/Ide/Uri/file", test_uri_file);
+  g_test_add_func ("/Ide/Uri/sftp", test_uri_sftp);
+  g_test_add_func ("/Ide/Uri/smb", test_uri_smb);
+
+  return g_test_run ();
+}


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