[gjs] coverage: Coverage without cache internal-only



commit 79d0b865ec646d8f6eb0c103201610fd0f1b321d
Author: Philip Chimento <philip endlessm com>
Date:   Fri Dec 9 18:20:49 2016 -0800

    coverage: Coverage without cache internal-only
    
    This removes the gjs_coverage_new_for_cache() constructor. Instead,
    gjs_coverage_new() always creates a cache in the default location, since
    it is unlikely that you would ever want to run coverage without a cache
    except in GjsCoverage's unit tests.
    
    For the tests, this adds two new constructors to coverage-internal.h,
    _gjs_coverage_new_internal_with_cache() and
    _gjs_coverage_new_internal_without_cache().
    
    As for creating a GjsCoverage object directly with g_object_new(), you
    can unfortunately still get whatever behaviour you want from the public
    API. If you don't specify a "cache" property at construct time, you get
    the default cache. If you do specify it and it is NULL, you get no cache.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775776

 gjs/console.cpp              |    8 +-----
 gjs/coverage-internal.h      |   11 +++++++++
 gjs/coverage.cpp             |   49 +++++++++++++++++++++++------------------
 gjs/coverage.h               |    5 ----
 installed-tests/gjs-unit.cpp |    6 +----
 test/gjs-test-coverage.cpp   |   34 +++++++++++++++++------------
 6 files changed, 61 insertions(+), 52 deletions(-)
---
diff --git a/gjs/console.cpp b/gjs/console.cpp
index f705f44..34c41e0 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -38,8 +38,6 @@ static char *coverage_output_path = NULL;
 static char *command = NULL;
 static gboolean print_version = false;
 
-static const char *GJS_COVERAGE_CACHE_FILE_NAME = ".internal-gjs-coverage-cache";
-
 static GOptionEntry entries[] = {
     { "version", 0, 0, G_OPTION_ARG_NONE, &print_version, "Print GJS version and exit" },
     { "command", 'c', 0, G_OPTION_ARG_STRING, &command, "Program passed in as a string", "COMMAND" },
@@ -267,11 +265,9 @@ main(int argc, char **argv)
             g_error("--coverage-output is required when taking coverage statistics");
 
         GFile *output = g_file_new_for_commandline_arg(coverage_output_path);
-        GFile *cache_file = g_file_get_child(output, GJS_COVERAGE_CACHE_FILE_NAME);
-        coverage = gjs_coverage_new_from_cache((const gchar **) coverage_prefixes,
-                                               js_context, output, cache_file);
+        coverage = gjs_coverage_new((const char **) coverage_prefixes, js_context,
+                                    output);
         g_object_unref(output);
-        g_object_unref(cache_file);
     }
 
     /* prepare command line arguments */
diff --git a/gjs/coverage-internal.h b/gjs/coverage-internal.h
index d736b75..a720ad4 100644
--- a/gjs/coverage-internal.h
+++ b/gjs/coverage-internal.h
@@ -21,11 +21,22 @@
 #ifndef _GJS_COVERAGE_INTERNAL_H
 #define _GJS_COVERAGE_INTERNAL_H
 
+#include <gio/gio.h>
+
 #include "jsapi-util.h"
 #include "coverage.h"
 
 G_BEGIN_DECLS
 
+GjsCoverage *_gjs_coverage_new_internal_with_cache(const char * const *coverage_prefixes,
+                                                   GjsContext         *context,
+                                                   GFile              *output_dir,
+                                                   GFile              *cache_path);
+
+GjsCoverage *_gjs_coverage_new_internal_without_cache(const char * const *prefixes,
+                                                      GjsContext         *cx,
+                                                      GFile              *output_dir);
+
 GBytes * gjs_serialize_statistics(GjsCoverage *coverage);
 
 JSString * gjs_deserialize_cache_to_object(GjsCoverage *coverage,
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index ccad654..8580a0c 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -36,6 +36,8 @@ struct _GjsCoveragePrivate {
 
     GFile *output_dir;
     GFile *cache;
+    /* tells whether priv->cache == NULL means no cache, or not specified */
+    bool cache_specified;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE(GjsCoverage,
@@ -1687,6 +1689,11 @@ gjs_coverage_constructed(GObject *object)
 
     JSContext *context = (JSContext *) gjs_context_get_native_context(priv->context);
 
+    if (!priv->cache_specified) {
+        g_message("Cache path was not given, picking default one");
+        priv->cache = g_file_new_for_path(".internal-gjs-coverage-cache");
+    }
+
     /* Before bootstrapping, turn off the JIT on the context */
     JS::RuntimeOptionsRef(context)
         .setIon(false)
@@ -1716,6 +1723,7 @@ gjs_coverage_set_property(GObject      *object,
         priv->context = GJS_CONTEXT(g_value_dup_object(value));
         break;
     case PROP_CACHE:
+        priv->cache_specified = true;
         /* g_value_dup_object() adds a reference if not NULL */
         priv->cache = static_cast<GFile *>(g_value_dup_object(value));
         break;
@@ -1821,10 +1829,16 @@ gjs_coverage_class_init (GjsCoverageClass *klass)
 
 /**
  * gjs_coverage_new:
- * @coverage_prefixes: (transfer none): A null-terminated strv of prefixes of files to perform coverage on
+ * @prefixes: A null-terminated strv of prefixes of files on which to record
+ * code coverage
+ * @context: A #GjsContext object
  * @output_dir: A #GFile handle to a directory in which to write coverage
  * information
  *
+ * Creates a new #GjsCoverage object, using a cache in a temporary file to
+ * pre-fill the AST information for the specified scripts in @prefixes, so long
+ * as the data in the cache has the same mtime as those scripts.
+ *
  * Scripts which were provided as part of @prefixes will be written out to
  * @output_dir, in the same directory structure relative to the source dir where
  * the tests were run.
@@ -1846,28 +1860,11 @@ gjs_coverage_new (const char **prefixes,
     return coverage;
 }
 
-/**
- * gjs_coverage_new_from_cache:
- * Creates a new GjsCoverage object, but uses @cache_path to pre-fill the AST information for
- * the specified scripts in coverage_paths, so long as the data in the cache has the same
- * mtime as those scripts.
- *
- * @coverage_prefixes: (transfer none): A null-terminated strv of prefixes of files to perform coverage on
- * @context: (transfer full): A #GjsContext object.
- * @output_dir: #GFile for directory write coverage information to.
- * @cache: A #GFile containing a serialized cache.
- *
- * Scripts which were provided as part of @coverage_prefixes will be written out
- * to @output_dir, in the same directory structure relative to the source dir
- * where the tests were run.
- *
- * Returns: A #GjsCoverage object
- */
 GjsCoverage *
-gjs_coverage_new_from_cache(const char **coverage_prefixes,
-                            GjsContext *context,
-                            GFile       *output_dir,
-                            GFile      *cache)
+_gjs_coverage_new_internal_with_cache(const char * const *coverage_prefixes,
+                                      GjsContext         *context,
+                                      GFile              *output_dir,
+                                      GFile              *cache)
 {
     GjsCoverage *coverage =
         GJS_COVERAGE(g_object_new(GJS_TYPE_COVERAGE,
@@ -1879,3 +1876,11 @@ gjs_coverage_new_from_cache(const char **coverage_prefixes,
 
     return coverage;
 }
+
+GjsCoverage *
+_gjs_coverage_new_internal_without_cache(const char * const *prefixes,
+                                         GjsContext         *cx,
+                                         GFile              *output_dir)
+{
+    return _gjs_coverage_new_internal_with_cache(prefixes, cx, output_dir, NULL);
+}
diff --git a/gjs/coverage.h b/gjs/coverage.h
index 0954b56..3646c5c 100644
--- a/gjs/coverage.h
+++ b/gjs/coverage.h
@@ -78,11 +78,6 @@ GjsCoverage * gjs_coverage_new(const char   **coverage_prefixes,
                                GjsContext    *coverage_context,
                                GFile         *output_dir);
 
-GjsCoverage * gjs_coverage_new_from_cache(const char **coverage_prefixes,
-                                          GjsContext *context,
-                                          GFile       *output_dir,
-                                          GFile       *cache);
-
 G_END_DECLS
 
 #endif
diff --git a/installed-tests/gjs-unit.cpp b/installed-tests/gjs-unit.cpp
index b4ac8ee..3f602ec 100644
--- a/installed-tests/gjs-unit.cpp
+++ b/installed-tests/gjs-unit.cpp
@@ -89,12 +89,8 @@ setup(GjsTestJSFixture *fix,
         }
 
         GFile *output = g_file_new_for_commandline_arg(data->coverage_output_path);
-        GFile *cache_file = g_file_get_child(output, ".internal-coverage-cache");
-        fix->coverage = gjs_coverage_new_from_cache((const char **) coverage_prefixes,
-                                                    fix->context, output,
-                                                    cache_file);
+        fix->coverage = gjs_coverage_new(coverage_prefixes, fix->context, output);
         g_object_unref(output);
-        g_object_unref(cache_file);
     }
 }
 
diff --git a/test/gjs-test-coverage.cpp b/test/gjs-test-coverage.cpp
index a6649bd..fb27d93 100644
--- a/test/gjs-test-coverage.cpp
+++ b/test/gjs-test-coverage.cpp
@@ -118,8 +118,9 @@ gjs_coverage_fixture_set_up(gpointer      fixture_data,
     };
 
     fixture->context = gjs_context_new_with_search_path((char **) search_paths);
-    fixture->coverage = gjs_coverage_new((const char **)coverage_paths, fixture->context,
-                                         fixture->lcov_output_dir);
+    fixture->coverage =
+        _gjs_coverage_new_internal_without_cache(coverage_paths, fixture->context,
+                                                 fixture->lcov_output_dir);
 
     replace_file(fixture->tmp_js_script, js_script);
     g_free(tmp_output_dir_name);
@@ -309,8 +310,9 @@ create_coverage_for_script(GjsContext *context,
         NULL
     };
 
-    GjsCoverage *retval = gjs_coverage_new((const char **) coverage_scripts,
-                                           context, output_dir);
+    GjsCoverage *retval =
+        _gjs_coverage_new_internal_without_cache(coverage_scripts, context,
+                                                 output_dir);
     g_free(script_path);
     return retval;
 }
@@ -327,8 +329,9 @@ create_coverage_for_script_and_cache(GjsContext *context,
         NULL
     };
 
-    GjsCoverage *retval = gjs_coverage_new_from_cache((const char **) coverage_scripts,
-                                                      context, output_dir, cache);
+    GjsCoverage *retval =
+        _gjs_coverage_new_internal_with_cache(coverage_scripts, context,
+                                              output_dir, cache);
     g_free(script_path);
     return retval;
 }
@@ -355,8 +358,9 @@ test_covered_file_is_duplicated_into_output_if_resource(gpointer      fixture_da
 
     fixture->context = gjs_context_new_with_search_path(search_paths);
     fixture->coverage =
-        gjs_coverage_new(coverage_scripts, fixture->context,
-                         fixture->lcov_output_dir);
+        _gjs_coverage_new_internal_without_cache(coverage_scripts,
+                                                 fixture->context,
+                                                 fixture->lcov_output_dir);
 
     gjs_context_eval_file(fixture->context,
                           mock_resource_filename,
@@ -499,9 +503,10 @@ test_expected_entry_not_written_for_nonexistent_file(gpointer      fixture_data,
     };
 
     g_object_unref(fixture->coverage);
-    fixture->coverage = gjs_coverage_new(coverage_paths,
-                                         fixture->context,
-                                         fixture->lcov_output_dir);
+    fixture->coverage =
+        _gjs_coverage_new_internal_without_cache(coverage_paths,
+                                                 fixture->context,
+                                                 fixture->lcov_output_dir);
 
     /* Temporarily disable fatal mask and silence warnings */
     GLogLevelFlags old_flags = g_log_set_always_fatal((GLogLevelFlags) G_LOG_LEVEL_ERROR);
@@ -1265,9 +1270,10 @@ gjs_coverage_multiple_source_files_to_single_output_fixture_set_up(gpointer fixt
     };
 
     fixture->base_fixture.context = gjs_context_new_with_search_path(search_paths);
-    fixture->base_fixture.coverage = gjs_coverage_new((const char **) coverage_paths,
-                                                      fixture->base_fixture.context,
-                                                      fixture->base_fixture.lcov_output_dir);
+    fixture->base_fixture.coverage =
+        _gjs_coverage_new_internal_without_cache(coverage_paths,
+                                                 fixture->base_fixture.context,
+                                                 fixture->base_fixture.lcov_output_dir);
 
     g_free(output_path);
     g_free(first_js_script_path);


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