[gnome-builder] path: improve expansion of common paths



commit 66fae18c6d1ce428e31703e535d8e55a5bede7ea
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 26 12:35:16 2022 -0800

    path: improve expansion of common paths

 src/libide/io/ide-path.c   | 18 +++++++++++++++++
 src/tests/meson.build      |  7 +++++++
 src/tests/test-libide-io.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
---
diff --git a/src/libide/io/ide-path.c b/src/libide/io/ide-path.c
index 5f2e62fc5..70da88a57 100644
--- a/src/libide/io/ide-path.c
+++ b/src/libide/io/ide-path.c
@@ -46,6 +46,7 @@ gchar *
 ide_path_expand (const gchar *path)
 {
   wordexp_t state = { 0 };
+  char *replace_home = NULL;
   char *ret = NULL;
   char *escaped;
   int r;
@@ -53,6 +54,22 @@ ide_path_expand (const gchar *path)
   if (path == NULL)
     return NULL;
 
+  /* Special case some path prefixes */
+  if (path[0] == '~')
+    {
+      if (path[1] == 0)
+        path = g_get_home_dir ();
+      else if (path[1] == G_DIR_SEPARATOR)
+        path = replace_home = g_strdup_printf ("%s%s", g_get_home_dir (), &path[1]);
+    }
+  else if (strncmp (path, "$HOME", 5) == 0)
+    {
+      if (path[5] == 0)
+        path = g_get_home_dir ();
+      else if (path[5] == G_DIR_SEPARATOR)
+        path = replace_home = g_strdup_printf ("%s%s", g_get_home_dir (), &path[5]);
+    }
+
   escaped = g_shell_quote (path);
   r = wordexp (escaped, &state, WRDE_NOCMD);
   if (r == 0 && state.we_wordc > 0)
@@ -66,6 +83,7 @@ ide_path_expand (const gchar *path)
       ret = g_build_filename (g_get_home_dir (), freeme, NULL);
     }
 
+  g_free (replace_home);
   g_free (escaped);
 
   return ret;
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 281a02094..89006c84f 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -35,6 +35,13 @@ test_libide_core = executable('test-libide-core', 'test-libide-core.c',
 test('test-libide-core', test_libide_core, env: test_env)
 
 
+test_libide_io = executable('test-libide-io', 'test-libide-io.c',
+        c_args: test_cflags,
+  dependencies: [ libide_io_dep ],
+)
+test('test-libide-io', test_libide_io, env: test_env)
+
+
 test_snippet_parser = executable('test-snippet-parser', 'test-snippet-parser.c',
         c_args: test_cflags,
   dependencies: [ libide_sourceview_dep ],
diff --git a/src/tests/test-libide-io.c b/src/tests/test-libide-io.c
new file mode 100644
index 000000000..1fae801ff
--- /dev/null
+++ b/src/tests/test-libide-io.c
@@ -0,0 +1,48 @@
+/* test-libide-core.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include <libide-io.h>
+
+static void
+test_expand (const char *path,
+             char       *expected)
+{
+  g_autofree char *expand = ide_path_expand (path);
+  g_assert_cmpstr (expand, ==, expected);
+  g_free (expected);
+}
+
+static void
+test_path_expand (void)
+{
+  test_expand ("~/", g_build_filename (g_get_home_dir (), G_DIR_SEPARATOR_S, NULL));
+  test_expand ("$HOME/foo", g_build_filename (g_get_home_dir (), "foo", NULL));
+  test_expand ("foo", g_build_filename (g_get_home_dir (), "foo", NULL));
+}
+
+gint
+main (int argc,
+      char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  g_test_add_func ("/libide-io/path/expand", test_path_expand);
+  return g_test_run ();
+}
+


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