gplugin/gplugin

Parents 9096f0f5f434
Children dca3194601e5
Add convenience functions gplugin_manager_append_paths_from_environment and gplugin_manager_prepend_paths_from_environment

Testing Done:
Wrote a test that passes.

Reviewed at https://reviews.imfreedom.org/r/997/
--- a/gplugin/gplugin-manager.c Thu Oct 14 23:11:27 2021 -0500
+++ b/gplugin/gplugin-manager.c Sat Oct 23 02:03:59 2021 -0500
@@ -125,6 +125,36 @@
}
static void
+gplugin_manager_change_paths_from_environment(
+ GPluginManager *manager,
+ const gchar *name,
+ gboolean prepend)
+{
+ gchar **paths;
+ gint i;
+ const gchar *from_env;
+ void (*changer)(GPluginManager *, const gchar *);
+
+ from_env = g_getenv(name);
+ if(from_env == NULL) {
+ return;
+ }
+
+ paths = g_strsplit(from_env, G_SEARCHPATH_SEPARATOR_S, 0);
+ if(prepend) {
+ changer = &gplugin_manager_prepend_path;
+ } else {
+ changer = &gplugin_manager_append_path;
+ }
+
+ for(i = 0; paths[i]; i++) {
+ (*changer)(manager, paths[i]);
+ }
+
+ g_strfreev(paths);
+}
+
+static void
gplugin_manager_foreach_unload_plugin(
gpointer key,
gpointer value,
@@ -685,6 +715,40 @@
}
/**
+ * gplugin_manager_append_paths_from_environment:
+ * @manager: The manager instance.
+ * @name: The name of the environment variable containing the paths to add.
+ *
+ * Append the paths held in the environment variable @name to the list.
+ *
+ * Since: 0.37.0
+ */
+void
+gplugin_manager_append_paths_from_environment(
+ GPluginManager *manager,
+ const gchar *name)
+{
+ gplugin_manager_change_paths_from_environment(manager, name, FALSE);
+}
+
+/**
+ * gplugin_manager_prepend_paths_from_environment:
+ * @manager: The manager instance.
+ * @name: The name of the environment variable containing the paths to add.
+ *
+ * Prepends the paths held in the environment variable @name to the list.
+ *
+ * Since: 0.37.0
+ */
+void
+gplugin_manager_prepend_paths_from_environment(
+ GPluginManager *manager,
+ const gchar *name)
+{
+ gplugin_manager_change_paths_from_environment(manager, name, TRUE);
+}
+
+/**
* gplugin_manager_get_paths:
* @manager: The manager instance.
*
--- a/gplugin/gplugin-manager.h Thu Oct 14 23:11:27 2021 -0500
+++ b/gplugin/gplugin-manager.h Sat Oct 23 02:03:59 2021 -0500
@@ -30,12 +30,7 @@
G_BEGIN_DECLS
#define GPLUGIN_TYPE_MANAGER (gplugin_manager_get_type())
-G_DECLARE_FINAL_TYPE(
- GPluginManager,
- gplugin_manager,
- GPLUGIN,
- MANAGER,
- GObject)
+G_DECLARE_FINAL_TYPE(GPluginManager, gplugin_manager, GPLUGIN, MANAGER, GObject)
typedef void (*GPluginManagerForeachFunc)(
const gchar *id,
@@ -52,6 +47,12 @@
GPluginManager *manager,
const gchar *prefix,
const gchar *appname);
+void gplugin_manager_append_paths_from_environment(
+ GPluginManager *manager,
+ const gchar *name);
+void gplugin_manager_prepend_paths_from_environment(
+ GPluginManager *manager,
+ const gchar *name);
GList *gplugin_manager_get_paths(GPluginManager *manager);
--- a/gplugin/tests/test-plugin-manager-paths.c Thu Oct 14 23:11:27 2021 -0500
+++ b/gplugin/tests/test-plugin-manager-paths.c Sat Oct 23 02:03:59 2021 -0500
@@ -221,6 +221,72 @@
g_assert_cmpuint(0, ==, size);
}
+static void
+test_gplugin_manager_add_app_paths_via_environ(void)
+{
+ GList *paths = NULL, *l = NULL;
+ gint path_pos = 0;
+ gchar *pre_paths, *post_paths;
+ gint found_paths = 0;
+ gint path_count;
+ const gchar *pre_env_var_name = "GPLUGIN_EXTRA_TEST_PREPATHS";
+ const gchar *post_env_var_name = "GPLUGIN_EXTRA_TEST_POSTPATHS";
+
+ GPluginManager *manager = gplugin_manager_get_default();
+
+ /* get number of paths to start */
+ paths = gplugin_manager_get_paths(manager);
+ path_count = g_list_length(paths);
+
+ /* build contents of environment vars */
+ pre_paths = g_strdup_printf(
+ "%s%s%s",
+ "/prefoo",
+ G_SEARCHPATH_SEPARATOR_S,
+ "/prebar/");
+ post_paths = g_strdup_printf(
+ "%s%s%s",
+ "/postfoo",
+ G_SEARCHPATH_SEPARATOR_S,
+ "/postbar/");
+
+ /* put /envfoo and /envbar as paths in an environment variable */
+ g_setenv(pre_env_var_name, pre_paths, TRUE);
+ g_setenv(post_env_var_name, post_paths, TRUE);
+
+ /* have the manager add them */
+ gplugin_manager_prepend_paths_from_environment(manager, pre_env_var_name);
+ gplugin_manager_append_paths_from_environment(manager, post_env_var_name);
+
+ /* now get the manager's current paths */
+ paths = gplugin_manager_get_paths(manager);
+
+ /* four more paths now */
+ test_path_count(manager, path_count + 4);
+
+ /* check order is correct */
+ path_pos = 0;
+ for(l = paths; l != NULL; l = l->next) {
+ path_pos += 1;
+ if(g_strcmp0(l->data, "/prefoo/") == 0) {
+ g_assert_cmpint(2, >=, path_pos);
+ found_paths += 1;
+ } else if(g_strcmp0(l->data, "/prebar/") == 0) {
+ g_assert_cmpint(2, >=, path_pos);
+ found_paths += 1;
+ } else if(g_strcmp0(l->data, "/postfoo/") == 0) {
+ g_assert_cmpint(2, <, path_pos);
+ found_paths += 1;
+ } else if(g_strcmp0(l->data, "/postbar/") == 0) {
+ g_assert_cmpint(2, <, path_pos);
+ found_paths += 1;
+ }
+ }
+ g_assert_cmpint(4, ==, found_paths);
+ g_free(pre_paths);
+ g_free(post_paths);
+}
+
gint
main(gint argc, gchar **argv)
{
@@ -256,9 +322,14 @@
g_test_add_func(
"/plugins/paths/add_default_paths",
test_gplugin_manager_add_default_paths);
+
g_test_add_func(
"/plugins/paths/add_app_paths",
test_gplugin_manager_add_app_paths);
+ g_test_add_func(
+ "/plugins/paths/add_app_paths_via_environ",
+ test_gplugin_manager_add_app_paths_via_environ);
+
return g_test_run();
}