test_unify.py (8692B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import os 6 import sys 7 from io import StringIO 8 9 import mozunit 10 11 from mozbuild.dirutils import ensureParentDir 12 from mozpack.errors import AccumulatedErrors, ErrorMessage, errors 13 from mozpack.files import FileFinder 14 from mozpack.mozjar import JarWriter 15 from mozpack.test.test_files import MockDest, TestWithTmpDir 16 from mozpack.unify import UnifiedBuildFinder, UnifiedFinder 17 18 19 class TestUnified(TestWithTmpDir): 20 def create_one(self, which, path, content): 21 file = self.tmppath(os.path.join(which, path)) 22 ensureParentDir(file) 23 if isinstance(content, str): 24 content = content.encode("utf-8") 25 open(file, "wb").write(content) 26 27 def create_both(self, path, content): 28 for p in ["a", "b"]: 29 self.create_one(p, path, content) 30 31 32 class TestUnifiedFinder(TestUnified): 33 def test_unified_finder(self): 34 self.create_both("foo/bar", "foobar") 35 self.create_both("foo/baz", "foobaz") 36 self.create_one("a", "bar", "bar") 37 self.create_one("b", "baz", "baz") 38 self.create_one("a", "qux", "foobar") 39 self.create_one("b", "qux", "baz") 40 self.create_one("a", "test/foo", "a\nb\nc\n") 41 self.create_one("b", "test/foo", "b\nc\na\n") 42 self.create_both("test/bar", "a\nb\nc\n") 43 44 finder = UnifiedFinder( 45 FileFinder(self.tmppath("a")), 46 FileFinder(self.tmppath("b")), 47 sorted=["test"], 48 ) 49 self.assertEqual( 50 sorted([ 51 (f, c.open().read().decode("utf-8")) for f, c in finder.find("foo") 52 ]), 53 [("foo/bar", "foobar"), ("foo/baz", "foobaz")], 54 ) 55 self.assertRaises(ErrorMessage, any, finder.find("bar")) 56 self.assertRaises(ErrorMessage, any, finder.find("baz")) 57 self.assertRaises(ErrorMessage, any, finder.find("qux")) 58 self.assertEqual( 59 sorted([ 60 (f, c.open().read().decode("utf-8")) for f, c in finder.find("test") 61 ]), 62 [("test/bar", "a\nb\nc\n"), ("test/foo", "a\nb\nc\n")], 63 ) 64 65 66 class TestUnifiedBuildFinder(TestUnified): 67 def test_unified_build_finder(self): 68 finder = UnifiedBuildFinder( 69 FileFinder(self.tmppath("a")), FileFinder(self.tmppath("b")) 70 ) 71 72 # Test chrome.manifest unification 73 self.create_both("chrome.manifest", "a\nb\nc\n") 74 self.create_one("a", "chrome/chrome.manifest", "a\nb\nc\n") 75 self.create_one("b", "chrome/chrome.manifest", "b\nc\na\n") 76 self.assertEqual( 77 sorted([ 78 (f, c.open().read().decode("utf-8")) 79 for f, c in finder.find("**/chrome.manifest") 80 ]), 81 [("chrome.manifest", "a\nb\nc\n"), ("chrome/chrome.manifest", "a\nb\nc\n")], 82 ) 83 84 # Test buildconfig.html unification 85 self.create_one( 86 "a", 87 "chrome/browser/foo/buildconfig.html", 88 "\n".join([ 89 "<html>", 90 " <body>", 91 " <div>", 92 " <h1>Build Configuration</h1>", 93 " <div>foo</div>", 94 " </div>", 95 " </body>", 96 "</html>", 97 ]), 98 ) 99 self.create_one( 100 "b", 101 "chrome/browser/foo/buildconfig.html", 102 "\n".join([ 103 "<html>", 104 " <body>", 105 " <div>", 106 " <h1>Build Configuration</h1>", 107 " <div>bar</div>", 108 " </div>", 109 " </body>", 110 "</html>", 111 ]), 112 ) 113 self.assertEqual( 114 sorted([ 115 (f, c.open().read().decode("utf-8")) 116 for f, c in finder.find("**/buildconfig.html") 117 ]), 118 [ 119 ( 120 "chrome/browser/foo/buildconfig.html", 121 "\n".join([ 122 "<html>", 123 " <body>", 124 " <div>", 125 " <h1>Build Configuration</h1>", 126 " <div>foo</div>", 127 " <hr> </hr>", 128 " <div>bar</div>", 129 " </div>", 130 " </body>", 131 "</html>", 132 ]), 133 ) 134 ], 135 ) 136 137 # Test xpi file unification 138 xpi = MockDest() 139 with JarWriter(fileobj=xpi, compress=True) as jar: 140 jar.add("foo", "foo") 141 jar.add("bar", "bar") 142 foo_xpi = xpi.read() 143 self.create_both("foo.xpi", foo_xpi) 144 145 with JarWriter(fileobj=xpi, compress=True) as jar: 146 jar.add("foo", "bar") 147 self.create_one("a", "bar.xpi", foo_xpi) 148 self.create_one("b", "bar.xpi", xpi.read()) 149 150 errors.out = StringIO() 151 with self.assertRaises(AccumulatedErrors), errors.accumulate(): 152 self.assertEqual( 153 [(f, c.open().read()) for f, c in finder.find("*.xpi")], 154 [("foo.xpi", foo_xpi)], 155 ) 156 errors.out = sys.stderr 157 158 # Test install.rdf unification 159 x86_64 = "Darwin_x86_64-gcc3" 160 x86 = "Darwin_x86-gcc3" 161 target_tag = "<{em}targetPlatform>{platform}</{em}targetPlatform>" 162 target_attr = '{em}targetPlatform="{platform}" ' 163 164 rdf_tag = "".join([ 165 '<{RDF}Description {em}bar="bar" {em}qux="qux">', 166 "<{em}foo>foo</{em}foo>", 167 "{targets}", 168 "<{em}baz>baz</{em}baz>", 169 "</{RDF}Description>", 170 ]) 171 rdf_attr = "".join([ 172 '<{RDF}Description {em}bar="bar" {attr}{em}qux="qux">', 173 "{targets}", 174 "<{em}foo>foo</{em}foo><{em}baz>baz</{em}baz>", 175 "</{RDF}Description>", 176 ]) 177 178 for descr_ns, target_ns in (("RDF:", ""), ("", "em:"), ("RDF:", "em:")): 179 # First we need to infuse the above strings with our namespaces and 180 # platform values. 181 ns = {"RDF": descr_ns, "em": target_ns} 182 target_tag_x86_64 = target_tag.format(platform=x86_64, **ns) 183 target_tag_x86 = target_tag.format(platform=x86, **ns) 184 target_attr_x86_64 = target_attr.format(platform=x86_64, **ns) 185 target_attr_x86 = target_attr.format(platform=x86, **ns) 186 187 tag_x86_64 = rdf_tag.format(targets=target_tag_x86_64, **ns) 188 tag_x86 = rdf_tag.format(targets=target_tag_x86, **ns) 189 tag_merged = rdf_tag.format( 190 targets=target_tag_x86_64 + target_tag_x86, **ns 191 ) 192 tag_empty = rdf_tag.format(targets="", **ns) 193 194 attr_x86_64 = rdf_attr.format(attr=target_attr_x86_64, targets="", **ns) 195 attr_x86 = rdf_attr.format(attr=target_attr_x86, targets="", **ns) 196 attr_merged = rdf_attr.format( 197 attr="", targets=target_tag_x86_64 + target_tag_x86, **ns 198 ) 199 200 # This table defines the test cases, columns "a" and "b" being the 201 # contents of the install.rdf of the respective platform and 202 # "result" the exepected merged content after unification. 203 testcases = ( 204 # _____a_____ _____b_____ ___result___# 205 (tag_x86_64, tag_x86, tag_merged), 206 (tag_x86_64, tag_empty, tag_empty), 207 (tag_empty, tag_x86, tag_empty), 208 (tag_empty, tag_empty, tag_empty), 209 (attr_x86_64, attr_x86, attr_merged), 210 (tag_x86_64, attr_x86, tag_merged), 211 (attr_x86_64, tag_x86, attr_merged), 212 (attr_x86_64, tag_empty, tag_empty), 213 (tag_empty, attr_x86, tag_empty), 214 ) 215 216 # Now create the files from the above table and compare 217 results = [] 218 for emid, (rdf_a, rdf_b, result) in enumerate(testcases): 219 filename = f"ext/id{emid}/install.rdf" 220 self.create_one("a", filename, rdf_a) 221 self.create_one("b", filename, rdf_b) 222 results.append((filename, result)) 223 224 self.assertEqual( 225 sorted([ 226 (f, c.open().read().decode("utf-8")) 227 for f, c in finder.find("**/install.rdf") 228 ]), 229 results, 230 ) 231 232 233 if __name__ == "__main__": 234 mozunit.main()