[gjs/wip/ptomato/develop] tests: Speed up coverage tests



commit ecae9d8f2dc39b2706465517a53d821c5e1fa46c
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Sep 18 22:35:49 2017 -0700

    tests: Speed up coverage tests
    
    Several coverage tests have a sleep(1) in the middle. This is annoying.
    In one case it's not necessary. In other cases, achieve the same effect
    by modifying the coverage cache file's mtime on disk.

 test/gjs-test-coverage.cpp |   34 ++++++++++++++++++++++++----------
 1 files changed, 24 insertions(+), 10 deletions(-)
---
diff --git a/test/gjs-test-coverage.cpp b/test/gjs-test-coverage.cpp
index 5ce5d57..10d08a7 100644
--- a/test/gjs-test-coverage.cpp
+++ b/test/gjs-test-coverage.cpp
@@ -1542,10 +1542,6 @@ test_coverage_cache_data_in_expected_format(gpointer      fixture_data,
                                                                           fixture->tmp_js_script);
     g_assert(cache_in_object_notation != NULL);
 
-    /* Sleep for a little while to make sure that the new file has a
-     * different mtime */
-    sleep(1);
-
     GTimeVal mtime;
     bool successfully_got_mtime = gjs_get_file_mtime(fixture->tmp_js_script, &mtime);
     g_assert_true(successfully_got_mtime);
@@ -1775,6 +1771,28 @@ eval_file_for_tmp_ast_cache(GjsContext  *context,
     return retval;
 }
 
+static void
+rewind_file_mtime(GFile *file)
+{
+    GjsAutoUnref<GFileInfo> info = g_file_query_info(file,
+                                                     G_FILE_ATTRIBUTE_TIME_MODIFIED,
+                                                     G_FILE_QUERY_INFO_NONE,
+                                                     nullptr, nullptr);
+    g_assert_nonnull(info);
+
+    GTimeVal mtime;
+    g_file_info_get_modification_time(info, &mtime);
+
+    g_assert_cmpint(mtime.tv_sec, >, 0);
+    mtime.tv_sec--;
+    g_file_info_set_modification_time(info, &mtime);
+
+    gboolean ok = g_file_set_attributes_from_info(file, info,
+                                                  G_FILE_QUERY_INFO_NONE,
+                                                  nullptr, nullptr);
+    g_assert_true(ok);
+}
+
 /* Effectively, the results should be what we expect even though
  * we overwrote the original script after getting coverage and
  * fetching the cache */
@@ -1788,9 +1806,7 @@ test_coverage_cache_invalidation(gpointer      fixture_data,
                                                     fixture->coverage,
                                                     fixture->tmp_js_script);
 
-    /* Sleep for a little while to make sure that the new file has a
-     * different mtime */
-    sleep(1);
+    rewind_file_mtime(cache_file);
 
     /* Overwrite tracefile with nothing */
     replace_file(fixture->lcov_output, "");
@@ -2018,9 +2034,7 @@ test_coverage_cache_updated_when_cache_stale(gpointer      fixture_data,
                                                              cache_file,
                                                              fixture->tmp_js_script);
 
-    /* Sleep for a little while to make sure that the new file has a
-     * different mtime */
-    sleep(1);
+    rewind_file_mtime(cache_file);
 
     /* Write a new script into the temporary js file, which will be
      * completely different to the original script that was there */


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