diff --git a/compose/asc-globals.c b/compose/asc-globals.c index 938e27ff..4330bd6f 100644 --- a/compose/asc-globals.c +++ b/compose/asc-globals.c @@ -190,6 +190,11 @@ void asc_globals_set_use_optipng (gboolean enabled) { AscGlobalsPrivate *priv = asc_globals_get_priv (); + if (enabled && priv->optipng_bin == NULL) { + g_critical ("Refusing to enable optipng: not found in $PATH"); + priv->use_optipng = FALSE; + return; + } priv->use_optipng = enabled; } @@ -216,6 +221,8 @@ asc_globals_set_optipng_binary (const gchar *path) AscGlobalsPrivate *priv = asc_globals_get_priv (); g_free (priv->optipng_bin); priv->optipng_bin = g_strdup (path); + if (priv->optipng_bin == NULL) + priv->use_optipng = FALSE; } /** diff --git a/compose/asc-image.c b/compose/asc-image.c index 9102fd8d..5d657ef6 100644 --- a/compose/asc-image.c +++ b/compose/asc-image.c @@ -204,6 +204,7 @@ asc_optimize_png (const gchar *fname, GError **error) { gint exit_status; gboolean r; + const gchar *optipng_path; g_autofree gchar *opng_stdout = NULL; g_autofree gchar *opng_stderr = NULL; g_autofree const gchar **argv = NULL; @@ -212,8 +213,17 @@ asc_optimize_png (const gchar *fname, GError **error) if (!asc_globals_get_use_optipng ()) return TRUE; + optipng_path = asc_globals_get_optipng_binary (); + if (optipng_path == NULL) { + g_set_error (error, + ASC_IMAGE_ERROR, + ASC_IMAGE_ERROR_FAILED, + "optipng not found in $PATH"); + return FALSE; + } + argv = g_new0 (const gchar*, 2 + 1); - argv[0] = asc_globals_get_optipng_binary (); + argv[0] = optipng_path; argv[1] = fname; /* NOTE: Maybe add an option to run optipng with stronger optimization? (>= -o4) */ diff --git a/tests/test-compose.c b/tests/test-compose.c index 31334ea0..f1afb4e2 100644 --- a/tests/test-compose.c +++ b/tests/test-compose.c @@ -27,6 +27,11 @@ static gchar *datadir = NULL; +typedef struct +{ + gchar *path; +} Fixture; + /** * test_utils: * @@ -493,6 +498,32 @@ test_compose_desktop_entry () g_clear_pointer (&cpt, g_object_unref); } +static void +setup (Fixture *fixture, gconstpointer user_data) +{ + fixture->path = g_strdup (g_getenv ("PATH")); + /* not unset because glib has a hardcoded fallback */ + g_setenv ("PATH", "", TRUE); +} + +static void +teardown (Fixture *fixture, gconstpointer user_data) +{ + g_setenv ("PATH", fixture->path, TRUE); + g_clear_pointer (&fixture->path, g_free); +} + +static void +test_compose_optipng_not_found (Fixture *fixture, gconstpointer user_data) +{ + g_test_expect_message (G_LOG_DOMAIN, + G_LOG_LEVEL_CRITICAL, + "*Refusing to enable optipng: not found in $PATH"); + asc_globals_set_use_optipng (TRUE); + g_assert_false (asc_globals_get_use_optipng ()); + g_test_assert_expected_messages (); +} + int main (int argc, char **argv) { @@ -513,6 +544,7 @@ main (int argc, char **argv) /* only critical and error are fatal */ g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); + g_test_add ("/AppStream/Compose/OptipngNotfound", Fixture, NULL, setup, test_compose_optipng_not_found, teardown); g_test_add_func ("/AppStream/Compose/Utils", test_utils); g_test_add_func ("/AppStream/Compose/FontInfo", test_read_fontinfo); g_test_add_func ("/AppStream/Compose/Image", test_image_transform);