Commit 00cf26f3 authored by Kalev Lember's avatar Kalev Lember

Move EVR parsing to gs-utils and add self tests

parent cb81a293
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2013-2017 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2015-2017 Kalev Lember <klember@redhat.com>
* Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com>
*
* Licensed under the GNU General Public License Version 2
*
......@@ -163,6 +163,78 @@ gs_utils_error_func (void)
g_assert_cmpstr (error->message, ==, "failed");
}
static void
gs_utils_parse_evr_func (void)
{
gboolean ret;
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("3.26.0-1.fc27", &epoch, &version, &release);
g_assert (ret);
g_assert_cmpstr (epoch, ==, "0");
g_assert_cmpstr (version, ==, "3.26.0");
g_assert_cmpstr (release, ==, "1.fc27");
}
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("1:3.26.0-1.fc27", &epoch, &version, &release);
g_assert (ret);
g_assert_cmpstr (epoch, ==, "1");
g_assert_cmpstr (version, ==, "3.26.0");
g_assert_cmpstr (release, ==, "1.fc27");
}
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("234", &epoch, &version, &release);
g_assert (ret);
g_assert_cmpstr (epoch, ==, "0");
g_assert_cmpstr (version, ==, "234");
g_assert_cmpstr (release, ==, "0");
}
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("3:1.6~git20131207+dfsg-2ubuntu1~14.04.3", &epoch, &version, &release);
g_assert (ret);
g_assert_cmpstr (epoch, ==, "3");
g_assert_cmpstr (version, ==, "1.6~git20131207+dfsg");
g_assert_cmpstr (release, ==, "2ubuntu1~14.04.3");
}
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("1-2-3-4-5-6", &epoch, &version, &release);
g_assert (!ret);
}
{
g_autofree gchar *epoch = NULL;
g_autofree gchar *version = NULL;
g_autofree gchar *release = NULL;
ret = gs_utils_parse_evr ("", &epoch, &version, &release);
g_assert (!ret);
}
}
static void
gs_plugin_download_rewrite_func (void)
{
......@@ -626,6 +698,7 @@ main (int argc, char **argv)
g_test_add_func ("/gnome-software/lib/utils{error}", gs_utils_error_func);
g_test_add_func ("/gnome-software/lib/utils{cache}", gs_utils_cache_func);
g_test_add_func ("/gnome-software/lib/utils{append-kv}", gs_utils_append_kv_func);
g_test_add_func ("/gnome-software/lib/utils{parse-evr}", gs_utils_parse_evr_func);
g_test_add_func ("/gnome-software/lib/os-release", gs_os_release_func);
g_test_add_func ("/gnome-software/lib/app", gs_app_func);
g_test_add_func ("/gnome-software/lib/app{addons}", gs_app_addons_func);
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2013-2017 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2015 Kalev Lember <klember@redhat.com>
* Copyright (C) 2015-2018 Kalev Lember <klember@redhat.com>
*
* Licensed under the GNU General Public License Version 2
*
......@@ -1041,4 +1041,66 @@ gs_utils_get_memory_total (void)
return si.totalram / MB_IN_BYTES / si.mem_unit;
}
/**
* gs_utils_parse_evr:
* @out_epoch: (out): return location for the epoch string
* @out_version: (out): return location for the version string
* @out_release: (out): return location for the release string
*
* Splits EVR into epoch-version-release strings.
*
* Returns: %TRUE for success
**/
gboolean
gs_utils_parse_evr (const gchar *evr,
gchar **out_epoch,
gchar **out_version,
gchar **out_release)
{
const gchar *version_release;
g_auto(GStrv) split_colon = NULL;
g_auto(GStrv) split_dash = NULL;
/* split on : to get epoch */
split_colon = g_strsplit (evr, ":", -1);
switch (g_strv_length (split_colon)) {
case 1:
/* epoch is 0 when not set */
*out_epoch = g_strdup ("0");
version_release = split_colon[0];
break;
case 2:
/* epoch set */
*out_epoch = g_strdup (split_colon[0]);
version_release = split_colon[1];
break;
default:
/* error */
return FALSE;
}
/* split on - to get version and release */
split_dash = g_strsplit (version_release, "-", -1);
switch (g_strv_length (split_dash)) {
case 1:
/* all of the string is version */
*out_version = g_strdup (split_dash[0]);
*out_release = g_strdup ("0");
break;
case 2:
/* both version and release set */
*out_version = g_strdup (split_dash[0]);
*out_release = g_strdup (split_dash[1]);
break;
default:
/* error */
return FALSE;
}
g_assert (*out_epoch != NULL);
g_assert (*out_version != NULL);
g_assert (*out_release != NULL);
return TRUE;
}
/* vim: set noexpandtab: */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2013-2016 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2018 Kalev Lember <klember@redhat.com>
*
* Licensed under the GNU General Public License Version 2
*
......@@ -93,6 +94,10 @@ void gs_utils_append_key_value (GString *str,
const gchar *key,
const gchar *value);
guint gs_utils_get_memory_total (void);
gboolean gs_utils_parse_evr (const gchar *evr,
gchar **out_epoch,
gchar **out_version,
gchar **out_release);
G_END_DECLS
......
......@@ -349,58 +349,6 @@ create_app_row (GsApp *app)
return row;
}
static gboolean
parse_evr (const gchar *evr,
gchar **out_epoch,
gchar **out_version,
gchar **out_release)
{
const gchar *version_release;
g_auto(GStrv) split_colon = NULL;
g_auto(GStrv) split_dash = NULL;
/* split on : to get epoch */
split_colon = g_strsplit (evr, ":", -1);
switch (g_strv_length (split_colon)) {
case 1:
/* epoch is 0 when not set */
*out_epoch = g_strdup ("0");
version_release = split_colon[0];
break;
case 2:
/* epoch set */
*out_epoch = g_strdup (split_colon[0]);
version_release = split_colon[1];
break;
default:
/* error */
return FALSE;
}
/* split on - to get version and release */
split_dash = g_strsplit (version_release, "-", -1);
switch (g_strv_length (split_dash)) {
case 1:
/* all of the string is version */
*out_version = g_strdup (split_dash[0]);
*out_release = g_strdup ("0");
break;
case 2:
/* both version and release set */
*out_version = g_strdup (split_dash[0]);
*out_release = g_strdup (split_dash[1]);
break;
default:
/* error */
return FALSE;
}
g_assert (*out_epoch != NULL);
g_assert (*out_version != NULL);
g_assert (*out_release != NULL);
return TRUE;
}
static gboolean
is_downgrade (const gchar *evr1,
const gchar *evr2)
......@@ -414,9 +362,9 @@ is_downgrade (const gchar *evr1,
g_autofree gchar *release2 = NULL;
/* split into epoch-version-release */
if (!parse_evr (evr1, &epoch1, &version1, &release1))
if (!gs_utils_parse_evr (evr1, &epoch1, &version1, &release1))
return FALSE;
if (!parse_evr (evr2, &epoch2, &version2, &release2))
if (!gs_utils_parse_evr (evr2, &epoch2, &version2, &release2))
return FALSE;
/* ignore epoch here as it's a way to make downgrades happen and not
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment