| 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 unittest |
| 7 |
|
| 8 |
import mozunit |
| 9 |
from buildconfig import topobjdir |
| 10 |
from mozunit import MockedOpen |
| 11 |
|
| 12 |
import mozpack.path as mozpath |
| 13 |
from mozbuild.preprocessor import Preprocessor |
| 14 |
from mozpack.chrome.manifest import ( |
| 15 |
ManifestBinaryComponent, |
| 16 |
ManifestContent, |
| 17 |
ManifestResource, |
| 18 |
) |
| 19 |
from mozpack.errors import ErrorMessage, errors |
| 20 |
from mozpack.files import GeneratedFile |
| 21 |
from mozpack.packager import ( |
| 22 |
CallDeque, |
| 23 |
Component, |
| 24 |
SimpleManifestSink, |
| 25 |
SimplePackager, |
| 26 |
preprocess_manifest, |
| 27 |
) |
| 28 |
|
| 29 |
MANIFEST = """ |
| 30 |
bar/* |
| 31 |
[foo] |
| 32 |
foo/* |
| 33 |
-foo/bar |
| 34 |
chrome.manifest |
| 35 |
[zot destdir="destdir"] |
| 36 |
foo/zot |
| 37 |
; comment |
| 38 |
#ifdef baz |
| 39 |
[baz] |
| 40 |
baz@SUFFIX@ |
| 41 |
#endif |
| 42 |
""" |
| 43 |
|
| 44 |
|
| 45 |
class TestPreprocessManifest(unittest.TestCase): |
| 46 |
MANIFEST_PATH = mozpath.join("$OBJDIR", "manifest") |
| 47 |
|
| 48 |
EXPECTED_LOG = [ |
| 49 |
((MANIFEST_PATH, 2), "add", "", "bar/*"), |
| 50 |
((MANIFEST_PATH, 4), "add", "foo", "foo/*"), |
| 51 |
((MANIFEST_PATH, 5), "remove", "foo", "foo/bar"), |
| 52 |
((MANIFEST_PATH, 6), "add", "foo", "chrome.manifest"), |
| 53 |
((MANIFEST_PATH, 8), "add", 'zot destdir="destdir"', "foo/zot"), |
| 54 |
] |
| 55 |
|
| 56 |
def setUp(self): |
| 57 |
class MockSink: |
| 58 |
def __init__(self): |
| 59 |
self.log = [] |
| 60 |
|
| 61 |
def add(self, component, path): |
| 62 |
self._log(errors.get_context(), "add", repr(component), path) |
| 63 |
|
| 64 |
def remove(self, component, path): |
| 65 |
self._log(errors.get_context(), "remove", repr(component), path) |
| 66 |
|
| 67 |
def _log(self, *args): |
| 68 |
self.log.append(args) |
| 69 |
|
| 70 |
self.sink = MockSink() |
| 71 |
self.cwd = os.getcwd() |
| 72 |
os.chdir(topobjdir) |
| 73 |
|
| 74 |
def tearDown(self): |
| 75 |
os.chdir(self.cwd) |
| 76 |
|
| 77 |
def test_preprocess_manifest(self): |
| 78 |
with MockedOpen({"manifest": MANIFEST}): |
| 79 |
preprocess_manifest(self.sink, "manifest") |
| 80 |
self.assertEqual(self.sink.log, self.EXPECTED_LOG) |
| 81 |
|
| 82 |
def test_preprocess_manifest_missing_define(self): |
| 83 |
with MockedOpen({"manifest": MANIFEST}): |
| 84 |
self.assertRaises( |
| 85 |
Preprocessor.Error, |
| 86 |
preprocess_manifest, |
| 87 |
self.sink, |
| 88 |
"manifest", |
| 89 |
{"baz": 1}, |
| 90 |
) |
| 91 |
|
| 92 |
def test_preprocess_manifest_defines(self): |
| 93 |
with MockedOpen({"manifest": MANIFEST}): |
| 94 |
preprocess_manifest(self.sink, "manifest", {"baz": 1, "SUFFIX": ".exe"}) |
| 95 |
self.assertEqual( |
| 96 |
self.sink.log, |
| 97 |
self.EXPECTED_LOG + [((self.MANIFEST_PATH, 12), "add", "baz", "baz.exe")], |
| 98 |
) |
| 99 |
|
| 100 |
|
| 101 |
class MockFinder: |
| 102 |
def __init__(self, files): |
| 103 |
self.files = files |
| 104 |
self.log = [] |
| 105 |
|
| 106 |
def find(self, path): |
| 107 |
self.log.append(path) |
| 108 |
for f in sorted(self.files): |
| 109 |
if mozpath.match(f, path): |
| 110 |
yield f, self.files[f] |
| 111 |
|
| 112 |
def __iter__(self): |
| 113 |
return self.find("") |
| 114 |
|
| 115 |
|
| 116 |
class MockFormatter: |
| 117 |
def __init__(self): |
| 118 |
self.log = [] |
| 119 |
|
| 120 |
def add_base(self, *args): |
| 121 |
self._log(errors.get_context(), "add_base", *args) |
| 122 |
|
| 123 |
def add_manifest(self, *args): |
| 124 |
self._log(errors.get_context(), "add_manifest", *args) |
| 125 |
|
| 126 |
def add_interfaces(self, *args): |
| 127 |
self._log(errors.get_context(), "add_interfaces", *args) |
| 128 |
|
| 129 |
def add(self, *args): |
| 130 |
self._log(errors.get_context(), "add", *args) |
| 131 |
|
| 132 |
def _log(self, *args): |
| 133 |
self.log.append(args) |
| 134 |
|
| 135 |
|
| 136 |
class TestSimplePackager(unittest.TestCase): |
| 137 |
def test_simple_packager(self): |
| 138 |
class GeneratedFileWithPath(GeneratedFile): |
| 139 |
def __init__(self, path, content): |
| 140 |
GeneratedFile.__init__(self, content) |
| 141 |
self.path = path |
| 142 |
|
| 143 |
formatter = MockFormatter() |
| 144 |
packager = SimplePackager(formatter) |
| 145 |
curdir = os.path.abspath(os.curdir) |
| 146 |
file = GeneratedFileWithPath( |
| 147 |
os.path.join(curdir, "foo", "bar.manifest"), |
| 148 |
b"resource bar bar/\ncontent bar bar/", |
| 149 |
) |
| 150 |
with errors.context("manifest", 1): |
| 151 |
packager.add("foo/bar.manifest", file) |
| 152 |
|
| 153 |
file = GeneratedFileWithPath( |
| 154 |
os.path.join(curdir, "foo", "baz.manifest"), b"resource baz baz/" |
| 155 |
) |
| 156 |
with errors.context("manifest", 2): |
| 157 |
packager.add("bar/baz.manifest", file) |
| 158 |
|
| 159 |
with errors.context("manifest", 3): |
| 160 |
packager.add( |
| 161 |
"qux/qux.manifest", |
| 162 |
GeneratedFile( |
| 163 |
b"".join( |
| 164 |
[ |
| 165 |
b"resource qux qux/\n", |
| 166 |
b"binary-component qux.so\n", |
| 167 |
] |
| 168 |
) |
| 169 |
), |
| 170 |
) |
| 171 |
bar_xpt = GeneratedFile(b"bar.xpt") |
| 172 |
qux_xpt = GeneratedFile(b"qux.xpt") |
| 173 |
foo_html = GeneratedFile(b"foo_html") |
| 174 |
bar_html = GeneratedFile(b"bar_html") |
| 175 |
with errors.context("manifest", 4): |
| 176 |
packager.add("foo/bar.xpt", bar_xpt) |
| 177 |
with errors.context("manifest", 5): |
| 178 |
packager.add("foo/bar/foo.html", foo_html) |
| 179 |
packager.add("foo/bar/bar.html", bar_html) |
| 180 |
|
| 181 |
file = GeneratedFileWithPath( |
| 182 |
os.path.join(curdir, "foo.manifest"), |
| 183 |
b"".join( |
| 184 |
[ |
| 185 |
b"manifest foo/bar.manifest\n", |
| 186 |
b"manifest bar/baz.manifest\n", |
| 187 |
] |
| 188 |
), |
| 189 |
) |
| 190 |
with errors.context("manifest", 6): |
| 191 |
packager.add("foo.manifest", file) |
| 192 |
with errors.context("manifest", 7): |
| 193 |
packager.add("foo/qux.xpt", qux_xpt) |
| 194 |
|
| 195 |
file = GeneratedFileWithPath( |
| 196 |
os.path.join(curdir, "addon", "chrome.manifest"), b"resource hoge hoge/" |
| 197 |
) |
| 198 |
with errors.context("manifest", 8): |
| 199 |
packager.add("addon/chrome.manifest", file) |
| 200 |
|
| 201 |
install_rdf = GeneratedFile(b"<RDF></RDF>") |
| 202 |
with errors.context("manifest", 9): |
| 203 |
packager.add("addon/install.rdf", install_rdf) |
| 204 |
|
| 205 |
with errors.context("manifest", 10): |
| 206 |
packager.add("addon2/install.rdf", install_rdf) |
| 207 |
packager.add( |
| 208 |
"addon2/chrome.manifest", GeneratedFile(b"binary-component addon2.so") |
| 209 |
) |
| 210 |
|
| 211 |
with errors.context("manifest", 11): |
| 212 |
packager.add("addon3/install.rdf", install_rdf) |
| 213 |
packager.add( |
| 214 |
"addon3/chrome.manifest", |
| 215 |
GeneratedFile(b"manifest components/components.manifest"), |
| 216 |
) |
| 217 |
packager.add( |
| 218 |
"addon3/components/components.manifest", |
| 219 |
GeneratedFile(b"binary-component addon3.so"), |
| 220 |
) |
| 221 |
|
| 222 |
with errors.context("manifest", 12): |
| 223 |
install_rdf_addon4 = GeneratedFile( |
| 224 |
b"<RDF>\n<...>\n<em:unpack>true</em:unpack>\n<...>\n</RDF>" |
| 225 |
) |
| 226 |
packager.add("addon4/install.rdf", install_rdf_addon4) |
| 227 |
|
| 228 |
with errors.context("manifest", 13): |
| 229 |
install_rdf_addon5 = GeneratedFile( |
| 230 |
b"<RDF>\n<...>\n<em:unpack>false</em:unpack>\n<...>\n</RDF>" |
| 231 |
) |
| 232 |
packager.add("addon5/install.rdf", install_rdf_addon5) |
| 233 |
|
| 234 |
with errors.context("manifest", 14): |
| 235 |
install_rdf_addon6 = GeneratedFile( |
| 236 |
b"<RDF>\n<... em:unpack=true>\n<...>\n</RDF>" |
| 237 |
) |
| 238 |
packager.add("addon6/install.rdf", install_rdf_addon6) |
| 239 |
|
| 240 |
with errors.context("manifest", 15): |
| 241 |
install_rdf_addon7 = GeneratedFile( |
| 242 |
b"<RDF>\n<... em:unpack=false>\n<...>\n</RDF>" |
| 243 |
) |
| 244 |
packager.add("addon7/install.rdf", install_rdf_addon7) |
| 245 |
|
| 246 |
with errors.context("manifest", 16): |
| 247 |
install_rdf_addon8 = GeneratedFile( |
| 248 |
b'<RDF>\n<... em:unpack="true">\n<...>\n</RDF>' |
| 249 |
) |
| 250 |
packager.add("addon8/install.rdf", install_rdf_addon8) |
| 251 |
|
| 252 |
with errors.context("manifest", 17): |
| 253 |
install_rdf_addon9 = GeneratedFile( |
| 254 |
b'<RDF>\n<... em:unpack="false">\n<...>\n</RDF>' |
| 255 |
) |
| 256 |
packager.add("addon9/install.rdf", install_rdf_addon9) |
| 257 |
|
| 258 |
with errors.context("manifest", 18): |
| 259 |
install_rdf_addon10 = GeneratedFile( |
| 260 |
b"<RDF>\n<... em:unpack='true'>\n<...>\n</RDF>" |
| 261 |
) |
| 262 |
packager.add("addon10/install.rdf", install_rdf_addon10) |
| 263 |
|
| 264 |
with errors.context("manifest", 19): |
| 265 |
install_rdf_addon11 = GeneratedFile( |
| 266 |
b"<RDF>\n<... em:unpack='false'>\n<...>\n</RDF>" |
| 267 |
) |
| 268 |
packager.add("addon11/install.rdf", install_rdf_addon11) |
| 269 |
|
| 270 |
we_manifest = GeneratedFile( |
| 271 |
b'{"manifest_version": 2, "name": "Test WebExtension", "version": "1.0"}' |
| 272 |
) |
| 273 |
# hybrid and hybrid2 are both bootstrapped extensions with |
| 274 |
# embedded webextensions, they differ in the order in which |
| 275 |
# the manifests are added to the packager. |
| 276 |
with errors.context("manifest", 20): |
| 277 |
packager.add("hybrid/install.rdf", install_rdf) |
| 278 |
|
| 279 |
with errors.context("manifest", 21): |
| 280 |
packager.add("hybrid/webextension/manifest.json", we_manifest) |
| 281 |
|
| 282 |
with errors.context("manifest", 22): |
| 283 |
packager.add("hybrid2/webextension/manifest.json", we_manifest) |
| 284 |
|
| 285 |
with errors.context("manifest", 23): |
| 286 |
packager.add("hybrid2/install.rdf", install_rdf) |
| 287 |
|
| 288 |
with errors.context("manifest", 24): |
| 289 |
packager.add("webextension/manifest.json", we_manifest) |
| 290 |
|
| 291 |
non_we_manifest = GeneratedFile(b'{"not a webextension": true}') |
| 292 |
with errors.context("manifest", 25): |
| 293 |
packager.add("nonwebextension/manifest.json", non_we_manifest) |
| 294 |
|
| 295 |
self.assertEqual(formatter.log, []) |
| 296 |
|
| 297 |
with errors.context("dummy", 1): |
| 298 |
packager.close() |
| 299 |
self.maxDiff = None |
| 300 |
# The formatter is expected to reorder the manifest entries so that |
| 301 |
# chrome entries appear before the others. |
| 302 |
self.assertEqual( |
| 303 |
formatter.log, |
| 304 |
[ |
| 305 |
(("dummy", 1), "add_base", "", False), |
| 306 |
(("dummy", 1), "add_base", "addon", True), |
| 307 |
(("dummy", 1), "add_base", "addon10", "unpacked"), |
| 308 |
(("dummy", 1), "add_base", "addon11", True), |
| 309 |
(("dummy", 1), "add_base", "addon2", "unpacked"), |
| 310 |
(("dummy", 1), "add_base", "addon3", "unpacked"), |
| 311 |
(("dummy", 1), "add_base", "addon4", "unpacked"), |
| 312 |
(("dummy", 1), "add_base", "addon5", True), |
| 313 |
(("dummy", 1), "add_base", "addon6", "unpacked"), |
| 314 |
(("dummy", 1), "add_base", "addon7", True), |
| 315 |
(("dummy", 1), "add_base", "addon8", "unpacked"), |
| 316 |
(("dummy", 1), "add_base", "addon9", True), |
| 317 |
(("dummy", 1), "add_base", "hybrid", True), |
| 318 |
(("dummy", 1), "add_base", "hybrid2", True), |
| 319 |
(("dummy", 1), "add_base", "qux", False), |
| 320 |
(("dummy", 1), "add_base", "webextension", True), |
| 321 |
( |
| 322 |
(os.path.join(curdir, "foo", "bar.manifest"), 2), |
| 323 |
"add_manifest", |
| 324 |
ManifestContent("foo", "bar", "bar/"), |
| 325 |
), |
| 326 |
( |
| 327 |
(os.path.join(curdir, "foo", "bar.manifest"), 1), |
| 328 |
"add_manifest", |
| 329 |
ManifestResource("foo", "bar", "bar/"), |
| 330 |
), |
| 331 |
( |
| 332 |
("bar/baz.manifest", 1), |
| 333 |
"add_manifest", |
| 334 |
ManifestResource("bar", "baz", "baz/"), |
| 335 |
), |
| 336 |
( |
| 337 |
("qux/qux.manifest", 1), |
| 338 |
"add_manifest", |
| 339 |
ManifestResource("qux", "qux", "qux/"), |
| 340 |
), |
| 341 |
( |
| 342 |
("qux/qux.manifest", 2), |
| 343 |
"add_manifest", |
| 344 |
ManifestBinaryComponent("qux", "qux.so"), |
| 345 |
), |
| 346 |
(("manifest", 4), "add_interfaces", "foo/bar.xpt", bar_xpt), |
| 347 |
(("manifest", 7), "add_interfaces", "foo/qux.xpt", qux_xpt), |
| 348 |
( |
| 349 |
(os.path.join(curdir, "addon", "chrome.manifest"), 1), |
| 350 |
"add_manifest", |
| 351 |
ManifestResource("addon", "hoge", "hoge/"), |
| 352 |
), |
| 353 |
( |
| 354 |
("addon2/chrome.manifest", 1), |
| 355 |
"add_manifest", |
| 356 |
ManifestBinaryComponent("addon2", "addon2.so"), |
| 357 |
), |
| 358 |
( |
| 359 |
("addon3/components/components.manifest", 1), |
| 360 |
"add_manifest", |
| 361 |
ManifestBinaryComponent("addon3/components", "addon3.so"), |
| 362 |
), |
| 363 |
(("manifest", 5), "add", "foo/bar/foo.html", foo_html), |
| 364 |
(("manifest", 5), "add", "foo/bar/bar.html", bar_html), |
| 365 |
(("manifest", 9), "add", "addon/install.rdf", install_rdf), |
| 366 |
(("manifest", 10), "add", "addon2/install.rdf", install_rdf), |
| 367 |
(("manifest", 11), "add", "addon3/install.rdf", install_rdf), |
| 368 |
(("manifest", 12), "add", "addon4/install.rdf", install_rdf_addon4), |
| 369 |
(("manifest", 13), "add", "addon5/install.rdf", install_rdf_addon5), |
| 370 |
(("manifest", 14), "add", "addon6/install.rdf", install_rdf_addon6), |
| 371 |
(("manifest", 15), "add", "addon7/install.rdf", install_rdf_addon7), |
| 372 |
(("manifest", 16), "add", "addon8/install.rdf", install_rdf_addon8), |
| 373 |
(("manifest", 17), "add", "addon9/install.rdf", install_rdf_addon9), |
| 374 |
(("manifest", 18), "add", "addon10/install.rdf", install_rdf_addon10), |
| 375 |
(("manifest", 19), "add", "addon11/install.rdf", install_rdf_addon11), |
| 376 |
(("manifest", 20), "add", "hybrid/install.rdf", install_rdf), |
| 377 |
( |
| 378 |
("manifest", 21), |
| 379 |
"add", |
| 380 |
"hybrid/webextension/manifest.json", |
| 381 |
we_manifest, |
| 382 |
), |
| 383 |
( |
| 384 |
("manifest", 22), |
| 385 |
"add", |
| 386 |
"hybrid2/webextension/manifest.json", |
| 387 |
we_manifest, |
| 388 |
), |
| 389 |
(("manifest", 23), "add", "hybrid2/install.rdf", install_rdf), |
| 390 |
(("manifest", 24), "add", "webextension/manifest.json", we_manifest), |
| 391 |
( |
| 392 |
("manifest", 25), |
| 393 |
"add", |
| 394 |
"nonwebextension/manifest.json", |
| 395 |
non_we_manifest, |
| 396 |
), |
| 397 |
], |
| 398 |
) |
| 399 |
|
| 400 |
self.assertEqual( |
| 401 |
packager.get_bases(), |
| 402 |
set( |
| 403 |
[ |
| 404 |
"", |
| 405 |
"addon", |
| 406 |
"addon2", |
| 407 |
"addon3", |
| 408 |
"addon4", |
| 409 |
"addon5", |
| 410 |
"addon6", |
| 411 |
"addon7", |
| 412 |
"addon8", |
| 413 |
"addon9", |
| 414 |
"addon10", |
| 415 |
"addon11", |
| 416 |
"qux", |
| 417 |
"hybrid", |
| 418 |
"hybrid2", |
| 419 |
"webextension", |
| 420 |
] |
| 421 |
), |
| 422 |
) |
| 423 |
self.assertEqual(packager.get_bases(addons=False), set(["", "qux"])) |
| 424 |
|
| 425 |
def test_simple_packager_manifest_consistency(self): |
| 426 |
formatter = MockFormatter() |
| 427 |
# bar/ is detected as an addon because of install.rdf, but top-level |
| 428 |
# includes a manifest inside bar/. |
| 429 |
packager = SimplePackager(formatter) |
| 430 |
packager.add( |
| 431 |
"base.manifest", |
| 432 |
GeneratedFile( |
| 433 |
b"manifest foo/bar.manifest\n" b"manifest bar/baz.manifest\n" |
| 434 |
), |
| 435 |
) |
| 436 |
packager.add("foo/bar.manifest", GeneratedFile(b"resource bar bar")) |
| 437 |
packager.add("bar/baz.manifest", GeneratedFile(b"resource baz baz")) |
| 438 |
packager.add("bar/install.rdf", GeneratedFile(b"")) |
| 439 |
|
| 440 |
with self.assertRaises(ErrorMessage) as e: |
| 441 |
packager.close() |
| 442 |
|
| 443 |
self.assertEqual( |
| 444 |
str(e.exception), |
| 445 |
'error: "bar/baz.manifest" is included from "base.manifest", ' |
| 446 |
'which is outside "bar"', |
| 447 |
) |
| 448 |
|
| 449 |
# bar/ is detected as a separate base because of chrome.manifest that |
| 450 |
# is included nowhere, but top-level includes another manifest inside |
| 451 |
# bar/. |
| 452 |
packager = SimplePackager(formatter) |
| 453 |
packager.add( |
| 454 |
"base.manifest", |
| 455 |
GeneratedFile( |
| 456 |
b"manifest foo/bar.manifest\n" b"manifest bar/baz.manifest\n" |
| 457 |
), |
| 458 |
) |
| 459 |
packager.add("foo/bar.manifest", GeneratedFile(b"resource bar bar")) |
| 460 |
packager.add("bar/baz.manifest", GeneratedFile(b"resource baz baz")) |
| 461 |
packager.add("bar/chrome.manifest", GeneratedFile(b"resource baz baz")) |
| 462 |
|
| 463 |
with self.assertRaises(ErrorMessage) as e: |
| 464 |
packager.close() |
| 465 |
|
| 466 |
self.assertEqual( |
| 467 |
str(e.exception), |
| 468 |
'error: "bar/baz.manifest" is included from "base.manifest", ' |
| 469 |
'which is outside "bar"', |
| 470 |
) |
| 471 |
|
| 472 |
# bar/ is detected as a separate base because of chrome.manifest that |
| 473 |
# is included nowhere, but chrome.manifest includes baz.manifest from |
| 474 |
# the same directory. This shouldn't error out. |
| 475 |
packager = SimplePackager(formatter) |
| 476 |
packager.add("base.manifest", GeneratedFile(b"manifest foo/bar.manifest\n")) |
| 477 |
packager.add("foo/bar.manifest", GeneratedFile(b"resource bar bar")) |
| 478 |
packager.add("bar/baz.manifest", GeneratedFile(b"resource baz baz")) |
| 479 |
packager.add("bar/chrome.manifest", GeneratedFile(b"manifest baz.manifest")) |
| 480 |
packager.close() |
| 481 |
|
| 482 |
|
| 483 |
class TestSimpleManifestSink(unittest.TestCase): |
| 484 |
def test_simple_manifest_parser(self): |
| 485 |
formatter = MockFormatter() |
| 486 |
foobar = GeneratedFile(b"foobar") |
| 487 |
foobaz = GeneratedFile(b"foobaz") |
| 488 |
fooqux = GeneratedFile(b"fooqux") |
| 489 |
foozot = GeneratedFile(b"foozot") |
| 490 |
finder = MockFinder( |
| 491 |
{ |
| 492 |
"bin/foo/bar": foobar, |
| 493 |
"bin/foo/baz": foobaz, |
| 494 |
"bin/foo/qux": fooqux, |
| 495 |
"bin/foo/zot": foozot, |
| 496 |
"bin/foo/chrome.manifest": GeneratedFile(b"resource foo foo/"), |
| 497 |
"bin/chrome.manifest": GeneratedFile(b"manifest foo/chrome.manifest"), |
| 498 |
} |
| 499 |
) |
| 500 |
parser = SimpleManifestSink(finder, formatter) |
| 501 |
component0 = Component("component0") |
| 502 |
component1 = Component("component1") |
| 503 |
component2 = Component("component2", destdir="destdir") |
| 504 |
parser.add(component0, "bin/foo/b*") |
| 505 |
parser.add(component1, "bin/foo/qux") |
| 506 |
parser.add(component1, "bin/foo/chrome.manifest") |
| 507 |
parser.add(component2, "bin/foo/zot") |
| 508 |
self.assertRaises(ErrorMessage, parser.add, "component1", "bin/bar") |
| 509 |
|
| 510 |
self.assertEqual(formatter.log, []) |
| 511 |
parser.close() |
| 512 |
self.assertEqual( |
| 513 |
formatter.log, |
| 514 |
[ |
| 515 |
(None, "add_base", "", False), |
| 516 |
( |
| 517 |
("foo/chrome.manifest", 1), |
| 518 |
"add_manifest", |
| 519 |
ManifestResource("foo", "foo", "foo/"), |
| 520 |
), |
| 521 |
(None, "add", "foo/bar", foobar), |
| 522 |
(None, "add", "foo/baz", foobaz), |
| 523 |
(None, "add", "foo/qux", fooqux), |
| 524 |
(None, "add", "destdir/foo/zot", foozot), |
| 525 |
], |
| 526 |
) |
| 527 |
|
| 528 |
self.assertEqual( |
| 529 |
finder.log, |
| 530 |
[ |
| 531 |
"bin/foo/b*", |
| 532 |
"bin/foo/qux", |
| 533 |
"bin/foo/chrome.manifest", |
| 534 |
"bin/foo/zot", |
| 535 |
"bin/bar", |
| 536 |
"bin/chrome.manifest", |
| 537 |
], |
| 538 |
) |
| 539 |
|
| 540 |
|
| 541 |
class TestCallDeque(unittest.TestCase): |
| 542 |
def test_call_deque(self): |
| 543 |
class Logger: |
| 544 |
def __init__(self): |
| 545 |
self._log = [] |
| 546 |
|
| 547 |
def log(self, str): |
| 548 |
self._log.append(str) |
| 549 |
|
| 550 |
@staticmethod |
| 551 |
def staticlog(logger, str): |
| 552 |
logger.log(str) |
| 553 |
|
| 554 |
def do_log(logger, str): |
| 555 |
logger.log(str) |
| 556 |
|
| 557 |
logger = Logger() |
| 558 |
d = CallDeque() |
| 559 |
d.append(logger.log, "foo") |
| 560 |
d.append(logger.log, "bar") |
| 561 |
d.append(logger.staticlog, logger, "baz") |
| 562 |
d.append(do_log, logger, "qux") |
| 563 |
self.assertEqual(logger._log, []) |
| 564 |
d.execute() |
| 565 |
self.assertEqual(logger._log, ["foo", "bar", "baz", "qux"]) |
| 566 |
|
| 567 |
|
| 568 |
class TestComponent(unittest.TestCase): |
| 569 |
def do_split(self, string, name, options): |
| 570 |
n, o = Component._split_component_and_options(string) |
| 571 |
self.assertEqual(name, n) |
| 572 |
self.assertEqual(options, o) |
| 573 |
|
| 574 |
def test_component_split_component_and_options(self): |
| 575 |
self.do_split("component", "component", {}) |
| 576 |
self.do_split("trailingspace ", "trailingspace", {}) |
| 577 |
self.do_split(" leadingspace", "leadingspace", {}) |
| 578 |
self.do_split(" trim ", "trim", {}) |
| 579 |
self.do_split(' trim key="value"', "trim", {"key": "value"}) |
| 580 |
self.do_split(' trim empty=""', "trim", {"empty": ""}) |
| 581 |
self.do_split(' trim space=" "', "trim", {"space": " "}) |
| 582 |
self.do_split( |
| 583 |
'component key="value" key2="second" ', |
| 584 |
"component", |
| 585 |
{"key": "value", "key2": "second"}, |
| 586 |
) |
| 587 |
self.do_split( |
| 588 |
'trim key=" value with spaces " key2="spaces again"', |
| 589 |
"trim", |
| 590 |
{"key": " value with spaces ", "key2": "spaces again"}, |
| 591 |
) |
| 592 |
|
| 593 |
def do_split_error(self, string): |
| 594 |
self.assertRaises(ValueError, Component._split_component_and_options, string) |
| 595 |
|
| 596 |
def test_component_split_component_and_options_errors(self): |
| 597 |
self.do_split_error('"component') |
| 598 |
self.do_split_error('comp"onent') |
| 599 |
self.do_split_error('component"') |
| 600 |
self.do_split_error('"component"') |
| 601 |
self.do_split_error("=component") |
| 602 |
self.do_split_error("comp=onent") |
| 603 |
self.do_split_error("component=") |
| 604 |
self.do_split_error('key="val"') |
| 605 |
self.do_split_error("component key=") |
| 606 |
self.do_split_error('component key="val') |
| 607 |
self.do_split_error('component key=val"') |
| 608 |
self.do_split_error('component key="val" x') |
| 609 |
self.do_split_error('component x key="val"') |
| 610 |
self.do_split_error('component key1="val" x key2="val"') |
| 611 |
|
| 612 |
def do_from_string(self, string, name, destdir=""): |
| 613 |
component = Component.from_string(string) |
| 614 |
self.assertEqual(name, component.name) |
| 615 |
self.assertEqual(destdir, component.destdir) |
| 616 |
|
| 617 |
def test_component_from_string(self): |
| 618 |
self.do_from_string("component", "component") |
| 619 |
self.do_from_string("component-with-hyphen", "component-with-hyphen") |
| 620 |
self.do_from_string('component destdir="foo/bar"', "component", "foo/bar") |
| 621 |
self.do_from_string('component destdir="bar spc"', "component", "bar spc") |
| 622 |
self.assertRaises(ErrorMessage, Component.from_string, "") |
| 623 |
self.assertRaises(ErrorMessage, Component.from_string, "component novalue=") |
| 624 |
self.assertRaises( |
| 625 |
ErrorMessage, Component.from_string, "component badoption=badvalue" |
| 626 |
) |
| 627 |
|
| 628 |
|
| 629 |
if __name__ == "__main__": |
| 630 |
mozunit.main() |