From 3473823d2d023ee4b3dd28b44e0a7c1d97bd85c4 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Wed, 10 Aug 2016 16:04:12 +0100 Subject: Ensure predictable output order of XML attributes --- libappstream-builder/asb-self-test.c | 42 +++++++------- libappstream-glib/as-node.c | 18 ++++++ libappstream-glib/as-self-test.c | 104 ++++++++++++++++++----------------- 3 files changed, 94 insertions(+), 70 deletions(-) diff --git a/libappstream-builder/asb-self-test.c b/libappstream-builder/asb-self-test.c index 24674cf..2d921b4 100644 --- a/libappstream-builder/asb-self-test.c +++ b/libappstream-builder/asb-self-test.c @@ -425,7 +425,7 @@ asb_test_context_test_func (AsbTestContextMode mode) /* check it matches what we expect */ xml = as_store_to_xml (store, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); expected_xml = - "\n" + "\n" #ifdef HAVE_FONTS "\n" "Liberation\n" @@ -435,7 +435,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "Liberation\n" "Open source versions of several commecial fonts\n" "

The Liberation Fonts are intended to be replacements for Times New Roman, Arial, and Courier New.

\n" - "LiberationSerif.png\n" + "LiberationSerif.png\n" "\n" "Addons\n" "Fonts\n" @@ -453,7 +453,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "\n" "\n" "\n" - "\n" + "\n" "\n" "\n" "en\n" @@ -477,7 +477,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "http://people.freedesktop.org/\n" "app.desktop\n" "\n" - "\n" + "\n" "\n" "\n" "org.freedesktop.AppStream\n" @@ -496,7 +496,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "App\n" "A test application\n" "

Long description goes here.

\n" - "app.png\n" + "app.png\n" "\n" "Profiling\n" "System\n" @@ -518,7 +518,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "\n" "\n" "\n" - "\n" + "\n" "\n" "\n" "org.freedesktop.AppStream\n" @@ -542,10 +542,10 @@ asb_test_context_test_func (AsbTestContextMode mode) "GPL-2.0+\n" "http://www.hughski.com/\n" "\n" - "\n" + "\n" "http://www.hughski.com/downloads/colorhug2/firmware/colorhug-2.0.2.cab\n" - "" AS_TEST_WILDCARD_SHA1 "\n" - "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" "

This unstable release adds the following features:

" "
  • Add TakeReadingArray to enable panel latency measurements
  • " "
  • Speed up the auto-scaled measurements considerably, using 256ms " @@ -582,7 +582,7 @@ asb_test_context_test_func (AsbTestContextMode mode) /* check output */ xml_failed = as_store_to_xml (store_failed, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); expected_xml = - "\n" + "\n" #ifdef HAVE_FONTS "\n" "LiberationSerif\n" @@ -590,7 +590,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "font\n" "Liberation Serif\n" "A Bold font from Liberation Serif\n" - "LiberationSerif.png\n" + "LiberationSerif.png\n" "\n" "Addons\n" "Fonts\n" @@ -612,7 +612,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "\n" "\n" "\n" - "\n" + "\n" "\n" "\n" "en\n" @@ -639,7 +639,7 @@ asb_test_context_test_func (AsbTestContextMode mode) "http://people.freedesktop.org/\n" "app.desktop\n" "\n" - "\n" + "\n" "\n" "\n" "org.freedesktop.AppStream\n" @@ -668,7 +668,7 @@ asb_test_context_test_func (AsbTestContextMode mode) /* check output */ xml_ignore = as_store_to_xml (store_ignore, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); expected_xml = - "\n" + "\n" "\n" "app-console.noarch\n" "app-console\n" @@ -847,7 +847,7 @@ asb_test_firmware_func (void) /* check it matches what we expect */ xml = as_store_to_xml (store, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); expected_xml = - "\n" + "\n" "\n" "com.hughski.ColorHug2.firmware\n" "ColorHug Firmware\n" @@ -858,10 +858,10 @@ asb_test_firmware_func (void) "GPL-2.0+\n" "http://www.hughski.com/\n" "\n" - "\n" + "\n" "http://www.hughski.com/downloads/colorhug2/firmware/colorhug-2.0.2.cab\n" - "" AS_TEST_WILDCARD_SHA1 "\n" - "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" "

    This unstable release adds the following features:

    " "
    • Add TakeReadingArray to enable panel latency measurements
    • " "
    • Speed up the auto-scaled measurements considerably, using 256ms " @@ -869,10 +869,10 @@ asb_test_firmware_func (void) "14\n" "2011\n" "\n" - "\n" + "\n" "http://www.hughski.com/downloads/colorhug2/firmware/colorhug-2.0.1.cab\n" - "" AS_TEST_WILDCARD_SHA1 "\n" - "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" "

      This unstable release adds the following features:

      " "
      • Use TakeReadings() to do a quick non-adaptive measurement
      • " "
      • Scale XYZ measurement with a constant factor to make the CCMX more " diff --git a/libappstream-glib/as-node.c b/libappstream-glib/as-node.c index 9d651d4..c54f92d 100644 --- a/libappstream-glib/as-node.c +++ b/libappstream-glib/as-node.c @@ -213,6 +213,21 @@ as_node_add_padding (GString *xml, guint depth) g_string_append (xml, " "); } +static gint +as_node_sort_attr_by_name_cb (gconstpointer a, gconstpointer b) +{ + AsNodeAttr *attr1 = (AsNodeAttr *) a; + AsNodeAttr *attr2 = (AsNodeAttr *) b; + + /* this is always first */ + if (g_strcmp0 (attr1->key, "type") == 0) + return -1; + if (g_strcmp0 (attr2->key, "type") == 0) + return 1; + + return g_strcmp0 (attr1->key, attr2->key); +} + static gchar * as_node_get_attr_string (AsNodeData *data) { @@ -220,6 +235,9 @@ as_node_get_attr_string (AsNodeData *data) GList *l; GString *str; + /* ensure predictable output order */ + data->attrs = g_list_sort (data->attrs, as_node_sort_attr_by_name_cb); + str = g_string_new (""); for (l = data->attrs; l != NULL; l = l->next) { attr = l->data; diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 02f215a..2c7497d 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -449,7 +449,7 @@ as_test_release_func (void) AsNode *n; AsNode *root; GString *xml; - const gchar *src = ""; + const gchar *src = ""; gboolean ret; g_autofree AsNodeContext *ctx = NULL; g_autoptr(AsRelease) release = NULL; @@ -572,11 +572,11 @@ as_test_release_appstream_func (void) gboolean ret; guint64 sz; const gchar *src = - "\n" + "\n" "http://foo.com/bar.zip\n" "http://baz.com/bar.cab\n" - "12345\n" - "deadbeef\n" + "12345\n" + "deadbeef\n" "

        This is a new release

        • Point
        \n" "

        Oprogramowanie

        \n" "123456\n" @@ -947,7 +947,7 @@ as_test_checksum_func (void) AsNode *n; AsNode *root; GString *xml; - const gchar *src = "12345"; + const gchar *src = "12345"; gboolean ret; g_autofree AsNodeContext *ctx = NULL; g_autoptr(AsChecksum) csum = NULL; @@ -1106,7 +1106,7 @@ as_test_image_func (void) AsNode *root; GString *xml; const gchar *src = - "" + "" "http://www.hughsie.com/a.jpg"; gboolean ret; g_autofree AsNodeContext *ctx = NULL; @@ -1181,7 +1181,7 @@ as_test_review_func (void) AsNode *root; GString *xml; const gchar *src = - "\n" + "\n" "5\n" "Hello world\n" "

        Mighty Fine

        \n" @@ -1243,7 +1243,7 @@ as_test_bundle_func (void) AsNode *root; GString *xml; const gchar *src = - "gnome-3-16"; + "gnome-3-16"; gboolean ret; g_autofree AsNodeContext *ctx = NULL; g_autoptr(AsBundle) bundle = NULL; @@ -1446,7 +1446,7 @@ as_test_app_func (void) GString *xml; gboolean ret; const gchar *src = - "\n" + "\n" "org.gnome.Software.desktop\n" "gnome-software\n" "gnome-software-src\n" @@ -1458,8 +1458,8 @@ as_test_app_func (void) "GNOME Foundation\n" "

        Software allows you to find stuff

        \n" "

        O aplicativo Software.

        \n" - "org.gnome.Software1.png\n" - "org.gnome.Software2.png\n" + "org.gnome.Software1.png\n" + "org.gnome.Software2.png\n" "\n" "System\n" "\n" @@ -1502,8 +1502,8 @@ as_test_app_func (void) "moderate\n" "\n" "\n" - "\n" - "\n" + "\n" + "\n" "\n" "\n" "/usr/bin/gnome-shell\n" @@ -2733,7 +2733,7 @@ as_test_store_embedded_func (void) g_autoptr(AsStore) store = NULL; g_autoptr(GString) xml = NULL; const gchar *xml_src = -"" +"" "" "eog.desktop" "eog" @@ -2807,7 +2807,9 @@ as_test_store_embedded_func (void) /* convert back to a file */ xml = as_store_to_xml (store, AS_NODE_TO_XML_FLAG_NONE); - g_assert_cmpstr (xml->str, ==, xml_src); + ret = as_test_compare_lines (xml->str, xml_src, &error); + g_assert_no_error (error); + g_assert (ret); /* strip out the embedded icons */ ret = as_store_convert_icons (store, AS_ICON_KIND_CACHED, &error); @@ -3282,7 +3284,7 @@ as_test_store_cab_func (void) /* check output */ src = - "\n" + "\n" "\n" "com.hughski.ColorHug2.firmware\n" "ColorHug Firmware\n" @@ -3293,10 +3295,10 @@ as_test_store_cab_func (void) "GPL-2.0+\n" "http://www.hughski.com/\n" "\n" - "\n" + "\n" "http://www.hughski.com/downloads/colorhug2/firmware/colorhug-2.0.2.cab\n" - "" AS_TEST_WILDCARD_SHA1 "\n" - "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" + "" AS_TEST_WILDCARD_SHA1 "\n" "

        This unstable release adds the following features:

        " "
        • Add TakeReadingArray to enable panel latency measurements
        • " "
        • Speed up the auto-scaled measurements considerably, using 256ms as" @@ -3519,7 +3521,7 @@ as_test_store_versions_func (void) AsStore *store; GError *error = NULL; gboolean ret; - GString *str; + GString *xml; /* load a file to the store */ store = as_store_new (); @@ -3549,39 +3551,43 @@ as_test_store_versions_func (void) as_store_set_api_version (store, 0.6); g_assert_cmpfloat (as_store_get_api_version (store), <, 0.6 + 0.01); g_assert_cmpfloat (as_store_get_api_version (store), >, 0.6 - 0.01); - str = as_store_to_xml (store, 0); - g_assert_cmpstr (str->str, ==, - "" - "" - "test.desktop" - "

          Hello world

          " - "i386" - "" - "" - "

          Hello

          " - "
          " - "
          " - "
          " - "
          "); - g_string_free (str, TRUE); + xml = as_store_to_xml (store, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); + ret = as_test_compare_lines (xml->str, + "\n" + "\n" + "test.desktop\n" + "

          Hello world

          \n" + "\n" + "i386\n" + "\n" + "\n" + "\n" + "

          Hello

          \n" + "
          \n" + "
          \n" + "
          \n" + "
          \n", &error); + g_assert_no_error (error); + g_assert (ret); + g_string_free (xml, TRUE); /* test with legacy options */ as_store_set_api_version (store, 0.6); - str = as_store_to_xml (store, 0); - g_assert_cmpstr (str->str, ==, + xml = as_store_to_xml (store, 0); + g_assert_cmpstr (xml->str, ==, "" "" "test.desktop" "

          Hello world

          " "i386" "" - "" + "" "

          Hello

          " "
          " "
          " "
          " "
          "); - g_string_free (str, TRUE); + g_string_free (xml, TRUE); g_object_unref (store); @@ -3597,14 +3603,14 @@ as_test_store_versions_func (void) g_assert (ret); /* test latest spec version */ - str = as_store_to_xml (store, 0); - g_assert_cmpstr (str->str, ==, + xml = as_store_to_xml (store, 0); + g_assert_cmpstr (xml->str, ==, "" "" "test.desktop" "" ""); - g_string_free (str, TRUE); + g_string_free (xml, TRUE); g_object_unref (store); } @@ -3658,9 +3664,9 @@ as_test_store_addons_func (void) /* check it marshals back to the same XML */ str = as_store_to_xml (store, 0); - if (g_strcmp0 (str->str, xml) != 0) - g_warning ("Expected:\n%s\nGot:\n%s", xml, str->str); - g_assert_cmpstr (str->str, ==, xml); + ret = as_test_compare_lines (str->str, xml, &error); + g_assert_no_error (error); + g_assert (ret); } /* @@ -4446,7 +4452,7 @@ as_test_store_yaml_func (void) g_autoptr(GFile) file = NULL; g_autoptr(GString) str = NULL; const gchar *xml = - "\n" + "\n" "\n" "dave.desktop\n" "dave\n" @@ -4479,9 +4485,9 @@ as_test_store_yaml_func (void) /* test it matches expected XML */ str = as_store_to_xml (store, AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE); - if (g_strcmp0 (str->str, xml) != 0) - g_warning ("Expected:\n%s\nGot:\n%s", xml, str->str); - g_assert_cmpstr (str->str, ==, xml); + ret = as_test_compare_lines (str->str, xml, &error); + g_assert_no_error (error); + g_assert (ret); /* test store properties */ g_assert_cmpstr (as_store_get_origin (store), ==, "aequorea"); -- cgit v1.2.1