[gnome-software/wip/jrocha/fix-file-to-app-logic: 3/3] flatpak: Set the correct origin when installing a flatpakref



commit 997a9acd1e17a89dbcdb09a17cb16cb6385c6425
Author: Joaquim Rocha <jrocha endlessm com>
Date:   Sat Jul 1 01:33:00 2017 +0200

    flatpak: Set the correct origin when installing a flatpakref
    
    When installing a flatpakref, part of the process is to install its
    ref file, and the code was assuming that this installation always
    results in a new remote (named as app_name-origin). However, if there
    is already a remote with the same URI as the one coming from the ref
    file, it will not add the new one in order to avoid redudancy.
    
    Since the GsApp representing the one in the flatpakref was being
    assigned the "app_name-origin" as its origin, it would fail to install
    later because this expected origin may not exist (as explained above).
    
    To fix this, once the ref file is installed, we get the resulting
    remote's name (i.e. a new one or the existing one) and override the
    app's origin with it, which will allow the installation to succeed.

 plugins/flatpak/gs-flatpak.c   |    8 ++
 plugins/flatpak/gs-self-test.c |  147 ++++++++++++++++++++++++++++++++++------
 2 files changed, 133 insertions(+), 22 deletions(-)
---
diff --git a/plugins/flatpak/gs-flatpak.c b/plugins/flatpak/gs-flatpak.c
index 9fa9666..e5c4e9f 100644
--- a/plugins/flatpak/gs-flatpak.c
+++ b/plugins/flatpak/gs-flatpak.c
@@ -2825,6 +2825,14 @@ gs_flatpak_app_install (GsFlatpak *self,
                        return FALSE;
                }
 
+               /* the installation of the ref file above will not create a new remote for
+                * the app if its URL is already configured as another remote, thus we
+                * need to update the app origin to match that or it may end up with
+                * an nonexistent origin; and we first need to set the origin to NULL to
+                * circumvent the safety check... */
+               gs_app_set_origin (app, NULL);
+               gs_app_set_origin (app, flatpak_remote_ref_get_remote_name (xref2));
+
                /* update search tokens for new remote */
                if (!gs_flatpak_refresh_appstream (self, G_MAXUINT, 0, cancellable, error))
                        return FALSE;
diff --git a/plugins/flatpak/gs-self-test.c b/plugins/flatpak/gs-self-test.c
index e9d65d8..9096f90 100644
--- a/plugins/flatpak/gs-self-test.c
+++ b/plugins/flatpak/gs-self-test.c
@@ -562,12 +562,58 @@ update_app_action_finish_sync (GObject *source, GAsyncResult *res, gpointer user
 }
 
 static void
+gs_plugins_flatpak_create_flatpak_ref (const gchar *app_id,
+                                      const gchar *app_url,
+                                      const gchar *repo_url,
+                                      const gchar *fn_ref)
+{
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GString) str = g_string_new (NULL);
+
+       g_string_append (str, "[Flatpak Ref]\n");
+       g_string_append_printf (str, "Title=%s\n", app_id);
+       g_string_append_printf (str, "Name=%s\n", app_id);
+       g_string_append (str, "Branch=master\n");
+       g_string_append_printf (str, "Url=%s\n", app_url);
+       g_string_append (str, "IsRuntime=False\n");
+       g_string_append (str, "Comment=Single line synopsis\n");
+       g_string_append (str, "Description=A Testing Application\n");
+       g_string_append (str, "Icon=https://getfedora.org/static/images/fedora-logotext.png\n";);
+       g_string_append (str, "Icon=RuntimeRepo=https://sdk.gnome.org/gnome-nightly.flatpakrepo\n";);
+       g_string_append_printf (str, "RuntimeRepo=%s\n", repo_url);
+       ret = g_file_set_contents (fn_ref, str->str, -1, &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+}
+
+static void
+gs_plugins_flatpak_create_flatpak_repo (const gchar *repo_title,
+                                       const gchar *repo_url,
+                                       const gchar *fn_repo)
+{
+       gboolean ret;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GString) str = g_string_new (NULL);
+
+       g_string_append (str, "[Flatpak Repo]\n");
+       g_string_append_printf (str, "Title=%s\n", repo_title);
+       g_string_append (str, "DefaultBranch=master\n");
+       g_string_append_printf (str, "Url=%s\n", repo_url);
+       g_string_append (str, "GPGKey=FOOBAR==\n");
+       ret = g_file_set_contents (fn_repo, str->str, -1, &error);
+       g_assert_no_error (error);
+       g_assert (ret);
+}
+
+static void
 gs_plugins_flatpak_runtime_repo_func (GsPluginLoader *plugin_loader)
 {
        GsApp *app_source;
        GsApp *runtime;
        const gchar *fn_ref = "/var/tmp/self-test/test.flatpakref";
        const gchar *fn_repo = "/var/tmp/self-test/test.flatpakrepo";
+       const gchar *fn_app_repo = "/var/tmp/self-test/test-app.flatpakrepo";
        gboolean ret;
        g_autofree gchar *fn_repourl = NULL;
        g_autofree gchar *testdir2 = NULL;
@@ -582,6 +628,7 @@ gs_plugins_flatpak_runtime_repo_func (GsPluginLoader *plugin_loader)
        g_autoptr(GsPluginJob) plugin_job = NULL;
        g_autoptr(GString) str2 = g_string_new (NULL);
        g_autoptr(GString) str = g_string_new (NULL);
+       g_autoptr(GFile) repo_file = NULL;
 
        /* drop all caches */
        gs_plugin_loader_setup_again (plugin_loader);
@@ -591,14 +638,7 @@ gs_plugins_flatpak_runtime_repo_func (GsPluginLoader *plugin_loader)
        if (testdir == NULL)
                return;
        testdir_repourl = g_strdup_printf ("file://%s/repo", testdir);
-       g_string_append (str, "[Flatpak Repo]\n");
-       g_string_append (str, "Title=foo-bar\n");
-       g_string_append (str, "DefaultBranch=master\n");
-       g_string_append_printf (str, "Url=%s\n", testdir_repourl);
-       g_string_append (str, "GPGKey=FOOBAR==\n");
-       ret = g_file_set_contents (fn_repo, str->str, -1, &error);
-       g_assert_no_error (error);
-       g_assert (ret);
+       gs_plugins_flatpak_create_flatpak_repo ("foo-bar", testdir_repourl, fn_repo);
 
        /* write a flatpakref file */
        fn_repourl = g_strdup_printf ("file://%s", fn_repo);
@@ -606,20 +646,9 @@ gs_plugins_flatpak_runtime_repo_func (GsPluginLoader *plugin_loader)
        if (testdir2 == NULL)
                return;
        testdir2_repourl = g_strdup_printf ("file://%s/repo", testdir2);
-       g_string_append (str2, "[Flatpak Ref]\n");
-       g_string_append (str2, "Title=Chiron\n");
-       g_string_append (str2, "Name=org.test.Chiron\n");
-       g_string_append (str2, "Branch=master\n");
-       g_string_append_printf (str2, "Url=%s\n", testdir2_repourl);
-       g_string_append (str2, "IsRuntime=False\n");
-       g_string_append (str2, "Comment=Single line synopsis\n");
-       g_string_append (str2, "Description=A Testing Application\n");
-       g_string_append (str2, "Icon=https://getfedora.org/static/images/fedora-logotext.png\n";);
-       g_string_append (str2, "Icon=RuntimeRepo=https://sdk.gnome.org/gnome-nightly.flatpakrepo\n";);
-       g_string_append_printf (str2, "RuntimeRepo=%s\n", fn_repourl);
-       ret = g_file_set_contents (fn_ref, str2->str, -1, &error);
-       g_assert_no_error (error);
-       g_assert (ret);
+
+       gs_plugins_flatpak_create_flatpak_ref ("org.test.Chiron", testdir2_repourl,
+                                              fn_repourl, fn_ref);
 
        /* convert it to a GsApp */
        file = g_file_new_for_path (fn_ref);
@@ -708,6 +737,80 @@ gs_plugins_flatpak_runtime_repo_func (GsPluginLoader *plugin_loader)
        g_assert_no_error (error);
        g_assert (ret);
        g_assert_cmpint (gs_app_get_state (app_source), ==, AS_APP_STATE_AVAILABLE);
+
+       /* install the repo directly */
+       gs_plugins_flatpak_create_flatpak_repo ("test-apps", testdir2_repourl,
+                                               fn_app_repo);
+
+       gs_plugin_loader_clear_caches (plugin_loader);
+       repo_file = g_file_new_for_path (fn_app_repo);
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_FILE_TO_APP,
+                                        "file", repo_file,
+                                        "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION |
+                                                        GS_PLUGIN_REFINE_FLAGS_REQUIRE_RUNTIME,
+                                        NULL);
+       g_object_unref (app_source);
+       app_source = gs_plugin_loader_job_process_app (plugin_loader, plugin_job, NULL, &error);
+       g_assert_no_error (error);
+       g_assert_cmpint (gs_app_get_state (app_source), ==, AS_APP_STATE_AVAILABLE);
+
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_INSTALL,
+                                        "app", app_source,
+                                        NULL);
+       ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error);
+       g_assert (ret);
+       g_assert_cmpint (gs_app_get_state (app_source), ==, AS_APP_STATE_INSTALLED);
+       gs_plugin_loader_clear_caches (plugin_loader);
+       gs_test_flush_main_context ();
+
+       /* install the app when it's already available in the repo */
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_FILE_TO_APP,
+                                        "file", file,
+                                        "refine-flags", GS_PLUGIN_REFINE_FLAGS_REQUIRE_VERSION |
+                                        GS_PLUGIN_REFINE_FLAGS_REQUIRE_RUNTIME,
+                                        NULL);
+       app = gs_plugin_loader_job_process_app (plugin_loader, plugin_job, NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_no_error (error);
+       g_assert (app != NULL);
+       g_assert_cmpint (gs_app_get_state (app), ==, AS_APP_STATE_AVAILABLE_LOCAL);
+
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_INSTALL,
+                                        "app", app,
+                                        NULL);
+       ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_no_error (error);
+       g_assert (ret);
+       g_assert_cmpint (gs_app_get_state (app), ==, AS_APP_STATE_INSTALLED);
+       g_assert (as_utils_unique_id_equal (gs_app_get_unique_id (app),
+                       "user/flatpak/test-app/desktop/org.test.Chiron.desktop/master"));
+
+       /* remove the app */
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_REMOVE,
+                                        "app", app,
+                                        NULL);
+       ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_no_error (error);
+       g_assert (ret);
+       g_assert_cmpint (gs_app_get_state (app), ==, AS_APP_STATE_AVAILABLE);
+
+       /* remove the repo */
+       g_object_unref (plugin_job);
+       plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_REMOVE,
+                                        "app", app_source,
+                                        NULL);
+       ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error);
+       gs_test_flush_main_context ();
+       g_assert_no_error (error);
+       g_assert (ret);
+       g_assert_cmpint (gs_app_get_state (app_source), ==, AS_APP_STATE_AVAILABLE);
 }
 
 static void


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