Bug 910660 - Add a test for the unpacker code. r=gps

This commit is contained in:
Mike Hommey 2015-03-13 18:21:53 +09:00
Родитель dc2c448768
Коммит 0ca5f49005
4 изменённых файлов: 83 добавлений и 8 удалений

Просмотреть файл

@ -74,6 +74,7 @@ PYTHON_UNIT_TESTS += [
'mozbuild/mozpack/test/test_packager.py',
'mozbuild/mozpack/test/test_packager_formats.py',
'mozbuild/mozpack/test/test_packager_l10n.py',
'mozbuild/mozpack/test/test_packager_unpack.py',
'mozbuild/mozpack/test/test_path.py',
'mozbuild/mozpack/test/test_unify.py',
]

Просмотреть файл

@ -160,15 +160,24 @@ class UnpackFinder(FileFinder):
return mozpack.path.join(base, jar), entry
def unpack_to_registry(source, registry):
'''
Transform a jar chrome or omnijar packaged directory into a flat package.
The given registry is filled with the flat package.
'''
finder = UnpackFinder(source)
packager = SimplePackager(FlatFormatter(registry))
for p, f in finder.find('*'):
if mozpack.path.split(p)[0] not in STARTUP_CACHE_PATHS:
packager.add(p, f)
packager.close()
def unpack(source):
'''
Transform a jar chrome or omnijar packaged directory into a flat package.
'''
copier = FileCopier()
finder = UnpackFinder(source)
packager = SimplePackager(FlatFormatter(copier))
for p, f in finder.find('*'):
if mozpack.path.split(p)[0] not in STARTUP_CACHE_PATHS:
packager.add(p, f)
packager.close()
unpack_to_registry(source, copier)
copier.copy(source, skip_if_older=False)

Просмотреть файл

@ -73,7 +73,7 @@ def fill_formatter(formatter, contents):
formatter.add(k, v)
def get_contents(registry):
def get_contents(registry, read_all=False):
result = {}
for k, v in registry:
if k.endswith('.xpt'):
@ -82,7 +82,7 @@ def get_contents(registry):
result[k] = read_interfaces(tmpfile)
elif isinstance(v, FileRegistry):
result[k] = get_contents(v)
elif isinstance(v, ManifestFile):
elif isinstance(v, ManifestFile) or read_all:
result[k] = v.open().read().splitlines()
else:
result[k] = v

Просмотреть файл

@ -0,0 +1,65 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import mozunit
from mozpack.packager.formats import (
FlatFormatter,
JarFormatter,
OmniJarFormatter,
)
from mozpack.packager.unpack import unpack_to_registry
from mozpack.copier import (
FileCopier,
FileRegistry,
)
from mozpack.test.test_packager_formats import (
CONTENTS,
fill_formatter,
get_contents,
)
from mozpack.test.test_files import TestWithTmpDir
class TestUnpack(TestWithTmpDir):
maxDiff = None
@staticmethod
def _get_copier(cls):
copier = FileCopier()
formatter = cls(copier)
fill_formatter(formatter, CONTENTS)
return copier
@classmethod
def setUpClass(cls):
cls.contents = get_contents(cls._get_copier(FlatFormatter),
read_all=True)
def _unpack_test(self, cls):
# Format a package with the given formatter class
copier = self._get_copier(cls)
copier.copy(self.tmpdir)
# Unpack that package. Its content is expected to match that of a Flat
# formatted package.
registry = FileRegistry()
unpack_to_registry(self.tmpdir, registry)
self.assertEqual(get_contents(registry, read_all=True), self.contents)
def test_flat_unpack(self):
self._unpack_test(FlatFormatter)
def test_jar_unpack(self):
self._unpack_test(JarFormatter)
def test_omnijar_unpack(self):
class OmniFooFormatter(OmniJarFormatter):
def __init__(self, registry):
super(OmniFooFormatter, self).__init__(registry, 'omni.foo')
self._unpack_test(OmniFooFormatter)
if __name__ == '__main__':
mozunit.main()