diff options
author | Peter Kjellerstedt <pkj@axis.com> | 2021-04-12 21:16:36 +0200 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2021-05-26 13:36:20 +0000 |
commit | 5d58c18146e1cda1ab9a3593fd1324985bb5d638 (patch) | |
tree | 4aca227a0786ab20461857962bf48496986d0522 | |
parent | d177609cb0283e41e23d4c19b94e17f42bdbdacb (diff) | |
download | git-repo-5d58c18146e1cda1ab9a3593fd1324985bb5d638.tar.gz |
tests: Make the tests pass for Python < 3.8
Before Python 3.8, xml.dom.minidom sorted the attributes of an element
when writing it to a file, while later versions output the attributes
in the order they were created. Avoid these differences by sorting the
attributes for each element before comparing the generated manifests
with the expected ones.
Bug: https://crbug.com/gerrit/14382
Change-Id: Ie2597727afcc48f9063a7261ad970e8a549f0587
Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/303326
Reviewed-by: Mike Frysinger <vapier@google.com>
-rw-r--r-- | tests/test_git_superproject.py | 36 | ||||
-rw-r--r-- | tests/test_manifest_xml.py | 47 |
2 files changed, 55 insertions, 28 deletions
diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index 5c1455f5..b1ae3576 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py | |||
@@ -23,6 +23,7 @@ from unittest import mock | |||
23 | import git_superproject | 23 | import git_superproject |
24 | import manifest_xml | 24 | import manifest_xml |
25 | import platform_utils | 25 | import platform_utils |
26 | from test_manifest_xml import sort_attributes | ||
26 | 27 | ||
27 | 28 | ||
28 | class SuperprojectTestCase(unittest.TestCase): | 29 | class SuperprojectTestCase(unittest.TestCase): |
@@ -140,12 +141,12 @@ class SuperprojectTestCase(unittest.TestCase): | |||
140 | with open(manifest_path, 'r') as fp: | 141 | with open(manifest_path, 'r') as fp: |
141 | manifest_xml = fp.read() | 142 | manifest_xml = fp.read() |
142 | self.assertEqual( | 143 | self.assertEqual( |
143 | manifest_xml, | 144 | sort_attributes(manifest_xml), |
144 | '<?xml version="1.0" ?><manifest>' | 145 | '<?xml version="1.0" ?><manifest>' |
145 | '<remote name="default-remote" fetch="http://localhost"/>' | 146 | '<remote fetch="http://localhost" name="default-remote"/>' |
146 | '<default remote="default-remote" revision="refs/heads/main"/>' | 147 | '<default remote="default-remote" revision="refs/heads/main"/>' |
147 | '<project name="platform/art" path="art" revision="ABCDEF" ' | 148 | '<project groups="notdefault,platform-' + self.platform + '" ' |
148 | 'groups="notdefault,platform-' + self.platform + '"/>' | 149 | 'name="platform/art" path="art" revision="ABCDEF"/>' |
149 | '<superproject name="superproject"/>' | 150 | '<superproject name="superproject"/>' |
150 | '</manifest>') | 151 | '</manifest>') |
151 | 152 | ||
@@ -167,13 +168,13 @@ class SuperprojectTestCase(unittest.TestCase): | |||
167 | with open(manifest_path, 'r') as fp: | 168 | with open(manifest_path, 'r') as fp: |
168 | manifest_xml = fp.read() | 169 | manifest_xml = fp.read() |
169 | self.assertEqual( | 170 | self.assertEqual( |
170 | manifest_xml, | 171 | sort_attributes(manifest_xml), |
171 | '<?xml version="1.0" ?><manifest>' | 172 | '<?xml version="1.0" ?><manifest>' |
172 | '<remote name="default-remote" fetch="http://localhost"/>' | 173 | '<remote fetch="http://localhost" name="default-remote"/>' |
173 | '<default remote="default-remote" revision="refs/heads/main"/>' | 174 | '<default remote="default-remote" revision="refs/heads/main"/>' |
174 | '<project name="platform/art" path="art" ' | 175 | '<project groups="notdefault,platform-' + self.platform + '" ' |
175 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" ' | 176 | 'name="platform/art" path="art" ' |
176 | 'groups="notdefault,platform-' + self.platform + '"/>' | 177 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>' |
177 | '<superproject name="superproject"/>' | 178 | '<superproject name="superproject"/>' |
178 | '</manifest>') | 179 | '</manifest>') |
179 | 180 | ||
@@ -208,16 +209,17 @@ class SuperprojectTestCase(unittest.TestCase): | |||
208 | with open(manifest_path, 'r') as fp: | 209 | with open(manifest_path, 'r') as fp: |
209 | manifest_xml = fp.read() | 210 | manifest_xml = fp.read() |
210 | self.assertEqual( | 211 | self.assertEqual( |
211 | manifest_xml, | 212 | sort_attributes(manifest_xml), |
212 | '<?xml version="1.0" ?><manifest>' | 213 | '<?xml version="1.0" ?><manifest>' |
213 | '<remote name="default-remote" fetch="http://localhost"/>' | 214 | '<remote fetch="http://localhost" name="default-remote"/>' |
214 | '<remote name="goog" fetch="http://localhost2"/>' | 215 | '<remote fetch="http://localhost2" name="goog"/>' |
215 | '<default remote="default-remote" revision="refs/heads/main"/>' | 216 | '<default remote="default-remote" revision="refs/heads/main"/>' |
216 | '<project name="platform/art" path="art" ' | 217 | '<project groups="notdefault,platform-' + self.platform + '" ' |
217 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea" ' | 218 | 'name="platform/art" path="art" ' |
218 | 'groups="notdefault,platform-' + self.platform + '"/>' | 219 | 'revision="2c2724cb36cd5a9cec6c852c681efc3b7c6b86ea"/>' |
219 | '<project name="platform/vendor/x" path="vendor/x" remote="goog" ' | 220 | '<project clone-depth="1" groups="vendor" ' |
220 | 'revision="master-with-vendor" groups="vendor" clone-depth="1"/>' | 221 | 'name="platform/vendor/x" path="vendor/x" remote="goog" ' |
222 | 'revision="master-with-vendor"/>' | ||
221 | '<superproject name="superproject"/>' | 223 | '<superproject name="superproject"/>' |
222 | '</manifest>') | 224 | '</manifest>') |
223 | 225 | ||
diff --git a/tests/test_manifest_xml.py b/tests/test_manifest_xml.py index 99848e57..bd74780d 100644 --- a/tests/test_manifest_xml.py +++ b/tests/test_manifest_xml.py | |||
@@ -16,6 +16,7 @@ | |||
16 | 16 | ||
17 | import os | 17 | import os |
18 | import platform | 18 | import platform |
19 | import re | ||
19 | import shutil | 20 | import shutil |
20 | import tempfile | 21 | import tempfile |
21 | import unittest | 22 | import unittest |
@@ -63,6 +64,30 @@ if os.path.sep != '/': | |||
63 | INVALID_FS_PATHS += tuple(x.replace('/', os.path.sep) for x in INVALID_FS_PATHS) | 64 | INVALID_FS_PATHS += tuple(x.replace('/', os.path.sep) for x in INVALID_FS_PATHS) |
64 | 65 | ||
65 | 66 | ||
67 | def sort_attributes(manifest): | ||
68 | """Sort the attributes of all elements alphabetically. | ||
69 | |||
70 | This is needed because different versions of the toxml() function from | ||
71 | xml.dom.minidom outputs the attributes of elements in different orders. | ||
72 | Before Python 3.8 they were output alphabetically, later versions preserve | ||
73 | the order specified by the user. | ||
74 | |||
75 | Args: | ||
76 | manifest: String containing an XML manifest. | ||
77 | |||
78 | Returns: | ||
79 | The XML manifest with the attributes of all elements sorted alphabetically. | ||
80 | """ | ||
81 | new_manifest = '' | ||
82 | # This will find every element in the XML manifest, whether they have | ||
83 | # attributes or not. This simplifies recreating the manifest below. | ||
84 | matches = re.findall(r'(<[/?]?[a-z-]+\s*)((?:\S+?="[^"]+"\s*?)*)(\s*[/?]?>)', manifest) | ||
85 | for head, attrs, tail in matches: | ||
86 | m = re.findall(r'\S+?="[^"]+"', attrs) | ||
87 | new_manifest += head + ' '.join(sorted(m)) + tail | ||
88 | return new_manifest | ||
89 | |||
90 | |||
66 | class ManifestParseTestCase(unittest.TestCase): | 91 | class ManifestParseTestCase(unittest.TestCase): |
67 | """TestCase for parsing manifests.""" | 92 | """TestCase for parsing manifests.""" |
68 | 93 | ||
@@ -254,9 +279,9 @@ class XmlManifestTests(ManifestParseTestCase): | |||
254 | self.assertEqual(manifest.superproject['name'], 'superproject') | 279 | self.assertEqual(manifest.superproject['name'], 'superproject') |
255 | self.assertEqual(manifest.superproject['remote'].name, 'test-remote') | 280 | self.assertEqual(manifest.superproject['remote'].name, 'test-remote') |
256 | self.assertEqual( | 281 | self.assertEqual( |
257 | manifest.ToXml().toxml(), | 282 | sort_attributes(manifest.ToXml().toxml()), |
258 | '<?xml version="1.0" ?><manifest>' | 283 | '<?xml version="1.0" ?><manifest>' |
259 | '<remote name="test-remote" fetch="http://localhost"/>' | 284 | '<remote fetch="http://localhost" name="test-remote"/>' |
260 | '<default remote="test-remote" revision="refs/heads/main"/>' | 285 | '<default remote="test-remote" revision="refs/heads/main"/>' |
261 | '<superproject name="superproject"/>' | 286 | '<superproject name="superproject"/>' |
262 | '</manifest>') | 287 | '</manifest>') |
@@ -408,9 +433,9 @@ class ProjectElementTests(ManifestParseTestCase): | |||
408 | project = manifest.projects[0] | 433 | project = manifest.projects[0] |
409 | project.SetRevisionId('ABCDEF') | 434 | project.SetRevisionId('ABCDEF') |
410 | self.assertEqual( | 435 | self.assertEqual( |
411 | manifest.ToXml().toxml(), | 436 | sort_attributes(manifest.ToXml().toxml()), |
412 | '<?xml version="1.0" ?><manifest>' | 437 | '<?xml version="1.0" ?><manifest>' |
413 | '<remote name="default-remote" fetch="http://localhost"/>' | 438 | '<remote fetch="http://localhost" name="default-remote"/>' |
414 | '<default remote="default-remote" revision="refs/heads/main"/>' | 439 | '<default remote="default-remote" revision="refs/heads/main"/>' |
415 | '<project name="test-name" revision="ABCDEF"/>' | 440 | '<project name="test-name" revision="ABCDEF"/>' |
416 | '</manifest>') | 441 | '</manifest>') |
@@ -516,9 +541,9 @@ class SuperProjectElementTests(ManifestParseTestCase): | |||
516 | self.assertEqual(manifest.superproject['remote'].name, 'test-remote') | 541 | self.assertEqual(manifest.superproject['remote'].name, 'test-remote') |
517 | self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject') | 542 | self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/superproject') |
518 | self.assertEqual( | 543 | self.assertEqual( |
519 | manifest.ToXml().toxml(), | 544 | sort_attributes(manifest.ToXml().toxml()), |
520 | '<?xml version="1.0" ?><manifest>' | 545 | '<?xml version="1.0" ?><manifest>' |
521 | '<remote name="test-remote" fetch="http://localhost"/>' | 546 | '<remote fetch="http://localhost" name="test-remote"/>' |
522 | '<default remote="test-remote" revision="refs/heads/main"/>' | 547 | '<default remote="test-remote" revision="refs/heads/main"/>' |
523 | '<superproject name="superproject"/>' | 548 | '<superproject name="superproject"/>' |
524 | '</manifest>') | 549 | '</manifest>') |
@@ -537,10 +562,10 @@ class SuperProjectElementTests(ManifestParseTestCase): | |||
537 | self.assertEqual(manifest.superproject['remote'].name, 'superproject-remote') | 562 | self.assertEqual(manifest.superproject['remote'].name, 'superproject-remote') |
538 | self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/platform/superproject') | 563 | self.assertEqual(manifest.superproject['remote'].url, 'http://localhost/platform/superproject') |
539 | self.assertEqual( | 564 | self.assertEqual( |
540 | manifest.ToXml().toxml(), | 565 | sort_attributes(manifest.ToXml().toxml()), |
541 | '<?xml version="1.0" ?><manifest>' | 566 | '<?xml version="1.0" ?><manifest>' |
542 | '<remote name="default-remote" fetch="http://localhost"/>' | 567 | '<remote fetch="http://localhost" name="default-remote"/>' |
543 | '<remote name="superproject-remote" fetch="http://localhost"/>' | 568 | '<remote fetch="http://localhost" name="superproject-remote"/>' |
544 | '<default remote="default-remote" revision="refs/heads/main"/>' | 569 | '<default remote="default-remote" revision="refs/heads/main"/>' |
545 | '<superproject name="platform/superproject" remote="superproject-remote"/>' | 570 | '<superproject name="platform/superproject" remote="superproject-remote"/>' |
546 | '</manifest>') | 571 | '</manifest>') |
@@ -557,9 +582,9 @@ class SuperProjectElementTests(ManifestParseTestCase): | |||
557 | self.assertEqual(manifest.superproject['name'], 'superproject') | 582 | self.assertEqual(manifest.superproject['name'], 'superproject') |
558 | self.assertEqual(manifest.superproject['remote'].name, 'default-remote') | 583 | self.assertEqual(manifest.superproject['remote'].name, 'default-remote') |
559 | self.assertEqual( | 584 | self.assertEqual( |
560 | manifest.ToXml().toxml(), | 585 | sort_attributes(manifest.ToXml().toxml()), |
561 | '<?xml version="1.0" ?><manifest>' | 586 | '<?xml version="1.0" ?><manifest>' |
562 | '<remote name="default-remote" fetch="http://localhost"/>' | 587 | '<remote fetch="http://localhost" name="default-remote"/>' |
563 | '<default remote="default-remote" revision="refs/heads/main"/>' | 588 | '<default remote="default-remote" revision="refs/heads/main"/>' |
564 | '<superproject name="superproject"/>' | 589 | '<superproject name="superproject"/>' |
565 | '</manifest>') | 590 | '</manifest>') |