summaryrefslogtreecommitdiffstats
path: root/tests/test_manifest_xml.py
diff options
context:
space:
mode:
authorPeter Kjellerstedt <pkj@axis.com>2021-04-12 21:16:36 +0200
committerMike Frysinger <vapier@google.com>2021-05-26 13:36:20 +0000
commit5d58c18146e1cda1ab9a3593fd1324985bb5d638 (patch)
tree4aca227a0786ab20461857962bf48496986d0522 /tests/test_manifest_xml.py
parentd177609cb0283e41e23d4c19b94e17f42bdbdacb (diff)
downloadgit-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>
Diffstat (limited to 'tests/test_manifest_xml.py')
-rw-r--r--tests/test_manifest_xml.py47
1 files changed, 36 insertions, 11 deletions
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
17import os 17import os
18import platform 18import platform
19import re
19import shutil 20import shutil
20import tempfile 21import tempfile
21import unittest 22import 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
67def 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
66class ManifestParseTestCase(unittest.TestCase): 91class 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>')