[gjs] coverage: Use GFile internally instead of paths



commit 0110886e171140702c0fd7b0a0d04c98444eb53b
Author: Philip Chimento <philip endlessm com>
Date:   Fri Dec 9 17:46:02 2016 -0800

    coverage: Use GFile internally instead of paths
    
    Some paths may be resource paths, so it's better to use GFile rather than
    using g_file_new_for_commandline_arg() to guess.
    
    (Although, we still must use g_file_new_for_commandline_arg() in some of
    the tests.)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775776

 configure.ac                 |    6 +-
 gjs/console.cpp              |    8 ++-
 gjs/coverage-internal.h      |   11 ++--
 gjs/coverage.cpp             |  128 +++++++++++++++++++-------------------
 gjs/coverage.h               |    2 +-
 installed-tests/gjs-unit.cpp |    8 ++-
 modules/coverage.js          |    2 +-
 test/gjs-test-coverage.cpp   |  142 +++++++++++++++++++++---------------------
 8 files changed, 157 insertions(+), 150 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index adf08fe..84adfe7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -57,18 +57,18 @@ m4_define(glib_required_version, 2.50.0)
 GOBJECT_INTROSPECTION_REQUIRE([1.41.4])
 
 GOBJECT_REQUIREMENT="gobject-2.0 >= glib_required_version"
+gjs_base_packages="$GOBJECT_REQUIREMENT gio-2.0"
 common_packages="gthread-2.0 gio-2.0 >= glib_required_version mozjs-31"
 gjs_packages="gobject-introspection-1.0 libffi $common_packages"
 gjs_cairo_packages="cairo cairo-gobject $common_packages"
-gjs_gdbus_packages="$GOBJECT_REQUIREMENT gio-2.0"
 gjs_gtk_packages="gtk+-3.0 >= 3.20"
 # gjs-tests links against everything
 gjstests_packages="gio-unix-2.0 $gjs_packages"
 
 AX_PKG_CHECK_MODULES([GJS], [$GOBJECT_REQUIREMENT], [$gjs_packages])
 dnl These don't need to be put in the .pc file so use regular PKG_CHECK_MODULES
-PKG_CHECK_MODULES([GJS_GDBUS], [$gjs_gdbus_packages])
-PKG_CHECK_MODULES([GJS_CONSOLE], [$GOBJECT_REQUIREMENT])
+PKG_CHECK_MODULES([GJS_GDBUS], [$gjs_base_packages])
+PKG_CHECK_MODULES([GJS_CONSOLE], [$gjs_base_packages])
 PKG_CHECK_MODULES([GJSTESTS], [$gjstests_packages])
 
 # Optional cairo dep (enabled by default)
diff --git a/gjs/console.cpp b/gjs/console.cpp
index acfa8cc..52280ad 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -26,6 +26,8 @@
 #include <stdlib.h>
 #include <locale.h>
 
+#include <gio/gio.h>
+
 #include <gjs/gjs.h>
 
 #include "coverage.h"
@@ -267,10 +269,12 @@ main(int argc, char **argv)
         char *path_to_cache_file = g_build_filename(coverage_output_path,
                                                     GJS_COVERAGE_CACHE_FILE_NAME,
                                                     NULL);
+        GFile *cache_file = g_file_new_for_path(path_to_cache_file);
+        g_free(path_to_cache_file);
         coverage = gjs_coverage_new_from_cache((const gchar **) coverage_prefixes,
                                                js_context,
-                                               path_to_cache_file);
-        g_free(path_to_cache_file);
+                                               cache_file);
+        g_object_unref(cache_file);
     }
 
     /* prepare command line arguments */
diff --git a/gjs/coverage-internal.h b/gjs/coverage-internal.h
index 8ad59e5..d736b75 100644
--- a/gjs/coverage-internal.h
+++ b/gjs/coverage-internal.h
@@ -37,12 +37,13 @@ bool gjs_inject_value_into_coverage_compartment(GjsCoverage     *coverage,
                                                 JS::HandleValue  value,
                                                 const char      *property);
 
-bool gjs_get_path_mtime(const char *path,
-                        GTimeVal   *mtime);
-gchar * gjs_get_path_checksum(const char *path);
+bool gjs_get_file_mtime(GFile    *file,
+                        GTimeVal *mtime);
 
-bool gjs_write_cache_to_path(const char *path,
-                             GBytes     *cache_bytes);
+char *gjs_get_file_checksum(GFile *file);
+
+bool gjs_write_cache_file(GFile  *file,
+                          GBytes *cache_bytes);
 
 extern const char *GJS_COVERAGE_CACHE_FILE_NAME;
 
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 3477a97..9de4f63 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -34,7 +34,7 @@ struct _GjsCoveragePrivate {
     GjsContext *context;
     JS::Heap<JSObject *> coverage_statistics;
 
-    char *cache_path;
+    GFile *cache;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE(GjsCoverage,
@@ -68,6 +68,14 @@ typedef struct _GjsCoverageFunction {
     unsigned int hit_count;
 } GjsCoverageFunction;
 
+static char *
+get_file_identifier(GFile *source_file) {
+    char *path = g_file_get_path(source_file);
+    if (!path)
+        path = g_file_get_uri(source_file);
+    return path;
+}
+
 static void
 write_source_file_header(GOutputStream *stream,
                          const gchar   *source_file_path)
@@ -886,23 +894,18 @@ get_covered_files(GjsCoverage *coverage)
 }
 
 bool
-gjs_get_path_mtime(const char *path, GTimeVal *mtime)
+gjs_get_file_mtime(GFile    *file,
+                   GTimeVal *mtime)
 {
-    /* path could be a resource path, as the callers don't check
-     * if the path is a resource path, but rather if the mtime fetch
-     * operation succeeded. Use g_file_new_for_commandline_arg to handle
-     * that case. */
     GError *error = NULL;
-    GFile *file = g_file_new_for_commandline_arg(path);
     GFileInfo *info = g_file_query_info(file,
                                         "time::modified,time::modified-usec",
                                         G_FILE_QUERY_INFO_NONE,
                                         NULL,
                                         &error);
 
-    g_clear_object(&file);
-
     if (!info) {
+        char *path = get_file_identifier(file);
         g_warning("Failed to get modification time of %s, "
                   "falling back to checksum method for caching. Reason was: %s",
                   path, error->message);
@@ -922,18 +925,13 @@ gjs_get_path_mtime(const char *path, GTimeVal *mtime)
 }
 
 static GBytes *
-read_all_bytes_from_path(const char *path)
+read_all_bytes_from_file(GFile *file)
 {
-    /* path could be a resource, so use g_file_new_for_commandline_arg. */
-    GFile *file = g_file_new_for_commandline_arg(path);
-
     /* We have to use g_file_query_exists here since
      * g_file_test(path, G_FILE_TEST_EXISTS) is implemented in terms
      * of access(), which doesn't work with resource paths. */
-    if (!g_file_query_exists(file, NULL)) {
-        g_object_unref(file);
+    if (!g_file_query_exists(file, NULL))
         return NULL;
-    }
 
     gsize len = 0;
     gchar *data = NULL;
@@ -946,10 +944,11 @@ read_all_bytes_from_path(const char *path)
                               &len,
                               NULL,
                               &error)) {
-        g_printerr("Unable to read bytes from: %s, reason was: %s\n",
+        char *path = get_file_identifier(file);
+        g_critical("Unable to read bytes from: %s, reason was: %s\n",
                    path, error->message);
         g_clear_error(&error);
-        g_object_unref(file);
+        g_free(path);
         return NULL;
     }
 
@@ -957,9 +956,9 @@ read_all_bytes_from_path(const char *path)
 }
 
 gchar *
-gjs_get_path_checksum(const char *path)
+gjs_get_file_checksum(GFile *file)
 {
-    GBytes *data = read_all_bytes_from_path(path);
+    GBytes *data = read_all_bytes_from_file(file);
 
     if (!data)
         return NULL;
@@ -1115,10 +1114,9 @@ gjs_fetch_statistics_from_js(GjsCoverage *coverage,
 }
 
 bool
-gjs_write_cache_to_path(const char *path,
-                        GBytes     *cache)
+gjs_write_cache_file(GFile  *file,
+                     GBytes *cache)
 {
-    GFile *file = g_file_new_for_commandline_arg(path);
     gsize cache_len = 0;
     char *cache_data = (char *) g_bytes_get_data(cache, &cache_len);
     GError *error = NULL;
@@ -1132,7 +1130,7 @@ gjs_write_cache_to_path(const char *path,
                                  NULL,
                                  NULL,
                                  &error)) {
-        g_object_unref(file);
+        char *path = get_file_identifier(file);
         g_warning("Failed to write all bytes to %s, reason was: %s\n",
                   path, error->message);
         g_warning("Will remove this file to prevent inconsistent cache "
@@ -1145,12 +1143,11 @@ gjs_write_cache_to_path(const char *path,
                        "mode again.", path, error->message);
             g_clear_error(&error);
         }
+        g_free(path);
 
         return false;
     }
 
-    g_object_unref(file);
-
     return true;
 }
 
@@ -1222,12 +1219,12 @@ gjs_coverage_write_statistics(GjsCoverage *coverage,
 
     g_strfreev(executed_coverage_files);
 
-    const bool has_cache_path = priv->cache_path != NULL;
+    const bool has_cache_path = priv->cache != NULL;
     const bool cache_is_stale = coverage_statistics_has_stale_cache(coverage);
 
     if (has_cache_path && cache_is_stale) {
         GBytes *cache_data = gjs_serialize_statistics(coverage);
-        gjs_write_cache_to_path(priv->cache_path, cache_data);
+        gjs_write_cache_file(priv->cache, cache_data);
         g_bytes_unref(cache_data);
     }
 
@@ -1357,16 +1354,20 @@ coverage_log(JSContext *context,
     return true;
 }
 
-static char *
-get_filename_from_filename_as_js_string(JSContext    *context,
-                                        JS::CallArgs &args) {
+static GFile *
+get_file_from_call_args_filename(JSContext    *context,
+                                 JS::CallArgs &args) {
     char *filename = NULL;
 
     if (!gjs_parse_call_args(context, "getFileContents", args, "s",
                              "filename", &filename))
         return NULL;
 
-    return filename;
+    /* path could be a resource, so use g_file_new_for_commandline_arg. */
+    GFile *file = g_file_new_for_commandline_arg(filename);
+
+    g_free(filename);
+    return file;
 }
 
 static bool
@@ -1377,12 +1378,12 @@ coverage_get_file_modification_time(JSContext *context,
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     GTimeVal mtime;
     bool ret = false;
-    char *filename = get_filename_from_filename_as_js_string(context, args);
+    GFile *file = get_file_from_call_args_filename(context, args);
 
-    if (!filename)
-        goto out;
+    if (!file)
+        return false;
 
-    if (gjs_get_path_mtime(filename, &mtime)) {
+    if (gjs_get_file_mtime(file, &mtime)) {
         JS::AutoValueArray<2> mtime_values_array(context);
         mtime_values_array[0].setInt32(mtime.tv_sec);
         mtime_values_array[1].setInt32(mtime.tv_usec);
@@ -1398,7 +1399,7 @@ coverage_get_file_modification_time(JSContext *context,
     ret = true;
 
 out:
-    g_free(filename);
+    g_object_unref(file);
     return ret;
 }
 
@@ -1409,15 +1410,18 @@ coverage_get_file_checksum(JSContext *context,
 {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     JSRuntime *runtime = JS_GetRuntime(context);
-    char *filename = get_filename_from_filename_as_js_string(context, args);
+    GFile *file = get_file_from_call_args_filename(context, args);
 
-    if (!filename)
+    if (!file)
         return false;
 
-    char *checksum = gjs_get_path_checksum(filename);
+    char *checksum = gjs_get_file_checksum(file);
 
     if (!checksum) {
+        char *filename = get_file_identifier(file);
         gjs_throw(context, "Failed to read %s and get its checksum", filename);
+        g_free(filename);
+        g_object_unref(file);
         return false;
     }
 
@@ -1425,7 +1429,7 @@ coverage_get_file_checksum(JSContext *context,
                                                                 checksum));
     args.rval().setString(rooted_checksum);
 
-    g_free(filename);
+    g_object_unref(file);
     g_free(checksum);
     return true;
 }
@@ -1437,17 +1441,14 @@ coverage_get_file_contents(JSContext *context,
 {
     bool ret = false;
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-    char *filename = NULL;
     GFile *file = NULL;
     char *script = NULL;
     gsize script_len;
     GError *error = NULL;
 
-    if (!gjs_parse_call_args(context, "getFileContents", args, "s",
-                             "filename", &filename))
-        goto out;
-
-    file = g_file_new_for_commandline_arg(filename);
+    file = get_file_from_call_args_filename(context, args);
+    if (!file)
+        return false;
 
     if (!g_file_load_contents(file,
                               NULL,
@@ -1455,7 +1456,9 @@ coverage_get_file_contents(JSContext *context,
                               &script_len,
                               NULL,
                               &error)) {
+        char *filename = get_file_identifier(file);
         gjs_throw(context, "Failed to load contents for filename %s: %s", filename, error->message);
+        g_free(filename);
         goto out;
     }
 
@@ -1464,9 +1467,7 @@ coverage_get_file_contents(JSContext *context,
 
  out:
     g_clear_error(&error);
-    if (file)
-        g_object_unref(file);
-    g_free(filename);
+    g_object_unref(file);
     g_free(script);
     return ret;
 }
@@ -1644,19 +1645,17 @@ bootstrap_coverage(GjsCoverage *coverage)
         /* Create value for holding the cache. This will be undefined if
          * the cache does not exist, otherwise it will be an object set
          * to the value of the cache */
-        JS::RootedValue cache_value(JS_GetRuntime(context));
+        JS::RootedValue cache_value(context);
 
-        if (priv->cache_path)
-            cache_bytes = read_all_bytes_from_path(priv->cache_path);
+        if (priv->cache)
+            cache_bytes = read_all_bytes_from_file(priv->cache);
 
         if (cache_bytes) {
             JSString *cache_object = gjs_deserialize_cache_to_object_for_compartment(context,
                                                                                      debugger_compartment,
                                                                                      cache_bytes);
-            cache_value.set(JS::StringValue(cache_object));
+            cache_value.setString(cache_object);
             g_bytes_unref(cache_bytes);
-        } else {
-            cache_value.set(JS::UndefinedValue());
         }
 
         /* Now create the array to pass the desired prefixes over */
@@ -1725,7 +1724,8 @@ gjs_coverage_set_property(GObject      *object,
         priv->context = GJS_CONTEXT(g_value_dup_object(value));
         break;
     case PROP_CACHE:
-        priv->cache_path = g_value_dup_string(value);
+        /* g_value_dup_object() adds a reference if not NULL */
+        priv->cache = static_cast<GFile *>(g_value_dup_object(value));
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
@@ -1782,7 +1782,7 @@ gjs_coverage_finalize (GObject *object)
     GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
 
     g_strfreev(priv->prefixes);
-    g_clear_pointer(&priv->cache_path, (GDestroyNotify) g_free);
+    g_clear_object(&priv->cache);
 
     G_OBJECT_CLASS(gjs_coverage_parent_class)->finalize(object);
 }
@@ -1807,10 +1807,10 @@ gjs_coverage_class_init (GjsCoverageClass *klass)
                                                    "A context to gather coverage stats for",
                                                    GJS_TYPE_CONTEXT,
                                                    (GParamFlags) (G_PARAM_CONSTRUCT_ONLY | 
G_PARAM_WRITABLE));
-    properties[PROP_CACHE] = g_param_spec_string("cache",
+    properties[PROP_CACHE] = g_param_spec_object("cache",
                                                  "Cache",
-                                                 "Path to a file containing a cache to preload ASTs from",
-                                                 NULL,
+                                                 "File containing a cache to preload ASTs from",
+                                                 G_TYPE_FILE,
                                                  (GParamFlags) (G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
 
     g_object_class_install_properties(object_class,
@@ -1846,20 +1846,20 @@ gjs_coverage_new (const char **prefixes,
  *
  * @coverage_prefixes: (transfer none): A null-terminated strv of prefixes of files to perform coverage on
  * @context: (transfer full): A #GjsContext object.
- * @cache_path: A path to a file containing a serialized cache.
+ * @cache: A #GFile containing a serialized cache.
  *
  * Returns: A #GjsCoverage object
  */
 GjsCoverage *
 gjs_coverage_new_from_cache(const char **coverage_prefixes,
                             GjsContext *context,
-                            const char *cache_path)
+                            GFile      *cache)
 {
     GjsCoverage *coverage =
         GJS_COVERAGE(g_object_new(GJS_TYPE_COVERAGE,
                                   "prefixes", coverage_prefixes,
                                   "context", context,
-                                  "cache", cache_path,
+                                  "cache", cache,
                                   NULL));
 
     return coverage;
diff --git a/gjs/coverage.h b/gjs/coverage.h
index bf1814d..40f957e 100644
--- a/gjs/coverage.h
+++ b/gjs/coverage.h
@@ -84,7 +84,7 @@ GjsCoverage * gjs_coverage_new(const char   **coverage_prefixes,
 
 GjsCoverage * gjs_coverage_new_from_cache(const char **coverage_prefixes,
                                           GjsContext *context,
-                                          const char *cache_path);
+                                          GFile       *cache);
 
 G_END_DECLS
 
diff --git a/installed-tests/gjs-unit.cpp b/installed-tests/gjs-unit.cpp
index a2075fe..c7b6084 100644
--- a/installed-tests/gjs-unit.cpp
+++ b/installed-tests/gjs-unit.cpp
@@ -30,6 +30,8 @@
 
 #include <string.h>
 
+#include <gio/gio.h>
+
 #include <gjs/gjs.h>
 
 #include "gjs/coverage.h"
@@ -89,10 +91,12 @@ setup(GjsTestJSFixture *fix,
         char *path_to_cache_file = g_build_filename(data->coverage_output_path,
                                                     ".internal-coverage-cache",
                                                     NULL);
+        GFile *cache_file = g_file_new_for_path(path_to_cache_file);
+        g_free(path_to_cache_file);
         fix->coverage = gjs_coverage_new_from_cache((const char **) coverage_prefixes,
                                                     fix->context,
-                                                    path_to_cache_file);
-        g_free(path_to_cache_file);
+                                                    cache_file);
+        g_object_unref(cache_file);
     }
 }
 
diff --git a/modules/coverage.js b/modules/coverage.js
index 1b4f2cc..9f4141b 100644
--- a/modules/coverage.js
+++ b/modules/coverage.js
@@ -766,7 +766,7 @@ function _fetchCountersFromReflection(filename, contents, nLines) {
 
 function CoverageStatisticsContainer(prefixes, cache) {
     /* Copy the files array, so that it can be re-used in the tests */
-    let cachedASTs = cache !== undefined ? JSON.parse(cache) : null;
+    let cachedASTs = cache ? JSON.parse(cache) : null;
     let coveredFiles = {};
     let cacheMisses = 0;
 
diff --git a/test/gjs-test-coverage.cpp b/test/gjs-test-coverage.cpp
index 7354c20..724a0a9 100644
--- a/test/gjs-test-coverage.cpp
+++ b/test/gjs-test-coverage.cpp
@@ -355,7 +355,7 @@ create_coverage_for_script(GjsContext *context,
 
 static GjsCoverage *
 create_coverage_for_script_and_cache(GjsContext *context,
-                                     const char *cache,
+                                     GFile      *cache,
                                      const char *script)
 {
     const char *coverage_scripts[] = {
@@ -1561,7 +1561,7 @@ format_expected_cache_object_notation(const char *mtimes,
 typedef struct _GjsCoverageCacheObjectNotationTestTableData {
     const char *test_name;
     const char *script;
-    const char *resource_path;
+    const char *uri;
     const char *expected_executable_lines;
     const char *expected_branches;
     const char *expected_functions;
@@ -1623,9 +1623,10 @@ test_coverage_cache_data_in_expected_format(gpointer      fixture_data,
     sleep(1);
 
     GTimeVal mtime;
-    bool successfully_got_mtime = gjs_get_path_mtime(fixture->base_fixture.temporary_js_script_filename,
-                                                     &mtime);
+    GFile *file = g_file_new_for_commandline_arg(fixture->base_fixture.temporary_js_script_filename);
+    bool successfully_got_mtime = gjs_get_file_mtime(file, &mtime);
     g_assert_true(successfully_got_mtime);
+    g_object_unref(file);
 
     char *mtime_string = g_strdup_printf("[%li,%li]", mtime.tv_sec, mtime.tv_usec);
     GString *expected_cache_object_notation = format_expected_cache_object_notation(mtime_string,
@@ -1649,23 +1650,27 @@ test_coverage_cache_data_in_expected_format_resource(gpointer      fixture_data,
     GjsCoverageCacheFixture                     *fixture = (GjsCoverageCacheFixture *) fixture_data;
     GjsCoverageCacheObjectNotationTableTestData *table_data = (GjsCoverageCacheObjectNotationTableTestData 
*) user_data;
 
-    char *hash_string_no_quotes = gjs_get_path_checksum(table_data->resource_path);
+    GFile *resource = g_file_new_for_uri(table_data->uri);
+
+    char *hash_string_no_quotes = gjs_get_file_checksum(resource);
+    g_object_unref(resource);
+
     char *hash_string = g_strdup_printf("\"%s\"", hash_string_no_quotes);
     g_free(hash_string_no_quotes);
 
     GString *expected_cache_object_notation = format_expected_cache_object_notation("null",
                                                                                     hash_string,
-                                                                                    
table_data->resource_path,
+                                                                                    table_data->uri,
                                                                                     
table_data->expected_executable_lines,
                                                                                     
table_data->expected_branches,
                                                                                     
table_data->expected_functions);
 
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script(fixture->base_fixture.context,
-                                                                             table_data->resource_path);
+                                                                table_data->uri);
     char *cache_in_object_notation = eval_file_for_ast_in_object_notation(fixture->base_fixture.context,
                                                                           fixture->base_fixture.coverage,
-                                                                          table_data->resource_path);
+                                                                          table_data->uri);
 
     g_assert_cmpstr(cache_in_object_notation, ==, expected_cache_object_notation->str);
 
@@ -1737,33 +1742,43 @@ typedef struct _GjsCoverageCacheEqualResultsTableTestData {
     const char *script;
 } GjsCoverageCacheEqualResultsTableTestData;
 
-static char *
-write_cache_to_temporary_file(const char *temp_dir,
-                              GBytes     *cache)
+
+static GFile *
+get_coverage_tmp_cache(void)
+{
+    GFileIOStream *stream;
+    GError *error = NULL;
+    GFile *cache_file = g_file_new_tmp("gjs-coverage-cache-XXXXXX", &stream, &error);
+    g_assert_no_error(error);
+    g_assert_nonnull(cache_file);
+    g_object_unref(stream);
+
+    return cache_file;
+}
+
+static GFile *
+write_cache_to_temporary_file(GBytes *cache)
 {
-    /* Just need a temporary file, don't care about its fd */
-    char *temporary_file = g_build_filename(temp_dir, "gjs_coverage_cache_XXXXXX", NULL);
-    close(mkstemps(temporary_file, 0));
+    GFile *temporary_file = get_coverage_tmp_cache();
 
-    if (!gjs_write_cache_to_path(temporary_file, cache)) {
-        g_free(temporary_file);
+    if (!gjs_write_cache_file(temporary_file, cache)) {
+        g_object_unref(temporary_file);
         return NULL;
     }
 
     return temporary_file;
 }
 
-static char *
+static GFile *
 serialize_ast_to_cache_in_temporary_file(GjsCoverage *coverage,
-                                         const char  *output_directory,
                                          const char  **coverage_paths)
 {
     GBytes   *cache = serialize_ast_to_bytes(coverage, coverage_paths);
-    char   *cache_path = write_cache_to_temporary_file(output_directory, cache);
+    GFile *cache_file = write_cache_to_temporary_file(cache);
 
     g_bytes_unref(cache);
 
-    return cache_path;
+    return cache_file;
 }
 
 static void
@@ -1787,16 +1802,15 @@ test_coverage_cache_equal_results_to_reflect_parse(gpointer      fixture_data,
                                           fixture->base_fixture.temporary_js_script_filename,
                                           fixture->output_file_directory,
                                           NULL);
-    char *cache_path = serialize_ast_to_cache_in_temporary_file(fixture->base_fixture.coverage,
-                                                                fixture->output_file_directory,
-                                                                coverage_paths);
-    g_assert(cache_path != NULL);
+    GFile *cache_file = serialize_ast_to_cache_in_temporary_file(fixture->base_fixture.coverage,
+                                                                 coverage_paths);
+    g_assert_nonnull(cache_file);
 
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
-    g_free(cache_path);
+    g_object_unref(cache_file);
 
     /* Overwrite tracefile with nothing and start over */
     write_to_file_at_beginning(fixture->output_file_handle, "");
@@ -1814,11 +1828,10 @@ test_coverage_cache_equal_results_to_reflect_parse(gpointer      fixture_data,
     g_free(coverage_data_contents_no_cache);
 }
 
-static char *
-eval_file_for_ast_cache_path(GjsContext  *context,
-                             GjsCoverage *coverage,
-                             const char  *filename,
-                             const char  *output_directory)
+static GFile *
+eval_file_for_tmp_ast_cache(GjsContext  *context,
+                            GjsCoverage *coverage,
+                            const char  *filename)
 {
     bool success = gjs_context_eval_file(context, filename, NULL, NULL);
     g_assert_true(success);
@@ -1829,7 +1842,6 @@ eval_file_for_ast_cache_path(GjsContext  *context,
     };
 
     return serialize_ast_to_cache_in_temporary_file(coverage,
-                                                    output_directory,
                                                     coverage_paths);
 }
 
@@ -1842,10 +1854,9 @@ test_coverage_cache_invalidation(gpointer      fixture_data,
 {
     GjsCoverageCacheFixture *fixture = (GjsCoverageCacheFixture *) fixture_data;
 
-    char *cache_path = eval_file_for_ast_cache_path(fixture->base_fixture.context,
+    GFile *cache_file = eval_file_for_tmp_ast_cache(fixture->base_fixture.context,
                                                     fixture->base_fixture.coverage,
-                                                    fixture->base_fixture.temporary_js_script_filename,
-                                                    fixture->output_file_directory);
+                                                    fixture->base_fixture.temporary_js_script_filename);
 
     /* Sleep for a little while to make sure that the new file has a
      * different mtime */
@@ -1862,9 +1873,9 @@ test_coverage_cache_invalidation(gpointer      fixture_data,
 
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
-    g_free(cache_path);
+    g_object_unref(cache_file);
 
     gsize coverage_data_len = 0;
     char *coverage_data_contents =
@@ -1950,10 +1961,9 @@ test_coverage_cache_invalidation_resource(gpointer      fixture_data,
     fixture->base_fixture.coverage = create_coverage_for_script(fixture->base_fixture.context,
                                                                 mock_resource_filename);
 
-    char *cache_path = eval_file_for_ast_cache_path(fixture->base_fixture.context,
+    GFile *cache_file = eval_file_for_tmp_ast_cache(fixture->base_fixture.context,
                                                     fixture->base_fixture.coverage,
-                                                    mock_resource_filename,
-                                                    fixture->output_file_directory);
+                                                    mock_resource_filename);
 
     /* Load the "after" resource, but have the exact same coverage paths */
     unload_resource(first_resource);
@@ -1964,9 +1974,9 @@ test_coverage_cache_invalidation_resource(gpointer      fixture_data,
 
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           mock_resource_filename);
-    g_free(cache_path);
+    g_object_unref(cache_file);
 
     char *coverage_data_contents =
         eval_script_and_get_coverage_data(fixture->base_fixture.context,
@@ -2014,28 +2024,16 @@ test_coverage_cache_invalidation_resource(gpointer      fixture_data,
     g_free(coverage_data_contents);
 }
 
-static char *
-get_coverage_cache_path(const char *output_directory)
-{
-    char *cache_path = g_build_filename(output_directory,
-                                        "coverage-cache-XXXXXX",
-                                        NULL);
-    close(mkstemp(cache_path));
-    unlink(cache_path);
-
-    return cache_path;
-}
-
 static void
 test_coverage_cache_file_written_when_no_cache_exists(gpointer      fixture_data,
                                                       gconstpointer user_data)
 {
     GjsCoverageCacheFixture *fixture = (GjsCoverageCacheFixture *) fixture_data;
-    char *cache_path = get_coverage_cache_path(fixture->output_file_directory);
+    GFile *cache_file = get_coverage_tmp_cache();
 
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
 
     /* We need to execute the script now in order for a cache entry
@@ -2050,14 +2048,14 @@ test_coverage_cache_file_written_when_no_cache_exists(gpointer      fixture_data
     gjs_coverage_write_statistics(fixture->base_fixture.coverage,
                                   fixture->output_file_directory);
 
-    g_assert_true(g_file_test(cache_path, G_FILE_TEST_EXISTS));
-    g_free(cache_path);
+    g_assert_true(g_file_query_exists(cache_file, NULL));
+    g_object_unref(cache_file);
 }
 
 static GTimeVal
 eval_script_for_cache_mtime(GjsContext  *context,
                             GjsCoverage *coverage,
-                            const char  *cache_path,
+                            GFile       *cache_file,
                             const char  *script,
                             const char  *output_directory)
 {
@@ -2068,7 +2066,7 @@ eval_script_for_cache_mtime(GjsContext  *context,
                                   output_directory);
 
     GTimeVal mtime;
-    bool successfully_got_mtime = gjs_get_path_mtime(cache_path, &mtime);
+    bool successfully_got_mtime = gjs_get_file_mtime(cache_file, &mtime);
     g_assert_true(successfully_got_mtime);
 
     return mtime;
@@ -2080,15 +2078,15 @@ test_coverage_cache_updated_when_cache_stale(gpointer      fixture_data,
 {
     GjsCoverageCacheFixture *fixture = (GjsCoverageCacheFixture *) fixture_data;
 
-    char *cache_path = get_coverage_cache_path(fixture->output_file_directory);
+    GFile *cache_file = get_coverage_tmp_cache();
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
 
     GTimeVal first_cache_mtime = eval_script_for_cache_mtime(fixture->base_fixture.context,
                                                              fixture->base_fixture.coverage,
-                                                             cache_path,
+                                                             cache_file,
                                                              
fixture->base_fixture.temporary_js_script_filename,
                                                              fixture->output_file_directory);
 
@@ -2105,7 +2103,7 @@ test_coverage_cache_updated_when_cache_stale(gpointer      fixture_data,
     /* Re-create coverage object, covering new script */
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
 
 
@@ -2115,7 +2113,7 @@ test_coverage_cache_updated_when_cache_stale(gpointer      fixture_data,
      * so the second mtime will be greater than the first */
     GTimeVal second_cache_mtime = eval_script_for_cache_mtime(fixture->base_fixture.context,
                                                               fixture->base_fixture.coverage,
-                                                              cache_path,
+                                                              cache_file,
                                                               
fixture->base_fixture.temporary_js_script_filename,
                                                               fixture->output_file_directory);
 
@@ -2125,7 +2123,7 @@ test_coverage_cache_updated_when_cache_stale(gpointer      fixture_data,
 
     g_assert_true(seconds_different || microseconds_different);
 
-    g_free(cache_path);
+    g_object_unref(cache_file);
 }
 
 static void
@@ -2134,22 +2132,22 @@ test_coverage_cache_not_updated_on_full_hits(gpointer      fixture_data,
 {
     GjsCoverageCacheFixture *fixture = (GjsCoverageCacheFixture *) fixture_data;
 
-    char *cache_path = get_coverage_cache_path(fixture->output_file_directory);
+    GFile *cache_file = get_coverage_tmp_cache();
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
 
     GTimeVal first_cache_mtime = eval_script_for_cache_mtime(fixture->base_fixture.context,
                                                              fixture->base_fixture.coverage,
-                                                             cache_path,
+                                                             cache_file,
                                                              
fixture->base_fixture.temporary_js_script_filename,
                                                              fixture->output_file_directory);
 
     /* Re-create coverage object, covering same script */
     g_clear_object(&fixture->base_fixture.coverage);
     fixture->base_fixture.coverage = create_coverage_for_script_and_cache(fixture->base_fixture.context,
-                                                                          cache_path,
+                                                                          cache_file,
                                                                           
fixture->base_fixture.temporary_js_script_filename);
 
 
@@ -2159,14 +2157,14 @@ test_coverage_cache_not_updated_on_full_hits(gpointer      fixture_data,
      * so the second mtime will be the same as the first */
     GTimeVal second_cache_mtime = eval_script_for_cache_mtime(fixture->base_fixture.context,
                                                               fixture->base_fixture.coverage,
-                                                              cache_path,
+                                                              cache_file,
                                                               
fixture->base_fixture.temporary_js_script_filename,
                                                               fixture->output_file_directory);
 
     g_assert_cmpint(first_cache_mtime.tv_sec, ==, second_cache_mtime.tv_sec);
     g_assert_cmpint(first_cache_mtime.tv_usec, ==, second_cache_mtime.tv_usec);
 
-    g_free(cache_path);
+    g_object_unref(cache_file);
 }
 
 typedef struct _FixturedTest {



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