Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Check optipng is there before we use it #321

Merged
merged 1 commit into from Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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;
}

/**
@@ -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) {
ximion marked this conversation as resolved.
Show resolved Hide resolved
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) */
@@ -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");
ximion marked this conversation as resolved.
Show resolved Hide resolved
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);