[gjs] test: handle multiple absolute paths when building filename



commit 14904c48311619803128c0487a12f6fc9c7396d2
Author: Tommi Komulainen <tko litl com>
Date:   Thu Mar 18 11:19:57 2010 +0000

    test: handle multiple absolute paths when building filename
    
    When GJS_TOP_SRCDIR is an absolute path g_build_filename constructs the
    paths in a way we're not expecting, e.g. given path elements
    
        ["/top_builddir", "/top_srcdir", NULL]
    
    we're expecting to get "/top_srcdir" but g_build_filename is returning
    "/top_builddir/top_srcdir". This results in gjs-unit looking for test
    files in wrong directory and dying with
    
        ** ERROR:(gjs-unit.c:172):main: assertion failed: (dir != NULL)
    
    Add custom function to handle absolute paths as we're expecting.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=613234

 test/gjs-unit.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 52 insertions(+), 8 deletions(-)
---
diff --git a/test/gjs-unit.c b/test/gjs-unit.c
index c605bf8..e531529 100644
--- a/test/gjs-unit.c
+++ b/test/gjs-unit.c
@@ -118,10 +118,57 @@ read_all_dir_sorted (const char *dirpath)
     return result;
 }
 
+/* Always return an absolute filename and treat all absolute path elements as
+ * absolute, not just the first one, e.g.
+ *     ("/foo", "/bar", NULL) returns "/bar"
+ * (g_build_filename would return "/foo/bar")
+ * Returned path may still include '..' path elements.
+ */
+static char *
+build_absolute_filename(const char *first_element,
+                        ...)
+{
+    const char *element;
+    va_list ap;
+    char *cwd;
+    char *path;
+    GPtrArray *array;
+
+    cwd = g_get_current_dir();
+
+    array = g_ptr_array_new();
+
+    g_ptr_array_add(array, cwd);
+
+    va_start(ap, first_element);
+
+    element = first_element;
+    while (element != NULL) {
+        if (g_path_is_absolute(element))
+            g_ptr_array_set_size(array, 0);
+
+        g_ptr_array_add(array, (char*)element);
+
+        element = va_arg(ap, const char *);
+    }
+    va_end(ap);
+
+    g_ptr_array_add(array, NULL);
+
+    path = g_build_filenamev((char **)array->pdata);
+
+    g_ptr_array_free(array, TRUE);
+
+    g_free(cwd);
+
+    return path;
+}
+
 int
 main(int argc, char **argv)
 {
-    /* These are relative to top_builddir */
+    /* These may be absolute or relative to top_builddir, depending whether
+     * GJS_TOP_SRCDIR is absolute or not */
     const char * const path_directories[] = {
         GJS_TOP_SRCDIR"/modules",
         GJS_TOP_SRCDIR"/test/js/modules",
@@ -141,15 +188,12 @@ main(int argc, char **argv)
 
     working_dir = g_get_current_dir();
 
-    if(g_path_is_absolute(argv[0]))
-        gjs_unit_path = g_strdup(argv[0]);
-    else
-        gjs_unit_path = g_build_filename(working_dir, argv[0], NULL);
+    gjs_unit_path = build_absolute_filename(argv[0], NULL);
 
     gjs_unit_dir = g_path_get_dirname(gjs_unit_path);
     /* the gjs-unit executable will be in <top_builddir>/.libs */
-    top_builddir = g_build_filename(gjs_unit_dir, "..", NULL);
-    top_srcdir = g_build_filename(top_builddir, GJS_TOP_SRCDIR, NULL);
+    top_builddir = g_path_get_dirname(gjs_unit_dir);
+    top_srcdir = build_absolute_filename(top_builddir, GJS_TOP_SRCDIR, NULL);
 
     /* Normalize, not strictly necessary */
     g_chdir(top_builddir);
@@ -173,7 +217,7 @@ main(int argc, char **argv)
         if (i != 0)
             g_string_append_c(path, ':');
 
-        directory = g_build_filename(top_builddir, path_directories[i], NULL);
+        directory = build_absolute_filename(top_builddir, path_directories[i], NULL);
         g_string_append(path, directory);
         g_free(directory);
     }



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