[gnome-software/wip/jrocha/fix_popular_overrides: 16/16] Add self tests for the core plugin



commit a217c898864b44dcf9bf694e3e970f0858513c31
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Thu Apr 13 16:13:21 2017 +0100

    Add self tests for the core plugin
    
    The tests cover currently gs_appstream_create_app.

 lib/gs-plugin-loader.c      |    2 +-
 lib/gs-plugin-loader.h      |    3 +
 plugins/core/gs-self-test.c |  142 +++++++++++++++++++++++++++++++++++++++++++
 plugins/core/meson.build    |   24 +++++++
 4 files changed, 170 insertions(+), 1 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index cb5d856..54e9f08 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -308,7 +308,7 @@ gs_plugin_loader_app_sort_id_cb (GsApp *app1, GsApp *app2, gpointer user_data)
        return g_strcmp0 (gs_app_get_id (app1), gs_app_get_id (app2));
 }
 
-static GsPlugin *
+GsPlugin *
 gs_plugin_loader_find_plugin (GsPluginLoader *plugin_loader,
                              const gchar *plugin_name)
 {
diff --git a/lib/gs-plugin-loader.h b/lib/gs-plugin-loader.h
index 98fce06..db8b95c 100644
--- a/lib/gs-plugin-loader.h
+++ b/lib/gs-plugin-loader.h
@@ -280,6 +280,9 @@ GsApp               *gs_plugin_loader_app_create            (GsPluginLoader 
*plugin_loader,
 /* only useful from the self tests */
 void            gs_plugin_loader_setup_again           (GsPluginLoader *plugin_loader);
 void            gs_plugin_loader_clear_caches  (GsPluginLoader *plugin_loader);
+GsPlugin       *gs_plugin_loader_find_plugin   (GsPluginLoader *plugin_loader,
+                                                const gchar    *plugin_name);
+
 
 G_END_DECLS
 
diff --git a/plugins/core/gs-self-test.c b/plugins/core/gs-self-test.c
new file mode 100644
index 0000000..5a248c0
--- /dev/null
+++ b/plugins/core/gs-self-test.c
@@ -0,0 +1,142 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Joaquim Rocha <jrocha endlessm com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#include "gnome-software-private.h"
+
+#include "gs-appstream.h"
+#include "gs-test.h"
+
+static void
+gs_plugins_core_app_creation_func (GsPluginLoader *plugin_loader)
+{
+       g_autoptr(GsApp) app = NULL;
+       AsApp *as_app = NULL;
+       GsApp *cached_app = NULL;
+       g_autoptr(AsStore) store = NULL;
+       g_autoptr(GError) error = NULL;
+       GsPlugin *plugin;
+       const gchar *test_icon_root = g_getenv ("GS_SELF_TEST_APPSTREAM_ICON_ROOT");
+       g_autofree gchar *xml = g_strdup ("<?xml version=\"1.0\"?>\n"
+                                         "<components version=\"0.9\">\n"
+                                         "  <component type=\"desktop\">\n"
+                                         "    <id>demeter.desktop</id>\n"
+                                         "    <name>Demeter</name>\n"
+                                         "    <summary>An agriculture application</summary>\n"
+                                         "  </component>\n"
+                                         "</components>\n");
+
+       /* drop all caches */
+       gs_plugin_loader_setup_again (plugin_loader);
+
+       app = gs_plugin_loader_app_create (plugin_loader,
+                                          "*/*/*/desktop/demeter.desktop/*");
+       gs_app_add_quirk (app, AS_APP_QUIRK_MATCH_ANY_PREFIX);
+
+       cached_app = gs_plugin_loader_app_create (plugin_loader,
+                                                 "*/*/*/desktop/demeter.desktop/*");
+
+       g_assert (app == cached_app);
+
+       /* Make sure the app still has the match-any-prefix quirk*/
+       g_assert(gs_app_has_quirk (app, AS_APP_QUIRK_MATCH_ANY_PREFIX));
+
+       /* Ensure gs_appstream creates a new app when a matching one is cached but
+        * has the match-any-prefix quirk */
+       store = as_store_new ();
+       if (!as_store_from_xml (store, xml, test_icon_root, &error)) {
+               g_warning (error->message);
+               return;
+       }
+
+       as_app = as_store_get_app_by_id (store, "demeter.desktop");
+       g_assert (as_app != NULL);
+
+       plugin = gs_plugin_loader_find_plugin (plugin_loader, "appstream");
+       g_assert (plugin != NULL);
+
+       g_clear_object (&app);
+       app = gs_appstream_create_app (plugin, as_app, NULL);
+       g_assert (app != NULL);
+       g_assert (cached_app != app);
+       g_assert (!gs_app_has_quirk (app, AS_APP_QUIRK_MATCH_ANY_PREFIX));
+
+       g_clear_object (&cached_app);
+       cached_app = gs_plugin_loader_app_create (plugin_loader,
+                                                 "*/*/*/desktop/demeter.desktop/*");
+       g_assert (cached_app == app);
+}
+
+int
+main (int argc, char **argv)
+{
+       const gchar *tmp_root = "/var/tmp/self-test";
+       gboolean ret;
+       g_autofree gchar *xml = NULL;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsPluginLoader) plugin_loader = NULL;
+       const gchar *whitelist[] = {
+               "appstream",
+               "icons",
+               NULL
+       };
+
+       g_test_init (&argc, &argv, NULL);
+       g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
+       g_setenv ("GS_SELF_TEST_CORE_DATADIR", tmp_root, TRUE);
+
+       /* ensure test root does not exist */
+       if (g_file_test (tmp_root, G_FILE_TEST_EXISTS)) {
+               ret = gs_utils_rmtree (tmp_root, &error);
+               g_assert_no_error (error);
+               g_assert (ret);
+               g_assert (!g_file_test (tmp_root, G_FILE_TEST_EXISTS));
+       }
+
+       //g_setenv ("GS_SELF_TEST_APPSTREAM_XML", xml, TRUE);
+       g_setenv ("GS_SELF_TEST_APPSTREAM_ICON_ROOT",
+                 "/var/tmp/self-test/flatpak/appstream/test/x86_64/active/", TRUE);
+
+       /* only critical and error are fatal */
+       g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
+
+       /* we can only load this once per process */
+       plugin_loader = gs_plugin_loader_new ();
+       gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR);
+       gs_plugin_loader_add_location (plugin_loader, LOCALPLUGINDIR_CORE);
+       ret = gs_plugin_loader_setup (plugin_loader,
+                                     (gchar**) whitelist,
+                                     NULL,
+                                     GS_PLUGIN_FAILURE_FLAGS_NONE,
+                                     NULL,
+                                     &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+
+       /* plugin tests go here */
+       g_test_add_data_func ("/gnome-software/plugins/core/app-creation",
+                             plugin_loader,
+                             (GTestDataFunc) gs_plugins_core_app_creation_func);
+       return g_test_run ();
+}
+
+/* vim: set noexpandtab: */
diff --git a/plugins/core/meson.build b/plugins/core/meson.build
index b04ed3d..9a989fd 100644
--- a/plugins/core/meson.build
+++ b/plugins/core/meson.build
@@ -153,3 +153,27 @@ shared_module(
   c_args : cargs,
   dependencies : plugin_libs
 )
+
+if get_option('enable-tests')
+  cargs += ['-DLOCALPLUGINDIR="' + meson.current_build_dir() + '"']
+  cargs += ['-DLOCALPLUGINDIR_CORE="' + meson.current_build_dir() + '/../core"']
+  cargs += ['-DTESTDATADIR="' + join_paths(meson.current_build_dir(), 'tests') + '"']
+  e = executable('gs-self-test-core',
+    sources : [
+      'gs-self-test.c',
+      'gs-appstream.c'
+    ],
+    include_directories : [
+      include_directories('../..'),
+      include_directories('../../lib'),
+    ],
+    dependencies : [
+      plugin_libs,
+    ],
+    link_with : [
+      libgnomesoftware
+    ],
+    c_args : cargs,
+  )
+  test('gs-self-test-core', e)
+endif


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