[gjs/wip/enum-gtype: 1/2] coverage: Use prefixes, not paths



commit 2ed4026877d719da80e39cf88e7206b24440bcd4
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Feb 3 14:07:05 2014 -0500

    coverage: Use prefixes, not paths

 Makefile-test.am             |   20 +--------------
 gjs/console.cpp              |    8 +++---
 gjs/coverage.cpp             |   41 +++++++++++++++----------------
 gjs/coverage.h               |    2 +-
 installed-tests/gjs-unit.cpp |   54 +++++++----------------------------------
 modules/coverage.js          |   14 ++++------
 6 files changed, 42 insertions(+), 97 deletions(-)
---
diff --git a/Makefile-test.am b/Makefile-test.am
index b91840b..ad5444e 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -60,27 +60,9 @@ TESTS_ENVIRONMENT =                                                  \
        G_FILENAME_ENCODING=latin1      # ensure filenames are not utf8
 
 if ENABLE_COVERAGE
-# These paths are resource paths but they have resource://
-# stripped out, for ease of parsing in the test runner
-coverage_paths = \
-       /org/gnome/gjs/modules/cairo.js \
-       /org/gnome/gjs/modules/coverage.js \
-       /org/gnome/gjs/modules/format.js \
-       /org/gnome/gjs/modules/gettext.js \
-       /org/gnome/gjs/modules/jsUnit.js \
-       /org/gnome/gjs/modules/lang.js \
-       /org/gnome/gjs/modules/mainloop.js \
-       /org/gnome/gjs/modules/signals.js
-
-empty :=
-space := $(empty) $(empty)
-colon := :
-
-coverage_env := $(subst $(space),$(colon),$(coverage_paths))
-
 TESTS_ENVIRONMENT += \
        GJS_UNIT_COVERAGE_OUTPUT=lcov \
-       GJS_UNIT_COVERAGE_PATHS=$(coverage_env)
+       GJS_UNIT_COVERAGE_PREFIX=resource:///org/gnome/gjs/
 endif
 
 ########################################################################
diff --git a/gjs/console.cpp b/gjs/console.cpp
index 411a753..50adca8 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -30,13 +30,13 @@
 #include <gjs/coverage.h>
 
 static char **include_path = NULL;
-static char **coverage_paths = NULL;
+static char **coverage_prefixes = NULL;
 static char *coverage_output_path = NULL;
 static char *command = NULL;
 
 static GOptionEntry entries[] = {
     { "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
-    { "coverage-path", 'C', 0, G_OPTION_ARG_STRING_ARRAY, &coverage_paths, "Add the filename FILE to the 
list of files to generate coverage info for", "FILE" },
+    { "coverage-prefix", 'C', 0, G_OPTION_ARG_STRING_ARRAY, &coverage_prefixes, "Add the prefix PREFIX to 
the list of files to generate coverage info for", "PREFIX" },
     { "coverage-output", 0, 0, G_OPTION_ARG_STRING, &coverage_output_path, "Write coverage output to a 
directory DIR. This option is mandatory when using --coverage-path", "DIR", },
     { "include-path", 'I', 0, G_OPTION_ARG_STRING_ARRAY, &include_path, "Add the directory DIR to the list 
of directories to search for js files.", "DIR" },
     { NULL }
@@ -117,11 +117,11 @@ main(int argc, char **argv)
                                             "program-name", program_name,
                                             NULL);
 
-    if (coverage_paths) {
+    if (coverage_prefixes) {
         if (!coverage_output_path)
             g_error("--coverage-output is required when taking coverage statistics");
 
-        coverage = gjs_coverage_new((const gchar **) coverage_paths,
+        coverage = gjs_coverage_new((const gchar **) coverage_prefixes,
                                     js_context);
     }
 
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index a9b982d..60b92c5 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -30,10 +30,9 @@ typedef struct _GjsDebugHooks GjsDebugHooks;
 typedef struct _GjsCoverageBranchData GjsCoverageBranchData;
 
 struct _GjsCoveragePrivate {
-    gchar         **covered_paths;
-
-    GjsContext    *context;
-    JSObject      *coverage_statistics;
+    gchar **prefixes;
+    GjsContext *context;
+    JSObject *coverage_statistics;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE(GjsCoverage,
@@ -42,7 +41,7 @@ G_DEFINE_TYPE_WITH_PRIVATE(GjsCoverage,
 
 enum {
     PROP_0,
-    PROP_COVERAGE_PATHS,
+    PROP_PREFIXES,
     PROP_CONTEXT,
     PROP_N
 };
@@ -1128,11 +1127,11 @@ bootstrap_coverage(GjsCoverage *coverage)
 
         JSObject *coverage_statistics_constructor = JSVAL_TO_OBJECT(coverage_statistics_prototype_value);
 
-        /* Now create the array to pass the desired script names over */
-        JSObject *filenames_js_array = gjs_build_string_array(context, -1, priv->covered_paths);
+        /* Now create the array to pass the desired prefixes over */
+        JSObject *prefixes = gjs_build_string_array(context, -1, priv->prefixes);
 
         jsval coverage_statistics_constructor_arguments[] = {
-            OBJECT_TO_JSVAL(filenames_js_array)
+            OBJECT_TO_JSVAL(prefixes)
         };
 
         JSObject *coverage_statistics = JS_New(context,
@@ -1181,9 +1180,9 @@ gjs_coverage_set_property(GObject      *object,
     GjsCoverage *coverage = GJS_DEBUG_COVERAGE(object);
     GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
     switch (prop_id) {
-    case PROP_COVERAGE_PATHS:
-        g_assert(priv->covered_paths == NULL);
-        priv->covered_paths = (char **) g_value_dup_boxed (value);
+    case PROP_PREFIXES:
+        g_assert(priv->prefixes == NULL);
+        priv->prefixes = (char **) g_value_dup_boxed (value);
         break;
     case PROP_CONTEXT:
         priv->context = GJS_CONTEXT(g_value_dup_object(value));
@@ -1211,7 +1210,7 @@ gjs_coverage_finalize (GObject *object)
     GjsCoverage *coverage = GJS_DEBUG_COVERAGE(object);
     GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
 
-    g_strfreev(priv->covered_paths);
+    g_strfreev(priv->prefixes);
 
     G_OBJECT_CLASS(gjs_coverage_parent_class)->finalize(object);
 }
@@ -1226,11 +1225,11 @@ gjs_coverage_class_init (GjsCoverageClass *klass)
     object_class->finalize = gjs_coverage_finalize;
     object_class->set_property = gjs_coverage_set_property;
 
-    properties[PROP_COVERAGE_PATHS] = g_param_spec_boxed("coverage-paths",
-                                                         "Coverage Paths",
-                                                         "Paths (and included subdirectories) of which to 
perform coverage analysis",
-                                                         G_TYPE_STRV,
-                                                         (GParamFlags) (G_PARAM_CONSTRUCT_ONLY | 
G_PARAM_WRITABLE));
+    properties[PROP_PREFIXES] = g_param_spec_boxed("prefixes",
+                                                   "Prefixes",
+                                                   "Prefixes of files on which to perform coverage analysis",
+                                                   G_TYPE_STRV,
+                                                   (GParamFlags) (G_PARAM_CONSTRUCT_ONLY | 
G_PARAM_WRITABLE));
     properties[PROP_CONTEXT] = g_param_spec_object("context",
                                                    "Context",
                                                    "A context to gather coverage stats for",
@@ -1244,18 +1243,18 @@ gjs_coverage_class_init (GjsCoverageClass *klass)
 
 /**
  * gjs_coverage_new:
- * @coverage_paths: (transfer none): A null-terminated strv of directories to generate
+ * @coverage_prefixes: (transfer none): A null-terminated strv of prefixes of files to perform coverage on
  * coverage_data for
  *
  * Returns: A #GjsDebugCoverage
  */
 GjsCoverage *
-gjs_coverage_new (const char    **coverage_paths,
-                  GjsContext    *context)
+gjs_coverage_new (const char **prefixes,
+                  GjsContext  *context)
 {
     GjsCoverage *coverage =
         GJS_DEBUG_COVERAGE(g_object_new(GJS_TYPE_DEBUG_COVERAGE,
-                                        "coverage-paths", coverage_paths,
+                                        "prefixes", prefixes,
                                         "context", context,
                                         NULL));
 
diff --git a/gjs/coverage.h b/gjs/coverage.h
index 9293183..c013e4f 100644
--- a/gjs/coverage.h
+++ b/gjs/coverage.h
@@ -78,7 +78,7 @@ GType gjs_debug_coverage_get_type(void);
 void gjs_coverage_write_statistics(GjsCoverage *coverage,
                                    const char  *output_directory);
 
-GjsCoverage * gjs_coverage_new(const char    **covered_directories,
+GjsCoverage * gjs_coverage_new(const char   **coverage_prefixes,
                                GjsContext    *coverage_context);
 
 G_END_DECLS
diff --git a/installed-tests/gjs-unit.cpp b/installed-tests/gjs-unit.cpp
index 5a33a42..512aa55 100644
--- a/installed-tests/gjs-unit.cpp
+++ b/installed-tests/gjs-unit.cpp
@@ -33,18 +33,18 @@
 #include <string.h>
 
 typedef struct {
-    const char **coverage_paths;
+    const char *coverage_prefix;
     const char *coverage_output_path;
     char       *filename;
 } GjsTestData;
 
 GjsTestData *
-gjs_unit_test_data_new(const char **coverage_paths,
+gjs_unit_test_data_new(const char *coverage_prefix,
                        const char *coverage_output_path,
                        char       *filename)
 {
     GjsTestData *data = (GjsTestData *) g_new0(GjsTestData, 1);
-    data->coverage_paths = coverage_paths;
+    data->coverage_prefix = coverage_prefix;
     data->coverage_output_path = coverage_output_path;
     data->filename = filename;
     return data;
@@ -70,13 +70,14 @@ setup(GjsTestJSFixture *fix,
     GjsTestData *data = (GjsTestData *) test_data;
     fix->context = gjs_context_new ();
 
-    if (data->coverage_paths) {
+    if (data->coverage_prefix) {
+        const char *coverage_prefixes[2] = { data->coverage_prefix, NULL };
+
         if (!data->coverage_output_path) {
-            g_error("GJS_UNIT_COVERAGE_OUTPUT is required when using GJS_UNIT_COVERAGE_PATHS");
+            g_error("GJS_UNIT_COVERAGE_OUTPUT is required when using GJS_UNIT_COVERAGE_PREFIX");
         }
 
-        fix->coverage = gjs_coverage_new((const char **) data->coverage_paths,
-                                         fix->context);
+        fix->coverage = gjs_coverage_new(coverage_prefixes, fix->context);
     }
 }
 
@@ -134,36 +135,6 @@ read_all_dir_sorted (const char *dirpath)
     return result;
 }
 
-static char **
-get_coverage_uris_from_env(const char *key)
-{
-    const char *value = g_getenv(key);
-
-    if (!value)
-        return NULL;
-
-    char **splitted_pathnames = g_strsplit(value, ":", -1);
-    char **splitted_pathnamed_iterator = splitted_pathnames;
-
-    for (; *splitted_pathnamed_iterator; splitted_pathnamed_iterator++) {
-
-        /* Don't just have resource:// for the last element */
-        if (g_strcmp0(*splitted_pathnamed_iterator, "") == 0) {
-            g_free(*splitted_pathnamed_iterator);
-            *splitted_pathnamed_iterator = NULL;
-            continue;
-        }
-
-        char *uri = g_strconcat("resource://",
-                                *splitted_pathnamed_iterator,
-                                NULL);
-        g_free(*splitted_pathnamed_iterator);
-        *splitted_pathnamed_iterator = uri;
-    }
-
-    return splitted_pathnames;
-}
-
 int
 main(int argc, char **argv)
 {
@@ -191,7 +162,7 @@ main(int argc, char **argv)
         js_test_dir = g_build_filename(INSTTESTDIR, "js", NULL);
     }
 
-    char       **coverage_paths = get_coverage_uris_from_env("GJS_UNIT_COVERAGE_PATHS");
+    const char *coverage_prefix = g_getenv("GJS_UNIT_COVERAGE_PREFIX");
     const char *coverage_output_directory = g_getenv("GJS_UNIT_COVERAGE_OUTPUT");
 
     all_tests = read_all_dir_sorted(js_test_dir);
@@ -214,9 +185,7 @@ main(int argc, char **argv)
         test_name[strlen(test_name)-3] = '\0';
 
         file_name = g_build_filename(js_test_dir, name, NULL);
-        test_data = gjs_unit_test_data_new((const char **) coverage_paths,
-                                           coverage_output_directory,
-                                           file_name);
+        test_data = gjs_unit_test_data_new(coverage_prefix, coverage_output_directory, file_name);
         g_test_add(test_name, GjsTestJSFixture, test_data, setup, test, teardown);
         g_free(name);
         g_free(test_name);
@@ -232,8 +201,5 @@ main(int argc, char **argv)
                     all_registered_test_data);
     g_slist_free(all_registered_test_data);
 
-    if (coverage_paths)
-        g_strfreev(coverage_paths);
-
     return retval;
 }
diff --git a/modules/coverage.js b/modules/coverage.js
index 8bd12ac..84b2ba1 100644
--- a/modules/coverage.js
+++ b/modules/coverage.js
@@ -653,18 +653,16 @@ function _convertFunctionCountersToArray(functionCounters) {
     return arrayReturn;
 }
 
-function CoverageStatisticsContainer(files) {
-    let pendingFiles = files;
+function CoverageStatisticsContainer(prefixes) {
     let coveredFiles = {};
 
     function wantsStatisticsFor(filename) {
-        return pendingFiles.indexOf(filename) !== -1;
+        return prefixes.some(function(prefix) {
+            return filename.startsWith(prefix);
+        });
     }
 
     function createStatisticsFor(filename) {
-        let idx = pendingFiles.indexOf(filename);
-        pendingFiles.splice(idx, 1);
-
         let contents = getFileContents(filename);
         let reflection = Reflect.parse(contents);
         let nLines = _getNumberOfLinesForScript(contents);
@@ -708,8 +706,8 @@ function CoverageStatisticsContainer(files) {
  *
  * It isn't poissible to unit test this class because it depends on running
  * Debugger which in turn depends on objects injected in from another compartment */
-function CoverageStatistics(files) {
-    this.container = new CoverageStatisticsContainer(files);
+function CoverageStatistics(prefixes) {
+    this.container = new CoverageStatisticsContainer(prefixes);
     let fetchStatistics = this.container.fetchStatistics.bind(this.container);
     let deleteStatistics = this.container.deleteStatistics.bind(this.container);
 


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