diff options
author | Ken Dreyer <ktdreyer@ktdreyer.com> | 2020-02-06 09:45:40 -0700 |
---|---|---|
committer | Ken Dreyer <ktdreyer@ktdreyer.com> | 2020-02-06 09:45:40 -0700 |
commit | 4096dbdd1bf5c64776fe1ee7446f2d45019296e1 (patch) | |
tree | 8a85cd806375aa0977cc4d8ee6301721bd6aa53d | |
parent | 29b0e366ab1360683512bc295b292ad6fe250b33 (diff) | |
download | python-jenkins-job-builder-4096dbdd1bf5c64776fe1ee7446f2d45019296e1.tar.gz python-jenkins-job-builder-4096dbdd1bf5c64776fe1ee7446f2d45019296e1.tar.xz python-jenkins-job-builder-4096dbdd1bf5c64776fe1ee7446f2d45019296e1.zip |
conditional-publisher: sort publisher attributes alphabetically
As described in dc83d5161fd58c22018c0355282f98c9c2369035, Python 3.8's
ElementTree now writes out attributes in insertion order. On older
Python versions, ElementTree writes attributes in alphabetical order
instead.
The conditional_publisher() method takes an existing publisher XML
node and alters it by changing the tag name to "publisher" and adding a
new "class" attribute with the original tag name. ElementTree on py38
preserves the insertion order of the "class" attribute, whereas py37 and
earlier would have sorted "class" ahead of the other attributes.
This leads to a unit test failure on Python 3.8 in
publishers/fixtures/conditional-publisher002.yaml. The conditional
"copy-to-master" publisher has a "plugin" attribute in the XML. On py37,
the "class" attribute sorted before the "plugin" attribute
alphabetically, but on py38+, "plugin" was inserted before "class", so
the order is effectively reversed.
To resolve the test failure, sort the attributes alphabetically when we
modify edited_node. This makes py38 write the edited_node attributes in
the same order as older Pythons. With this change, the test suite passes
when comparing the JJB output with the static XML fixture file.
Change-Id: Ib727365e101507d9ab69a426bb0bda89a402fb08
-rwxr-xr-x | jenkins_jobs/modules/publishers.py | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index f2a8ef5b..b424b9ce 100755 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -6547,6 +6547,12 @@ def conditional_publisher(registry, xml_parent, data): for edited_node in create_publishers(registry, action): if not use_publisher_list: edited_node.set("class", edited_node.tag) + # sort attributes alphabetically + attrib = edited_node.attrib + if len(attrib) > 1: + attribs = sorted(attrib.items()) + attrib.clear() + attrib.update(attribs) edited_node.tag = "publisher" parent.append(edited_node) |