[gjs] console: Check script args for stray GJS args



commit 27feb413ed5ae2a9c4973ba10ce06089b6656a08
Author: Philip Chimento <philip endlessm com>
Date:   Mon Oct 31 13:38:41 2016 -0700

    console: Check script args for stray GJS args
    
    In order to ease the pain of migration, check to see if any
    --include-path, --coverage-prefix, or --coverage-output arguments are
    being passed to the JS script. If so, treat them as if they had been
    passed to GJS while issuing a warning that this behaviour will go away in
    the future.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772033

 NEWS                    |    8 +++-
 gjs/console.cpp         |   92 ++++++++++++++++++++++++++++++++++++++++++++++-
 test/testCommandLine.sh |   13 +++++--
 3 files changed, 107 insertions(+), 6 deletions(-)
---
diff --git a/NEWS b/NEWS
index 79cebca..aaaefc2 100644
--- a/NEWS
+++ b/NEWS
@@ -47,8 +47,12 @@ NEXT
   that Python handles its command line arguments as well.
 
   If you previously relied on any -I arguments after your script being added to
-  the search path, then you should either reorder those arguments, or handle -I
-  inside your script, adding paths to imports.searchPath manually.
+  the search path, then you should either reorder those arguments, use GJS_PATH,
+  or handle -I inside your script, adding paths to imports.searchPath manually.
+
+  In order to ease the pain of migration, GJS will continue to take those
+  arguments into account for the time being, while still passing them on to the
+  script. A warning will be logged if you are using the deprecated behaviour.
 
 Version 1.46.0
 --------------
diff --git a/gjs/console.cpp b/gjs/console.cpp
index f9aff03..2472350 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -48,7 +48,8 @@ static GOptionEntry entries[] = {
 };
 
 static char **
-strndupv(int n, char **strv)
+strndupv(int           n,
+         char * const *strv)
 {
     int ix;
     if (n == 0)
@@ -60,6 +61,92 @@ strndupv(int n, char **strv)
     return retval;
 }
 
+static char **
+strcatv(char **strv1,
+        char **strv2)
+{
+    if (strv1 == NULL && strv2 == NULL)
+        return NULL;
+    if (strv1 == NULL)
+        return g_strdupv(strv2);
+    if (strv2 == NULL)
+        return g_strdupv(strv1);
+
+    unsigned len1 = g_strv_length(strv1);
+    unsigned len2 = g_strv_length(strv2);
+    char **retval = g_new(char *, len1 + len2 + 1);
+    unsigned ix;
+
+    for (ix = 0; ix < len1; ix++)
+        retval[ix] = g_strdup(strv1[ix]);
+    for (ix = 0; ix < len2; ix++)
+        retval[len1 + ix] = g_strdup(strv2[ix]);
+    retval[len1 + len2] = NULL;
+
+    return retval;
+}
+
+static void
+check_script_args_for_stray_gjs_args(int           argc,
+                                     char * const *argv)
+{
+    GError *error = NULL;
+    char **new_coverage_prefixes = NULL;
+    char *new_coverage_output_path = NULL;
+    char **new_include_paths = NULL;
+    static GOptionEntry script_check_entries[] = {
+        { "coverage-prefix", 'C', 0, G_OPTION_ARG_STRING_ARRAY, &new_coverage_prefixes },
+        { "coverage-output", 0, 0, G_OPTION_ARG_STRING, &new_coverage_output_path },
+        { "include-path", 'I', 0, G_OPTION_ARG_STRING_ARRAY, &new_include_paths },
+        { NULL }
+    };
+    char **argv_copy = g_new(char *, argc + 2);
+    int ix, argc_copy = argc + 1;
+
+    argv_copy[0] = g_strdup("dummy"); /* Fake argv[0] for GOptionContext */
+    for (ix = 0; ix < argc; ix++)
+        argv_copy[ix + 1] = g_strdup(argv[ix]);
+    argv_copy[argc + 1] = NULL;
+
+    GOptionContext *script_options = g_option_context_new(NULL);
+    g_option_context_set_ignore_unknown_options(script_options, true);
+    g_option_context_set_help_enabled(script_options, false);
+    g_option_context_add_main_entries(script_options, script_check_entries, NULL);
+    if (!g_option_context_parse(script_options, &argc_copy, &argv_copy, &error)) {
+        g_warning("Scanning script arguments failed: %s", error->message);
+        g_error_free(error);
+        return;
+    }
+
+    if (new_coverage_prefixes != NULL) {
+        g_warning("You used the --coverage-prefix option after the script on "
+                  "the GJS command line. Support for this will be removed in a "
+                  "future version. Place the option before the script.");
+        char **old_coverage_prefixes = coverage_prefixes;
+        coverage_prefixes = strcatv(old_coverage_prefixes, new_coverage_prefixes);
+        g_strfreev(old_coverage_prefixes);
+    }
+    if (new_include_paths != NULL) {
+        g_warning("You used the --include-path option after the script on the "
+                  "GJS command line. Support for this will be removed in a "
+                  "future version. Place the option before the script or use "
+                  "the GJS_PATH environment variable.");
+        char **old_include_paths = include_path;
+        include_path = strcatv(old_include_paths, new_include_paths);
+        g_strfreev(old_include_paths);
+    }
+    if (new_coverage_output_path != NULL) {
+        g_warning("You used the --coverage-output option after the script on "
+                  "the GJS command line. Support for this will be removed in a "
+                  "future version. Place the option before the script.");
+        g_free(coverage_output_path);
+        coverage_output_path = new_coverage_output_path;
+    }
+
+    g_option_context_free(script_options);
+    g_strfreev(argv_copy);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -148,6 +235,9 @@ main(int argc, char **argv)
         program_name = gjs_argv[1];
     }
 
+    /* This should be removed after a suitable time has passed */
+    check_script_args_for_stray_gjs_args(script_argc, script_argv);
+
     js_context = (GjsContext*) g_object_new(GJS_TYPE_CONTEXT,
                                             "search-path", include_path,
                                             "program-name", program_name,
diff --git a/test/testCommandLine.sh b/test/testCommandLine.sh
index 417ab16..89ada6c 100755
--- a/test/testCommandLine.sh
+++ b/test/testCommandLine.sh
@@ -57,9 +57,16 @@ test -z "`"$gjs" -c "$script" --help`" || \
     fail "--help after -c should not print anything"
 
 # -I after a program is not consumed by GJS
-if "$gjs" help.js --help -I sentinel; then
-    fail "-I after script file should not be added to search path"
-fi
+# Temporary behaviour: still consume the argument, but give a warning
+# if "$gjs" help.js --help -I sentinel; then
+#     fail "-I after script file should not be added to search path"
+# fi
+"$gjs" help.js --help -I sentinel 2>&1 | grep -q 'Gjs-WARNING.*--include-path' || \
+    fail "-I after script should succeed but give a warning"
+"$gjs" -c 'imports.system.exit(0)' --coverage-prefix=foo --coverage-output=foo 2>&1 | grep -q 
'Gjs-WARNING.*--coverage-prefix' || \
+    fail "--coverage-prefix after script should succeed but give a warning"
+"$gjs" -c 'imports.system.exit(0)' --coverage-prefix=foo --coverage-output=foo 2>&1 | grep -q 
'Gjs-WARNING.*--coverage-output' || \
+    fail "--coverage-output after script should succeed but give a warning"
 
 # --version works
 "$gjs" --version >/dev/null || \


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