| 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 |
[ |
| 79 |
(f, c.open().read().decode("utf-8")) |
| 80 |
for f, c in finder.find("**/chrome.manifest") |
| 81 |
] |
| 82 |
), |
| 83 |
[("chrome.manifest", "a\nb\nc\n"), ("chrome/chrome.manifest", "a\nb\nc\n")], |
| 84 |
) |
| 85 |
|
| 86 |
# Test buildconfig.html unification |
| 87 |
self.create_one( |
| 88 |
"a", |
| 89 |
"chrome/browser/foo/buildconfig.html", |
| 90 |
"\n".join( |
| 91 |
[ |
| 92 |
"<html>", |
| 93 |
" <body>", |
| 94 |
" <div>", |
| 95 |
" <h1>Build Configuration</h1>", |
| 96 |
" <div>foo</div>", |
| 97 |
" </div>", |
| 98 |
" </body>", |
| 99 |
"</html>", |
| 100 |
] |
| 101 |
), |
| 102 |
) |
| 103 |
self.create_one( |
| 104 |
"b", |
| 105 |
"chrome/browser/foo/buildconfig.html", |
| 106 |
"\n".join( |
| 107 |
[ |
| 108 |
"<html>", |
| 109 |
" <body>", |
| 110 |
" <div>", |
| 111 |
" <h1>Build Configuration</h1>", |
| 112 |
" <div>bar</div>", |
| 113 |
" </div>", |
| 114 |
" </body>", |
| 115 |
"</html>", |
| 116 |
] |
| 117 |
), |
| 118 |
) |
| 119 |
self.assertEqual( |
| 120 |
sorted( |
| 121 |
[ |
| 122 |
(f, c.open().read().decode("utf-8")) |
| 123 |
for f, c in finder.find("**/buildconfig.html") |
| 124 |
] |
| 125 |
), |
| 126 |
[ |
| 127 |
( |
| 128 |
"chrome/browser/foo/buildconfig.html", |
| 129 |
"\n".join( |
| 130 |
[ |
| 131 |
"<html>", |
| 132 |
" <body>", |
| 133 |
" <div>", |
| 134 |
" <h1>Build Configuration</h1>", |
| 135 |
" <div>foo</div>", |
| 136 |
" <hr> </hr>", |
| 137 |
" <div>bar</div>", |
| 138 |
" </div>", |
| 139 |
" </body>", |
| 140 |
"</html>", |
| 141 |
] |
| 142 |
), |
| 143 |
) |
| 144 |
], |
| 145 |
) |
| 146 |
|
| 147 |
# Test xpi file unification |
| 148 |
xpi = MockDest() |
| 149 |
with JarWriter(fileobj=xpi, compress=True) as jar: |
| 150 |
jar.add("foo", "foo") |
| 151 |
jar.add("bar", "bar") |
| 152 |
foo_xpi = xpi.read() |
| 153 |
self.create_both("foo.xpi", foo_xpi) |
| 154 |
|
| 155 |
with JarWriter(fileobj=xpi, compress=True) as jar: |
| 156 |
jar.add("foo", "bar") |
| 157 |
self.create_one("a", "bar.xpi", foo_xpi) |
| 158 |
self.create_one("b", "bar.xpi", xpi.read()) |
| 159 |
|
| 160 |
errors.out = StringIO() |
| 161 |
with self.assertRaises(AccumulatedErrors), errors.accumulate(): |
| 162 |
self.assertEqual( |
| 163 |
[(f, c.open().read()) for f, c in finder.find("*.xpi")], |
| 164 |
[("foo.xpi", foo_xpi)], |
| 165 |
) |
| 166 |
errors.out = sys.stderr |
| 167 |
|
| 168 |
# Test install.rdf unification |
| 169 |
x86_64 = "Darwin_x86_64-gcc3" |
| 170 |
x86 = "Darwin_x86-gcc3" |
| 171 |
target_tag = "<{em}targetPlatform>{platform}</{em}targetPlatform>" |
| 172 |
target_attr = '{em}targetPlatform="{platform}" ' |
| 173 |
|
| 174 |
rdf_tag = "".join( |
| 175 |
[ |
| 176 |
'<{RDF}Description {em}bar="bar" {em}qux="qux">', |
| 177 |
"<{em}foo>foo</{em}foo>", |
| 178 |
"{targets}", |
| 179 |
"<{em}baz>baz</{em}baz>", |
| 180 |
"</{RDF}Description>", |
| 181 |
] |
| 182 |
) |
| 183 |
rdf_attr = "".join( |
| 184 |
[ |
| 185 |
'<{RDF}Description {em}bar="bar" {attr}{em}qux="qux">', |
| 186 |
"{targets}", |
| 187 |
"<{em}foo>foo</{em}foo><{em}baz>baz</{em}baz>", |
| 188 |
"</{RDF}Description>", |
| 189 |
] |
| 190 |
) |
| 191 |
|
| 192 |
for descr_ns, target_ns in (("RDF:", ""), ("", "em:"), ("RDF:", "em:")): |
| 193 |
# First we need to infuse the above strings with our namespaces and |
| 194 |
# platform values. |
| 195 |
ns = {"RDF": descr_ns, "em": target_ns} |
| 196 |
target_tag_x86_64 = target_tag.format(platform=x86_64, **ns) |
| 197 |
target_tag_x86 = target_tag.format(platform=x86, **ns) |
| 198 |
target_attr_x86_64 = target_attr.format(platform=x86_64, **ns) |
| 199 |
target_attr_x86 = target_attr.format(platform=x86, **ns) |
| 200 |
|
| 201 |
tag_x86_64 = rdf_tag.format(targets=target_tag_x86_64, **ns) |
| 202 |
tag_x86 = rdf_tag.format(targets=target_tag_x86, **ns) |
| 203 |
tag_merged = rdf_tag.format( |
| 204 |
targets=target_tag_x86_64 + target_tag_x86, **ns |
| 205 |
) |
| 206 |
tag_empty = rdf_tag.format(targets="", **ns) |
| 207 |
|
| 208 |
attr_x86_64 = rdf_attr.format(attr=target_attr_x86_64, targets="", **ns) |
| 209 |
attr_x86 = rdf_attr.format(attr=target_attr_x86, targets="", **ns) |
| 210 |
attr_merged = rdf_attr.format( |
| 211 |
attr="", targets=target_tag_x86_64 + target_tag_x86, **ns |
| 212 |
) |
| 213 |
|
| 214 |
# This table defines the test cases, columns "a" and "b" being the |
| 215 |
# contents of the install.rdf of the respective platform and |
| 216 |
# "result" the exepected merged content after unification. |
| 217 |
testcases = ( |
| 218 |
# _____a_____ _____b_____ ___result___# |
| 219 |
(tag_x86_64, tag_x86, tag_merged), |
| 220 |
(tag_x86_64, tag_empty, tag_empty), |
| 221 |
(tag_empty, tag_x86, tag_empty), |
| 222 |
(tag_empty, tag_empty, tag_empty), |
| 223 |
(attr_x86_64, attr_x86, attr_merged), |
| 224 |
(tag_x86_64, attr_x86, tag_merged), |
| 225 |
(attr_x86_64, tag_x86, attr_merged), |
| 226 |
(attr_x86_64, tag_empty, tag_empty), |
| 227 |
(tag_empty, attr_x86, tag_empty), |
| 228 |
) |
| 229 |
|
| 230 |
# Now create the files from the above table and compare |
| 231 |
results = [] |
| 232 |
for emid, (rdf_a, rdf_b, result) in enumerate(testcases): |
| 233 |
filename = f"ext/id{emid}/install.rdf" |
| 234 |
self.create_one("a", filename, rdf_a) |
| 235 |
self.create_one("b", filename, rdf_b) |
| 236 |
results.append((filename, result)) |
| 237 |
|
| 238 |
self.assertEqual( |
| 239 |
sorted( |
| 240 |
[ |
| 241 |
(f, c.open().read().decode("utf-8")) |
| 242 |
for f, c in finder.find("**/install.rdf") |
| 243 |
] |
| 244 |
), |
| 245 |
results, |
| 246 |
) |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == "__main__": |
| 250 |
mozunit.main() |