From 0ca5f49005e6b211f134089bb5b35a548b1906ec Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 13 Mar 2015 18:21:53 +0900 Subject: [PATCH] Bug 910660 - Add a test for the unpacker code. r=gps --- python/moz.build | 1 + python/mozbuild/mozpack/packager/unpack.py | 21 ++++-- .../mozpack/test/test_packager_formats.py | 4 +- .../mozpack/test/test_packager_unpack.py | 65 +++++++++++++++++++ 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 python/mozbuild/mozpack/test/test_packager_unpack.py diff --git a/python/moz.build b/python/moz.build index fad8266fb9dc..d9f2064c2143 100644 --- a/python/moz.build +++ b/python/moz.build @@ -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', ] diff --git a/python/mozbuild/mozpack/packager/unpack.py b/python/mozbuild/mozpack/packager/unpack.py index 5aff9fcf406e..8a25fd74ea8d 100644 --- a/python/mozbuild/mozpack/packager/unpack.py +++ b/python/mozbuild/mozpack/packager/unpack.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) diff --git a/python/mozbuild/mozpack/test/test_packager_formats.py b/python/mozbuild/mozpack/test/test_packager_formats.py index cd57e27b3eff..cf6ec7ee8f15 100644 --- a/python/mozbuild/mozpack/test/test_packager_formats.py +++ b/python/mozbuild/mozpack/test/test_packager_formats.py @@ -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 diff --git a/python/mozbuild/mozpack/test/test_packager_unpack.py b/python/mozbuild/mozpack/test/test_packager_unpack.py new file mode 100644 index 000000000000..d201cabf74c4 --- /dev/null +++ b/python/mozbuild/mozpack/test/test_packager_unpack.py @@ -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()